Operators
undefined?, null?, true?, false?, zero?, boolean?, number?, string?, object?, array?, function?, empty?,
=, !=, !, >, <, <=, >=, +, -, *, /, %, &&, ||.
Note: =
and !=
work like ===
and !==
in Javascript.
Statements
chain
(chain (function expression) (.key1 args1 ...) (.key2 args2 ...) ...)
chain is the method chaining macro. Consider the JavaScript below.
$("#xyz").required().alphanum().min(3).max(30).with("email");
The same JavaScript in Elfenben using the method chaining macro.
(chain ($ "#xyz") (.required) (.alphanum) (.min 3) (.max 30) (.with "email"))
The node server example using the method chaining macro.
(chain
(require "http")
(.createServer
(function (request response)
(response.writeHead 200 {'Content-Type': 'text/plain'})
(response.end "Hello World\n")))
(.listen 3000 "127.0.0.1"))
do
The do statement evaluates a set of expressions passed as it arguments.
(do (expression1) (expression2) ...)
function
The function statement can be used to create named and anonymous functions. To create a named function do:
(function nameOfFunction ...)
For anonymous functions do:
(function ...)
try
try
takes a set of expressions and evaluates them. The last expression must be a
function, that will be called in case an exception is thrown. The function is called with the error object.
(try (expression1) (expression2) ... (catch function))
Example:
(var fs (require 'fs'))
(var outfile "text.txt")
(try
(fs.writeFileSync outfile "Hello World")
(function (err)
(console.log (+ "Cannot write file " outfile)
(process.exit 1)))
include
Includes a file to be compiled with this compilation unit. It will also search the
'includes' folder for Elfenben modules like 'html.ls'.
(include "string filename")
throw
Will throw an error. (expression) can be string or object or an expression
(throw (expression))
first
Returns the first element of the given array.
(first (array))
rest
Returns the remaining elements in the array after the first element.
(rest (array))
partial
Takes a function "func" with fewer arguments than expected for function "func"
and returns a function that takes the remaining arguments. When the returned function
is called it calls the original func with the previously supplied "args"" plus whatever
is supplied during the function call.
(partial func args)
pipe
Takes any number of functions as arguments and executes each function in the given order
and passes the result to the next function as the first argument.
(pipe ...args)
reduce
reduce
takes an array, an iterator function and an optional initial value.
The iterator is called with the following paramaters, previous value returned by
the last call to iterator, current value of array element, array index, and array.
First time around the initial value is used for previous value. If no initial value
is given, the previous value is set to the first element of the array and the
iteration starts from the second element. The last value returned by the iterator
is returned by reduce
.
(reduce array (iterator) [initial])
Example:
(reduce [1,2,3,4]
(function (accum val i list)
(+ accum val))))
The above call to reduce will return 10.
map
map
takes an array, an iterator function and an optional context. The iterator is
called for each element of the array with element value, index, array. The
returned value from the iterator for each call is pushed into a new array.
`map` returns the new array. If context is provided then "this" inside the iterator
function will be the context object.
(map array (iterator) [(context)])
filter
filter
takes an array and an iterator function that returns a boolean value.
The iterator is called for each element in the array and returns true or false
depending on the specified conditions.filter
returns a new array with elements
that caused the iterator function to return true.
(filter array (iterator))
for
In Elfenben for is a list comprehension expression. for
does not work like the JavaScript "for" statement.
(for
(a [1,2,3]
b [3,4,5])
(+ a b))
;; ==> [ 4, 5, 6, 5, 6, 7, 6, 7, 8 ]
;; we only want results that are <= 6 below
(for
(a [1,2,3]
b [3,4,5])
(when (<= (+ a b) 6)
(+ a b)))
;; ==> [ 4, 5, 6, 5, 6, 6 ]
(for
(letters ["a", "b", "c"]
numbers [3, 4, 5])
[letters, numbers])
;; ==> [['a',3],['a',4],['a',5],['b',3],['b',4],['b',5],['c',3],['c',4],['c',5]]
The bindings for a and b are made for every element of the corresponding arrays. In
the second example we applied a when condition to the result expression to keep
out results greater than six.