Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extracting values from lists #42

Closed
jebej opened this issue May 19, 2020 · 8 comments
Closed

Extracting values from lists #42

jebej opened this issue May 19, 2020 · 8 comments

Comments

@jebej
Copy link

jebej commented May 19, 2020

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.

@simonbyrne
Copy link
Member

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

@simonbyrne
Copy link
Member

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

@jebej
Copy link
Author

jebej commented May 26, 2020

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

@simonbyrne
Copy link
Member

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

@jebej
Copy link
Author

jebej commented May 26, 2020

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?

@simonbyrne
Copy link
Member

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?

@fremling
Copy link
Collaborator

fremling commented Nov 21, 2023

Hi @jebej , based on ticket (#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
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants