Skip to content

Commit b326652

Browse files
committed
Renamed W2Julia -> W2JuliaStruct
1 parent 002f058 commit b326652

File tree

4 files changed

+84
-84
lines changed

4 files changed

+84
-84
lines changed

README.md

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,27 @@ julia> W2Mstr(W`b/(c^(a+c))`)
164164

165165

166166

167-
## W2Julia - Conversion to Julia structures
168-
W2Julia is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.
167+
## W2JuliaStruct - Conversion to Julia structures
168+
`W2JuliaStruct` is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.
169169

170170
Some examples or tests that will evaluate to true:
171171

172172
```julia
173173
using Test
174-
@test W2Julia(W`{1,2,3}`) == [1,2,3]
175-
@test W2Julia([1,2,3]) == [1,2,3]
176-
@test W2Julia(W`{1,2,3}`) == [1,2,3]
177-
@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
178-
@test W2Julia([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
174+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
175+
@test W2JuliaStruct([1,2,3]) == [1,2,3]
176+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
177+
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
178+
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
179179

180-
@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
180+
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
181181

182-
@test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
182+
@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
183183

184-
@test W2Julia(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
184+
@test W2JuliaStruct(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
185185
```
186186

187-
W2Julia does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.
187+
`W2JuliaStruct` does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.
188188

189189

190190
## LateX printing in JuPyter Notebooks

src/MathLink.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ include("extras.jl")
2929
include("eval.jl")
3030
include("display.jl")
3131
include("operators.jl")
32-
include("W2Julia.jl")
32+
include("W2JuliaStruct.jl")
3333
end

src/W2Julia.jl

Lines changed: 0 additions & 72 deletions
This file was deleted.

src/W2JuliaStruct.jl

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
2+
3+
export W2JuliaStruct
4+
5+
"""
6+
W2JuliaStruct is designed primarily to convert wolfram structures to Julia structures. This includes conversions of Mathematica lists to Julia vectors and Mathematica associations to Julia dictionaries.
7+
8+
Some examples or tests that will evaluate to true:
9+
10+
using Test
11+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
12+
@test W2JuliaStruct([1,2,3]) == [1,2,3]
13+
@test W2JuliaStruct(W`{1,2,3}`) == [1,2,3]
14+
@test W2JuliaStruct(W`{1,a,{1,2}}`) == [1,W"a",[1,2]]
15+
@test W2JuliaStruct([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]]
16+
17+
@test W2JuliaStruct(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2)
18+
19+
20+
@test W2JuliaStruct(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D")
21+
22+
@test W2JuliaStruct(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C")
23+
24+
25+
W2JuliaStruct does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present.
26+
27+
"""
28+
W2JuliaStruct(X::Vector) = [ W2JuliaStruct(x) for x in X]
29+
function W2JuliaStruct(X::Dict)
30+
NewDict = Dict()
31+
for key in keys(X)
32+
NewDict[key] = W2JuliaStruct(X[key])
33+
end
34+
return NewDict
35+
end
36+
37+
38+
39+
W2JuliaStruct(X::Number) = X
40+
W2JuliaStruct(X::String) = X
41+
W2JuliaStruct(X::MathLink.WSymbol) = X
42+
function W2JuliaStruct(X::MathLink.WExpr)
43+
if X.head == W"List"
44+
return W2JuliaStruct.(X.args)
45+
elseif X.head == W"Association"
46+
W2JuliaStructAccociation(X)
47+
else
48+
return X
49+
end
50+
end
51+
52+
53+
function W2JuliaStructAccociation(Asso::MathLink.WExpr)
54+
if Asso.head != W"Association"
55+
error("Not an Association")
56+
end
57+
##Wprint(Asso)
58+
if Asso.head != W"Association"
59+
error("Not an association")
60+
end
61+
D=Dict()
62+
for Rule in Asso.args
63+
if Rule.head != W"Rule"
64+
error("not a rule")
65+
end
66+
if length(Rule.args) != 2
67+
error("Bad rule")
68+
end
69+
D[Rule.args[1]]=W2JuliaStruct(Rule.args[2])
70+
end
71+
return D
72+
end

0 commit comments

Comments
 (0)