如何将Dict作为方法Julia的参数传递

时间:2016-05-21 07:53:58

标签: julia

您好我尝试使用Julia语言中的Disct to Vector创建转换器方法。 但我收到错误,我无法理解

ERROR: TypeError: Tuple: in parameter, expected Type{T}, got Dict{AbstractString,Int64}

我的代码

type Family
    name::UTF8String
    value::Int
end

function convertToVector(a1::Dict{AbstractString, Int64}())
              A::Vector{Node}
          for k in sort(collect(keys(a1)))
              push!(A, Family(a1[k] , k))
              end
          return A
       end

有什么想法改变convertToVector方法吗?

2 个答案:

答案 0 :(得分:4)

上面的代码中有几个拼写错误,但我认为这应该有效:

public void demo() {
    try {
        client = new MqttClient("tcp://broker:1883", "Sending");
        client.connect();
        client.setCallback(this);
        client.subscribe("receive");
    } catch (MqttException e) {}
}


@Override
public void messageArrived(String topic, MqttMessage message)
        throws Exception {      
    message.setPayload("I'm replying".getBytes());
    client.publish("publish", message);
}

答案 1 :(得分:1)

另一种方式(可能不是很快):

julia> dict = Dict("fred" => 3, "jim" => 4)
Dict{ASCIIString,Int64} with 2 entries:
  "fred" => 3
  "jim"  => 4

julia> Vector{Family}(map(f -> Family(f...), map(x -> collect(x), dict)))
2-element Array{Family,1}:
 Family("fred",3)
 Family("jim",4)

也许我最近一直在使用过多的Lisp ......