-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexamples.lamj
More file actions
311 lines (250 loc) · 5.71 KB
/
examples.lamj
File metadata and controls
311 lines (250 loc) · 5.71 KB
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
# start examples
# javascript modules
assert = require "assert"
assertEqual = actual: expected: (curry assert.equal) actual expected _
"string"
123
x=
:
1
x
xyz= :2
id=
param:param
id "value"
one=
"too":33
too=
:"too"
one (too null)
intMap=
1:2
3:4
x:10
intMap 123
string=
:"this is a string!"
stringMap=
"key":4
stringMap "key"
y=
"z":6
"v": x:x
"w": "arg":8
y "z"
y "w"
p=
:9
p
add 2
subtract 1
subtract 2 1
rec=
1: 1
x: rec (subtract x 1)
fib=
{x: lessThan x 0 }: _
0:0
1:1
n: add (fib (subtract n 1)) (fib (subtract n 2))
B assertEqual fib 0 0
B assertEqual fib 1 1
B assertEqual fib 2 1
B assertEqual fib 3 2
B assertEqual fib 4 3
B assertEqual fib 5 5
B assertEqual fib 6 8
B assertEqual fib 9 34
subtract 2 1
w= :4
x= w: 3
z= x: w: 2
y= z: x: w: 1
y "z" "x" "w"
y z x w
y
# a comment
cons 1 _
cons 1 (cons 2 _)
head (cons 1 _)
addTwo=
n: add n 2
l= : cons 1 (cons 2 _)
x= : map addTwo (l _)
assertEqual (head (x _)) 3
# null/nothing
_
ifThenElse=
{test:test}: then: otherwise: then
x: then: otherwise: otherwise
# multiple argument with pattern matching
multiExpr=
x:
t= :add t 1
p= :subtract p 3
compose t p x
multiply 10.123 (add 2 3.3)
multi=
0:
1: "a"
n: "b"
x:
y: "c"
complexMatch=
{ x: subtract x 2 }: 0
x: x
ltt= x: lessThan x 2
filter ltt (cons 1 (cons 2 _))
last (cons 1 _)
any ltt (cons 1 (cons 2 _))
any ltt (cons 2 (cons 2 _))
assert (not (all ltt (cons 1 (cons 2 _))))
assert (all ltt (cons 1 (cons 1 _)))
assert (equal 1 1)
assertEqual (add 1 1) 2
assertEqual (add "ab" "cd") "abcd"
assert (equal (add "ab" "cd") "abcd")
exports myFunc= :"nothing"
assertEqual (head (append (cons 1 _) (cons 2 _))) 1
assertEqual (head (tail (append (cons 1 _) (cons 2 _)))) 2
listAddOne= x: cons (add x 1) _
assertEqual (head (listAddOne 1)) 2
assertEqual (head (listM_unit 1)) 1
assertEqual (head (listM_unit 1)) (head (cons 1 _))
compose append map listAddOne
append (map listAddOne)
adder = l: compose (listM_bind listAddOne) (listM_bind listAddOne) l
adder (listM_unit 1)
assertEqual (head (listM_bind listAddOne (listM_unit 1))) 2
assertEqual (head (listM_bind listAddOne (listM_bind listAddOne (listM_unit 1)))) 3
assertEqual (head (compose (listM_bind listAddOne) (listM_bind listAddOne) (listM_unit 1))) 3
assertEqual (head (adder (listM_unit 1))) 3
assertEqual (nothing "isJust") false
assertEqual (just 1 "isJust") true
addOne = x: add x 1
assertEqual (maybeM_unit 1 "isJust") true
assertEqual (maybeM_unit 1 "fromJust") 1
assertEqual (maybeM_bind addOne (maybeM_unit 1) "fromJust") 2
assertEqual (maybeM_bind addOne (maybeM_bind addOne (maybeM_unit 1)) "fromJust") 3
assertEqual (compose (maybeM_bind addOne) (maybeM_bind addOne) (maybeM_unit 1) "fromJust") 3
assertEqual (compose (maybeM_bind addOne) (maybeM_bind addOne) (nothing) "isJust") false
isTrue=
true: true
false: false
x: "other"
assertEqual (isTrue true) true
assertEqual (isTrue false) false
assertEqual (isTrue "true") "other"
assertEqual (isTrue "false") "other"
assertEqual (isTrue _) "other"
truthy=
_: false
{x: x}: true
assertEqual (truthy true) true
assertEqual (truthy "a") true
assertEqual (truthy (cons 1 _)) true
assertEqual (truthy " ") true
assertEqual (truthy false) false
assertEqual (truthy _) false
assertEqual (truthy "") false
assertEqual (toInt "1") 1
B assertEqual toInt "1" 1
assertEqual (toFloat "1.1") 1.1
assertEqual (toBool "true") true
assertEqual (toBool "True") true
assertEqual (toBool "false") false
assertEqual (toBool "other stuff") false
# assignment with a lambda, always wraps in a function,
# so _ to unwrap
# (ie apply that func with nothing)
# ie call with no arguments
curried= : compose (add 1) (add 1)
assertEqual (curried _ 2) 4
# assignment without a lambda
curried= compose (add 1) (add 1)
assertEqual (curried 2) 4
# using combinator
curried= B (add 1) (add 1)
B assertEqual curried 2 4
assertEqual (or 1 2) 1
B1 assertEqual or 1 2 1
assertEqual (or 1 _) 1
assertEqual (or _ 2) 2
assertEqual (or _ _) _
assertEqual (and 1 2) true
assertEqual (and _ 2) false
assertEqual (and 1 _) false
assertEqual (and _ _) false
assertEqual (not 1) false
assertEqual (not _) true
# some Math
assertEqual (min 1 2) 1
assertEqual (max 1 2) 2
assertEqual (pow 2 4) 16
# try method calls on js object
# use . to trick lamj into thinking s.length is a variable
# only works on variable names
s = "string"
l = s.length
# try out the combinators
# just like B, but always returns _
nil=
"toString":"nil"
:nil
nil nil nil "something"
uncurriedAdd = uncurry x: y: add x y
# add key to 'map'
one =
"a": 1
"b": 2
B assertEqual one "a" 1
two =
"c": 3
x: one x
B assertEqual two "a" 1
B assertEqual two "c" 3
# remove key from 'map'
three =
"b": _
x: two x
B assertEqual three "b" _
addKey =
map:
key:
value:
{x: equal x key}: value
x: map x
removeKey =
map:
key:
{x: equal x key}: _
x: map x
two = addKey one "c" 3
B assertEqual two "a" 1
B assertEqual two "c" 3
three = removeKey two "b"
B assertEqual three "b" _
assertEqual (head (one "keys")) "a"
assertEqual (head (tail (one "keys"))) "b"
assertEqual (head (keys one)) "a"
assertEqual (toString 1) "1"
assertEqual (head (values one)) 1
assertEqual (head (tail (values one))) 2
# build a list without nested cons
list1 = x: cons x _
list2 = x: y: cons x (list1 y)
list3 = x: y: z: cons x (list2 y z)
list4 = x: y: z: w: cons x (list3 y z w)
list4 1 2 3 4
# recursive building of a list using cons
#list =
# _:_
# x: y: z: cons x (list y) z
#list 1 2 _
# simple eval of string to function
x = "toString"
y = `x 1
assertEqual (concat (list3 "hello" " " "world")) "hello world"
# calling js functions
console.log (concat (list3 "hello" " " "world"))
# last line can now be a comment