MathLink.jl
MathLink.jl copied to clipboard
Extracting values from lists
How do we extract numbers from lists? More generally, how are we supposed to use various return values in Julia?
For example, this code returns a list of lists:
weval(W`Table[N[MathieuCharacteristicB[m, q]], {q, 195, 201, 1/10}, {m, 16, 22, 2}]`)
I'd be happy to put an example in the README on how to do this.
You can just access the .args
field of the returned object, but that is probably not ideal. This is related to #22.
I guess there is no harm in making them iterable and defining getindex
on them? then you could just call collect
?
Would there be any efficiency issues (e.g unnecessary copying)?
Yes, but we already copy so much stuff it probably wouldn’t make much difference.
Ok, then that seems fine, intuitive (I did try calling collect
initially to see if it would work), and certainly better than the current situation.
Would it be at-all useful to define specialized collect
methods that would just return args
when we detect that it is already an array?
we may want a different function, since collect
doesn't alias even for arrays:
julia> X = rand(3);
julia> collect(X) === X
false
parent
is often used for similar things, and might make sense?
Hi @jebej , based on ticket (https://github.com/JuliaInterop/MathLink.jl/issues/72) I have snuck in the feature (W2Julia
) in into the 0.5.3 release of yesterday (https://github.com/JuliaInterop/MathLink.jl/releases/tag/v0.5.3). It aims to convert MathLink objects to Julia style objects.
At the moment it can convert the "Assumptions" to a dictionary and lists to a list. Below are some examples:
@test W2Julia(W`{1,2,3}`) == [1,2,3]
@test W2Julia(W`{1,a,3}`) == [1,W"a",3]
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
@test W2Julia(W`Association["team" -> "HOU", "lastName" -> "Ching"]`) == Dict( "team" => "HOU" , "lastName" => "Ching")
@test W2Julia(W`Association["team" -> {1,2,3}, "lastName" -> "Ching"]`) == Dict( "team" => [1,2,3] , "lastName" => "Ching")
@test W2Julia(W`{1,Association["team" -> {1,2,3}, "lastName" -> "Ching"]}`) == [1,Dict( "team" => [1,2,3] , "lastName" => "Ching")]
As this is almost at the embryo stage, feel free to report strange (or lacking) behavior i other tickets.
As the W2Julia
function solves the original issue is will close this ticket.