{"category":{"__v":2,"_id":"566e55c12c1e760d0030c56d","pages":["566e55c12c1e760d0030c56e","566e55c12c1e760d0030c56f","566e55c12c1e760d0030c570","566e55c12c1e760d0030c571","566e55c12c1e760d0030c572","566e55c12c1e760d0030c573","566e55c12c1e760d0030c574","566e55c12c1e760d0030c575","566e55c12c1e760d0030c576","566e55c12c1e760d0030c577","566e55c12c1e760d0030c578","566e55c12c1e760d0030c579","566e55c12c1e760d0030c57a","566e55c12c1e760d0030c57b","566e55c12c1e760d0030c57c","566e55c12c1e760d0030c57d","566e55c12c1e760d0030c57e","566e55c12c1e760d0030c57f","566e55c12c1e760d0030c580","566e55c12c1e760d0030c581","566e55c12c1e760d0030c582","566e55c12c1e760d0030c583","566e55c12c1e760d0030c584","566e55c12c1e760d0030c585","566e55c12c1e760d0030c586","566e55c12c1e760d0030c587","566e55c12c1e760d0030c588","566e5640c15c8f0d000ee863"],"project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c","sync":{"url":"","isSync":false},"reference":false,"createdAt":"2014-10-17T03:34:44.886Z","from_sync":false,"order":0,"slug":"documentation","title":"Documentation"},"project":"54408e54309354080070a896","user":"544083bee239230800071bef","version":{"__v":1,"_id":"566e55c02c1e760d0030c56c","project":"54408e54309354080070a896","createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","categories":["566e55c12c1e760d0030c56d"],"is_deprecated":false,"is_hidden":false,"is_beta":false,"is_stable":true,"codename":"","version_clean":"1.2.3","version":"1.2.3"},"_id":"566e55c12c1e760d0030c56f","__v":0,"updates":[],"next":{"pages":[],"description":""},"createdAt":"2014-10-17T03:34:44.928Z","link_external":false,"link_url":"","githubsync":"","sync_unique":"","hidden":false,"api":{"results":{"codes":[]},"auth":"never","params":[],"url":""},"isReference":false,"order":1,"body":"# Julia can be used from Javascript directly using eval or exec. \n\n## eval allows Julia source to be embedded in Javascript as a string\nThe function **eval** takes a string and evaluates that string as if it were typed in the Julia REPL.  The following computes the sum of the first 100 integers\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"julia = require('node-julia');\\n\\nres = julia.eval('sum(Int64[n for n=1:100])');\\nconsole.log(\\\"sum of first 100 integers: \\\",res);\",\n      \"language\": \"javascript\",\n      \"name\": null\n    }\n  ]\n}\n[/block]\nThe result of the computation is returned as the function result. Alternatively there is a form where the results are returned as variables bound to arguments in a callback function.\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"julia = require('node-julia');\\n\\njulia.eval('sum(Int64[n for n=1:100])',function(err,res) {\\n  if(err) console.log(\\\"error in computation \\\",err);\\n  else console.log(\\\"sum of first 100 integers: \\\",res);\\n});\\n\\nsetTimeout(function(){ console.log('done.'); },1000);\",\n      \"language\": \"javascript\",\n      \"name\": \"sum.js\"\n    }\n  ]\n}\n[/block]\n\n## exec allows predefined Julia functions to be invoked by name\nThe function **exec** takes a string as the name of a function which may be module qualified, and any number of additional arguments that will be passed to that function.  \n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"julia = require('node-julia');\\n\\nvar a = julia.eval('rand(1:20000,1000)');\\nvar median = julia.exec('Base.median',a);\\n\\nconsole.log(\\\"median of 1000 random integers between 1 and 20000: \\\",median);\",\n      \"language\": \"javascript\",\n      \"name\": \"mean.js\"\n    }\n  ]\n}\n[/block]\n## Or Julia source code can be placed in an external file and then included\nFor example, using the definition of **mandel** located on [Julia Lang](http://julialang.org) is put in a file mandel.jl\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"function mandel(z)\\n   c = z\\n   maxiter = 80\\n   for n = 1:maxiter\\n      if abs(z) > 2\\n         return n-1\\n      end\\n      z = z^2 + c\\n   end\\n   return maxiter\\nend\",\n      \"language\": \"julia\",\n      \"name\": \"mandel.jl\"\n    }\n  ]\n}\n[/block]\nIt can be included and then used with the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"julia = require('node-julia');\\n\\njulia.exec('include','mandel.jl');\\njulia.exec('mandel',0.4,function(err,res) {\\n   if(!err) console.log(\\\"mandel of 0.4: \\\",res);\\n});\\n\\nsetTimeout(function(){ console.log(\\\"done.\\\"); },1000);\",\n      \"language\": \"javascript\",\n      \"name\": \"mandel.js\"\n    }\n  ]\n}\n[/block]\nLike above, this can be executed with or without a function callback\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"julia = require('node-julia');\\n\\njulia.exec('include','mandel.jl');\\nconsole.log(\\\"mandel of 0.4: \\\" + julia.exec('mandel',0.4));\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\n# Alternate call styles\nBoth eval and exec support a call style that does not require a function callback. \n\nThe style to use is determined by the type of the last argument; if it is a function, then the style is assumed to be callback-style, otherwise results are returned directly.   The two styles can be mixed within the same program.  The style determines if the call is [synchronous or asynchronous](doc:synchronous-vs-asynchronous).\n\n# Multiple value return\nJulia functions can return multiple values as a tuple.  In the function callback style, these are bound to multiple variables.  For example, the Julia function **svd** performs singular value decomposition on an input matrix, and return 2 matrices **u**, **v** and a vector **s**.  The singular value decomposition of a random 6x6 matrix can be obtained using the following:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require(\\\"node-julia\\\");\\n\\nvar a = julia.exec('rand',6,6);\\n\\njulia.exec('svd',a,function(err,u,s,v) {\\n   if(!err) {\\n      console.log(\\\"u matrix: \\\", u);\\n      console.log(\\\"s vector: \\\", s);\\n      console.log(\\\"v matrix: \\\", v);\\n   }\\n});\\n\\nsetTimeout(function(){ console.log(\\\"done.\\\"); },1000);\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]\nThe alternate style syntax returns multiple values in an array; the following is equivalent:\n[block:code]\n{\n  \"codes\": [\n    {\n      \"code\": \"var julia = require(\\\"node-julia\\\");\\n\\nvar a = julia.exec('rand',6,6);\\nvar res = julia.exec('svd',a);\\n\\nconsole.log(\\\"u matrix: \\\", res[0]);\\nconsole.log(\\\"s vector: \\\", res[1]);\\nconsole.log(\\\"v matrix: \\\", res[2]);\\n\",\n      \"language\": \"javascript\"\n    }\n  ]\n}\n[/block]","excerpt":"","slug":"eval-and-exec","type":"basic","title":"Eval And Exec"}
# Julia can be used from Javascript directly using eval or exec. ## eval allows Julia source to be embedded in Javascript as a string The function **eval** takes a string and evaluates that string as if it were typed in the Julia REPL. The following computes the sum of the first 100 integers [block:code] { "codes": [ { "code": "julia = require('node-julia');\n\nres = julia.eval('sum(Int64[n for n=1:100])');\nconsole.log(\"sum of first 100 integers: \",res);", "language": "javascript", "name": null } ] } [/block] The result of the computation is returned as the function result. Alternatively there is a form where the results are returned as variables bound to arguments in a callback function. [block:code] { "codes": [ { "code": "julia = require('node-julia');\n\njulia.eval('sum(Int64[n for n=1:100])',function(err,res) {\n if(err) console.log(\"error in computation \",err);\n else console.log(\"sum of first 100 integers: \",res);\n});\n\nsetTimeout(function(){ console.log('done.'); },1000);", "language": "javascript", "name": "sum.js" } ] } [/block] ## exec allows predefined Julia functions to be invoked by name The function **exec** takes a string as the name of a function which may be module qualified, and any number of additional arguments that will be passed to that function. [block:code] { "codes": [ { "code": "julia = require('node-julia');\n\nvar a = julia.eval('rand(1:20000,1000)');\nvar median = julia.exec('Base.median',a);\n\nconsole.log(\"median of 1000 random integers between 1 and 20000: \",median);", "language": "javascript", "name": "mean.js" } ] } [/block] ## Or Julia source code can be placed in an external file and then included For example, using the definition of **mandel** located on [Julia Lang](http://julialang.org) is put in a file mandel.jl [block:code] { "codes": [ { "code": "function mandel(z)\n c = z\n maxiter = 80\n for n = 1:maxiter\n if abs(z) > 2\n return n-1\n end\n z = z^2 + c\n end\n return maxiter\nend", "language": "julia", "name": "mandel.jl" } ] } [/block] It can be included and then used with the following: [block:code] { "codes": [ { "code": "julia = require('node-julia');\n\njulia.exec('include','mandel.jl');\njulia.exec('mandel',0.4,function(err,res) {\n if(!err) console.log(\"mandel of 0.4: \",res);\n});\n\nsetTimeout(function(){ console.log(\"done.\"); },1000);", "language": "javascript", "name": "mandel.js" } ] } [/block] Like above, this can be executed with or without a function callback [block:code] { "codes": [ { "code": "julia = require('node-julia');\n\njulia.exec('include','mandel.jl');\nconsole.log(\"mandel of 0.4: \" + julia.exec('mandel',0.4));", "language": "javascript" } ] } [/block] # Alternate call styles Both eval and exec support a call style that does not require a function callback. The style to use is determined by the type of the last argument; if it is a function, then the style is assumed to be callback-style, otherwise results are returned directly. The two styles can be mixed within the same program. The style determines if the call is [synchronous or asynchronous](doc:synchronous-vs-asynchronous). # Multiple value return Julia functions can return multiple values as a tuple. In the function callback style, these are bound to multiple variables. For example, the Julia function **svd** performs singular value decomposition on an input matrix, and return 2 matrices **u**, **v** and a vector **s**. The singular value decomposition of a random 6x6 matrix can be obtained using the following: [block:code] { "codes": [ { "code": "var julia = require(\"node-julia\");\n\nvar a = julia.exec('rand',6,6);\n\njulia.exec('svd',a,function(err,u,s,v) {\n if(!err) {\n console.log(\"u matrix: \", u);\n console.log(\"s vector: \", s);\n console.log(\"v matrix: \", v);\n }\n});\n\nsetTimeout(function(){ console.log(\"done.\"); },1000);", "language": "javascript" } ] } [/block] The alternate style syntax returns multiple values in an array; the following is equivalent: [block:code] { "codes": [ { "code": "var julia = require(\"node-julia\");\n\nvar a = julia.exec('rand',6,6);\nvar res = julia.exec('svd',a);\n\nconsole.log(\"u matrix: \", res[0]);\nconsole.log(\"s vector: \", res[1]);\nconsole.log(\"v matrix: \", res[2]);\n", "language": "javascript" } ] } [/block]