-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhello_embed.lua
More file actions
32 lines (26 loc) · 1.14 KB
/
Copy pathhello_embed.lua
File metadata and controls
32 lines (26 loc) · 1.14 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
-- hello_embed.lua — the smallest useful embedding of Shen in a Lua program.
--
-- luajit examples/hello_embed.lua
--
-- Everything the `shen` module offers in ~25 lines: boot, define a typed
-- Shen function from a string, call it from Lua, pass data both ways.
package.path = "./?.lua;" .. package.path -- run from the repo root
local shen = require("shen")
shen.boot{quiet = true} -- warm boot is ~30 ms (bytecode cache)
-- Define a Shen function at runtime, with the typechecker on
-- (sum/length come from the 41.2 stlib).
shen.eval([[
(tc +)
(define mean
{(list number) --> number}
Xs -> (/ (sum Xs) (length Xs)))
(tc -)
]])
-- Call Shen from Lua. shen.list marshals a Lua array to a cons list.
print("mean:", shen.call("mean", shen.list({3, 4, 5, 6}))) --> 4.5
-- shen.fn gives a plain Lua callable; partial application works too.
local mean = shen.fn("mean")
print("mean:", mean(shen.list({1, 2}))) --> 1.5
-- And back: cons lists marshal to Lua arrays.
local arr = shen.totable(shen.eval("(map (* 2) [1 2 3])"))
print("doubled:", table.concat(arr, ", ")) --> 2, 4, 6