MathLink.jl icon indicating copy to clipboard operation
MathLink.jl copied to clipboard

Extracting values from lists

Open jebej opened this issue 4 years ago • 6 comments

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.

jebej avatar May 19 '20 17:05 jebej

You can just access the .args field of the returned object, but that is probably not ideal. This is related to #22.

simonbyrne avatar May 19 '20 19:05 simonbyrne

I guess there is no harm in making them iterable and defining getindex on them? then you could just call collect?

simonbyrne avatar May 26 '20 03:05 simonbyrne

Would there be any efficiency issues (e.g unnecessary copying)?

jebej avatar May 26 '20 12:05 jebej

Yes, but we already copy so much stuff it probably wouldn’t make much difference.

simonbyrne avatar May 26 '20 15:05 simonbyrne

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?

jebej avatar May 26 '20 15:05 jebej

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?

simonbyrne avatar May 26 '20 16:05 simonbyrne

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.

fremling avatar Nov 21 '23 10:11 fremling

As the W2Julia function solves the original issue is will close this ticket.

fremling avatar Nov 28 '23 15:11 fremling