-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathexamples.tbd
82 lines (63 loc) · 1.4 KB
/
examples.tbd
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
; # Example Syntax
let [foo "bar" ; no commas
bar "foo"
done? true
bang! "bang"
this-*<>thing' "foo"
f x => console.log(x) {
inc x => {
; the last statement is implicitly returned
x + 1 ; this is a comment
}
some-symbol :foo ; uses Symbol.for("foo") under the hood
] {
}
def foo "bar"
defn foo fn(x y {
plus(x y)
})
defn plus [...args] {
reduce([acc curr] acc + curr, args, 0)
}
; Examples of single-arity function declarations
defn foo [] {
console.log('something');
}
defn inc [x] {
plus(x 1)
}
; Multi-arity function declaration
defn map
[f] {
map(f [])
}
[f coll] {
; perform map
}
let x = :foo;
let y = "bar";
return [x, y];
fn(x y ~{
[x y
{x y}
1 2 #{3 4 5}]
})(:foo "bar")
(defn map [f coll]
(if (empty? coll)
coll
(cons (f (first coll)) (map f (rest coll)))))
def(= fn(a b ~{invoke-operator("=" a b)}))
def(zero? fn(a ~{= a 0}))
def(empty fn(coll ~{zero?(.-length(coll))}))
def(first fn(coll ~{.-0(coll)}))
def(rest fn(coll ~{.slice(coll 1)}))
def(cons fn(el coll ~{[el ...coll]}))
def(log fn(...args ~{.log(console ...args)}))
def(plus fn(a b ~{invoke-operator("+" a b)}))
def(inc fn(a ~{plus a 1}))
log(inc(1))
def(map fn(f coll ~{if(empty(coll)
~{[]}
~{let([el first(coll)]
~{cons(f(el) map(f rest(coll)))})})}))
log(map(inc [1 2 3]))