{"metadata":{"image":[],"title":"","description":""},"api":{"url":"","auth":"never","results":{"codes":[]},"params":[]},"next":{"description":"","pages":[]},"title":"Eval And Exec","type":"basic","slug":"eval-and-exec","excerpt":"","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]","updates":[],"order":1,"isReference":false,"hidden":false,"sync_unique":"","link_url":"","link_external":false,"_id":"566e55c12c1e760d0030c56f","user":"544083bee239230800071bef","__v":0,"githubsync":"","project":"54408e54309354080070a896","version":{"version":"1.2.3","version_clean":"1.2.3","codename":"","is_stable":true,"is_beta":false,"is_hidden":false,"is_deprecated":false,"categories":["566e55c12c1e760d0030c56d"],"_id":"566e55c02c1e760d0030c56c","createdAt":"2015-12-14T05:38:08.400Z","releaseDate":"2015-12-14T05:38:08.400Z","project":"54408e54309354080070a896","__v":1},"createdAt":"2014-10-17T03:34:44.928Z","category":{"sync":{"isSync":false,"url":""},"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"],"title":"Documentation","slug":"documentation","order":0,"from_sync":false,"reference":false,"_id":"566e55c12c1e760d0030c56d","__v":2,"createdAt":"2014-10-17T03:34:44.886Z","project":"54408e54309354080070a896","version":"566e55c02c1e760d0030c56c"}}