UNPKG

1.83 kBPlain TextView Raw
1;; Some example code snippets
2
3(var square
4 (function (n)
5 (* n n)))
6(console.log (square 10))
7
8(try
9 (console.log "In try")
10 (throw "In catch")
11 (function (err)
12 (console.log err)))
13
14(if (undefined? window)
15 (console.log "Not Running on browser")
16 (console.log "Running on browser"))
17
18(var arr [1, 2, 3, 4, 5])
19(console.log (get 2 arr))
20
21(if (!= 1 2)
22 (console.log "Nos are not equal"))
23
24(if (object? console)
25 (console.log "console is an object")
26 (console.log "console is not an object"))
27
28(if (array? console)
29 (console.log "console is an array")
30 (console.log "console is not an array"))
31
32;; The example below shows the dangers of using a macro
33(macro square (x)
34 (* ~x ~x))
35(console.log (square 10))
36;; The code above works fine. Now consider the code below
37(var i 2)
38(console.log (square i++))
39;; Oops you got 6! An embarrassing square of a no. Thats because the macro
40;; expanded to (* i++ i++) which is multiplying 2 and three!
41
42(var _ (require 'underscore'))
43(Array.prototype.forEach.call [1, 2, 3] (function (elem i list) (console.log elem)))
44
45(macro let (args vals rest...)
46 ((function ~args ~rest...) ~@vals))
47
48(let (name email tel) ("John" "john@example.com" "555-555-5556")
49 (console.log name) (console.log email))
50
51(do
52 (console.log "testing do")
53 (console.log "test again"))
54
55(console.log (str "Hello1" " world1"))
56
57(var link
58 (template (data)
59 "<li><a href=" (.href data) ">" (.text data) "</a></li>\n"))
60
61
62(template page (title links)
63
64"<!DOCTYPE html>
65<html>
66<head>
67 <title>" title "</title>
68</head>
69<body>
70<ul class='nav'>"
71
72(reduce links (function (memo elem) (+ memo (link elem))) "")
73
74"</ul>
75</body>
76</html>")
77
78(console.log
79 (page "My Home Page"
80 [{href:"/about", text:"About"},
81 {href:"/products", text:"Products"},
82 {href:"/contact", text:"Contact"}]))
83