diff --git a/README.md b/README.md index ebf0b3f..902b535 100644 --- a/README.md +++ b/README.md @@ -162,6 +162,31 @@ julia> W2Mstr(W`b/(c^(a+c))`) ``` + + +## W2Julia - Conversion to Julia structures +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. + +Some examples or tests that will evaluate to true: + +``` +using Test +@test W2Julia(W`{1,2,3}`) == [1,2,3] +@test W2Julia([1,2,3]) == [1,2,3] +@test W2Julia(W`{1,2,3}`) == [1,2,3] +@test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]] +@test W2Julia([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]] + +@test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2) + +@test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D") + +@test W2Julia(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C") +``` + +W2Julia does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present. + + ## LateX printing in JuPyter Notebooks Printing in Jupyter notebooks is, by default, done in latex. This can be turned off with the command `MathLink.set_texOutput(false)` diff --git a/src/W2Julia.jl b/src/W2Julia.jl index 5c70ae4..cdb282b 100644 --- a/src/W2Julia.jl +++ b/src/W2Julia.jl @@ -2,6 +2,29 @@ export W2Julia +""" +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. + +Some examples or tests that will evaluate to true: + + using Test + @test W2Julia(W`{1,2,3}`) == [1,2,3] + @test W2Julia([1,2,3]) == [1,2,3] + @test W2Julia(W`{1,2,3}`) == [1,2,3] + @test W2Julia(W`{1,a,{1,2}}`) == [1,W"a",[1,2]] + @test W2Julia([.1,W`{1,a,3}`]) == [.1,[1,W"a",3]] + + @test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2) + + + @test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D") + + @test W2Julia(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C") + + +W2Julia does not convert expressions to Julia functions, as not all functions will be able to evaluate when WSymbols are present. + +""" W2Julia(X::Vector) = [ W2Julia(x) for x in X] function W2Julia(X::Dict) NewDict = Dict() diff --git a/test/runtests.jl b/test/runtests.jl index 0f9ced4..dec19af 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -6,7 +6,6 @@ import MathLink: WExpr, WSymbol @testset "W2Julia" begin ###Test of a simple MathLink to Julia converter. It converts MathLink expressions to the correcsponding Julia constructions - @testset "Lists to Lists" begin @test W2Julia(W`{1,2,3}`) == [1,2,3] @test W2Julia([1,2,3]) == [1,2,3] @@ -19,10 +18,14 @@ import MathLink: WExpr, WSymbol @testset "Association to Dict" begin @test W2Julia(Dict( 1 => "A" , "B" => 2)) ==Dict( 1 => "A" , "B" => 2) - @test W2Julia(W`Association["team" -> "HOU", "lastName" -> "Ching"]`) == Dict( "team" => "HOU" , "lastName" => "Ching") + @test W2Julia(W`Association["A" -> "B", "C" -> "D"]`) == Dict( "A" => "B" , "C" => "D") end @testset "Association and List Dict" begin - @test W2Julia(W`Association["team" -> {1,2,3}, "lastName" -> "Ching"]`) == Dict( "team" => [1,2,3] , "lastName" => "Ching") + @test W2Julia(W`Association["A" -> {1,2,3}, "B" -> "C"]`) == Dict( "A" => [1,2,3] , "B" => "C") + + @test W2Julia(W`Association["A" -> {1,a,3}, "B" -> "C"]`) == Dict( "A" => [1,W"a",3] , "B" => "C") + + @test W2Julia(W`{1,Association["team" -> {1,2,3}, "lastName" -> "Ching"]}`) == [1,Dict( "team" => [1,2,3] , "lastName" => "Ching")] end