13 ### less braces. Use indent
17 for(i <- 0..list.length)
18 if(list[i] > m) m = list[i]
21 // trace means "console.log" with last argument returned.
22 trace max [5, 4, 3, 2, 1]
32 inventory: [food, food, potion, bomb]
41 ### Currying, and `def` wrappers
44 def rec(x)(y) = g(x(x)) y
45 rec(rec) // ...means this value will be returned.
47 // Define recursive function using Y
48 // fibonacci = Y(functon(recurse){return function(n){...}})
49 def Y fibonacci(recurse)(n) =
51 else recurse(n - 2) + recurse(n - 1)
55 ### Simple "class" definition
58 def Man::speak(something):
61 def outof(Man) Child(name):
62 resend Man(name) // Man.call this, name
63 def Child::speak(something):
65 resend Man::speak(something) // Man.prototype.speak.call this, something
67 var tom = new Child "Tom"
70 ### Monadic transformation for async / enumerator / list comprehension......
73 def asyncLib = require "moe/libs/async"
74 def async = asyncLib.async
75 def sleep = asyncLib.sleep // sleep(f, dt) === setTimrout(dt, f)
77 def f() = process wait loadResource(something)
78 // def f = [build: fBuild]
79 // where fBuild(schemata)()():
80 // return schemata.yield loadResource something, (resource) :>
81 // return schemata.return process resource
83 def async randPrintNums(n):
87 sleep! (100 * Math.random())
96 def enumeration String::getEnumerator():
97 for(i <- 0..this.length)
98 enumeration.yield! this.charAt(i), i
100 for(c <- "this is a string")
105 -- Enumerator comprehension monad
106 var ecSchemata = object defaultMonadSchemata, =>
107 def Enumerable @return(x):
110 def @bindYield(f, thisp, args) = f.apply thisp, arguments.slice(2)
111 def Enumerable @bind(list, callback):
117 var f = G.build ecSchemata
118 f.apply(this, arguments)()
121 for(item <- mktable {var x <- (1..100); x * 2 + 1}) trace item
124 var t = mktable {var x <- (1...9); var y <- (x...9); x + ' * ' + y + ' = ' + x * y }
125 // t = mktable [build: fBuild]
126 // where fBuild(schemata)()() =
127 // schemata.bind (1...9), (x) =>
128 // schemata.bind (x...9), (y) =>
129 // schemata.return (x + ' * ' + y + ' = ' + x * y)
130 for(item <- t) trace item