-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdlib.lang
59 lines (48 loc) · 1.07 KB
/
stdlib.lang
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
let identity = fn(x: T) { x }
let and = fn (left: bool, right: bool) {
if(eq(left, true), {
if(eq(right, true), true, false)
}, {
false
})
}
let or = fn (left: bool, right: bool) {
if(eq(left, true), {
true
}, {
if(eq(right, true), true, false)
})
}
let map = fn (coll: list[T], func: (T) -> R) {
let i = 0
let result: list[R] = []
while(lt(i, lsize(coll)), {
result = push(result, func(lget(coll, i)))
i = add(i, 1)
})
result
}
let filter = fn (coll: list[T], func: (T) -> bool) {
let i = 0
let result: list[T] = []
while(lt(i, lsize(coll)), {
let item = lget(coll, i)
if(func(item), {
result = push(result, item)
nil
}, nil)
i = add(i, 1)
})
result
}
let pairs = fn (coll: map[K, V]) {
let i = 0
let ks = keys(coll)
let result: list[tup[K, V]] = []
while(lt(i, lsize(ks)), {
let key = lget(ks, i)
result = push(result, (key, mget(coll, key)))
i = add(i, 1)
})
result
}