-
Notifications
You must be signed in to change notification settings - Fork 1
/
core.berlin
641 lines (549 loc) · 14 KB
/
core.berlin
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
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
def(lib require("./lib"))
def(_ require("lodash"))
def(safe-symbol .-safe_symbol(require("./translator")))
def(= _.isEqual)
def(identical?! fn(a b invoke-operator("===" a b)))
def(identity! fn!(a a))
def(coll? fn(a
or(map?(a)
array?(a)
set?(a))))
def(primitive? fn(a not(coll?(a))))
def(type fn(a
reduce(fn(acc curr
if(empty?(acc)
let([f first(curr)
result second(curr)]
if(f(a)
result
nil))
acc))
nil
~{string? "string"
nil? "nil"
undefined? "undefined"
boolean? "boolean"
keyword? "keyword"
number? "number"
array? "array"
map? "map"
set? "set"})))
def(or fn(...args
if(empty?(args)
nil
if(identical?!(count(args) 1)
first(args)
if(identical?!(count(args) 2)
if(truthy?(first(args))
first(args)
last(args))
if(truthy?(first(args))
first(args)
apply(or rest(args))))))))
def(and fn(...args
if(empty?(args)
true
if(truthy?(first(args))
if(identical?!(count(args) 1)
first(args)
if(identical?!(count(args) 2)
last(args)
apply(and rest(args))))
first(args)))))
def(zero? fn(a identical?!(a 0)))
; Do not use fn "or" in here as it
; will lead to a circular dependency
def(count fn(coll
if(array?(coll)
.-length(coll)
if(map?(coll)
.-size(coll)
if(set?(coll)
.-size(coll)
if(nil?(coll)
0
if(string?(coll)
.-length(coll)
raise("count only works for arrays, maps, sets, strings, and nil"))))))))
def(empty? fn(coll
if(nil?(coll)
true
zero?(count(coll)))))
def(->?nil fn(n if(undefined?(n) nil n)))
def(first fn(coll ->?nil(.-0(arr(coll)))))
def(ffirst fn(coll first(first(coll))))
def(second fn(coll ->?nil(.-1(arr(coll)))))
def(third fn(coll ->?nil(.-2(arr(coll)))))
def(last fn(coll
if(next(coll)
last(rest(coll))
first(coll))))
def(butlast fn(coll
if(next(coll)
cons(first(coll) butlast(rest(coll)))
nil)))
def(rest fn(coll
if(nil?(coll)
[]
.slice(arr(coll) 1))))
def(next fn(coll
let([r rest(coll)]
if(empty?(r)
nil
r))))
def(cons fn(el coll
if(nil?(coll)
[el]
apply(conj [el] coll))))
def(string? fn(a identical?!(invoke-operator("typeof" a) "string")))
def(number? fn(a identical?!(invoke-operator("typeof" a) "number")))
def(keyword? fn(a identical?!(invoke-operator("typeof" a) "symbol")))
def(boolean? fn(a identical?!(invoke-operator("typeof" a) "boolean")))
def(nil? fn(a identical?!(a nil)))
def(set? fn(a invoke-operator("instanceof" a Set)))
def(map? fn(a invoke-operator("instanceof" a Map)))
def(array? fn(a .isArray(Array a)))
def(arr fn(a
if(nil?(a)
[]
if(string?(a)
.split(a "")
if(set?(a)
.from(Array a)
if(map?(a)
.from(Array a)
if(array?(a)
a
raise("Given item not iterable"))))))))
def(array fn(...args
args))
def(set fn(a
Set.(arr(a))))
def(hash-set fn(...args
Set.(args)))
def(hash-map fn(...kvs
Map.(apply(pairwise kvs))))
def(tuples->map fn(tuples
Map.(tuples)))
def(keys fn(m
->>(m arr [map first])))
def(vals fn(m
->>(m arr [map second])))
def(pairwise fn(...args
if(->(args count even?)
if(empty?(args)
[]
cons([first(args) second(args)] apply(pairwise rest(rest(args)))))
raise("expected an even number of arguments"))))
def(log fn(...args .log(console ...args)))
def(+ fn(...args
if(empty?(args)
0
if(identical?!(count(args) 1)
first(args)
invoke-operator("+" first(args) apply(+ rest(args)))))))
def(- fn(...args
if(empty?(args)
raise("- requires at least one argument")
if(identical?!(count(args) 1)
invoke-operator("-" 0 first(args))
invoke-operator("-" first(args) apply(+ rest(args)))))))
def(* fn(...args
if(empty?(args)
1
if(identical?!(count(args) 1)
first(args)
invoke-operator("*" first(args) apply(* rest(args)))))))
def(/ fn(...args
if(empty?(args)
raise("/ requires at least one argument")
if(identical?!(count(args) 1)
invoke-operator("/" 1 first(args))
invoke-operator("/" first(args) apply(* rest(args)))))))
def(% fn!(a b invoke-operator("%" a b)))
; How can I make the following comparing
; fns less repetitive?
def(< fn(...args
if(empty?(args)
raise("< requires at least one argument")
if(identical?!(count(args) 1)
true
if(identical?!(count(args) 2)
invoke-operator("<" first(args) second(args))
and(invoke-operator("<" first(args) second(args))
apply(< second(args) rest(rest(args)))))))))
def(<= fn(...args
if(empty?(args)
raise("<= requires at least one argument")
if(identical?!(count(args) 1)
true
if(identical?!(count(args) 2)
invoke-operator("<=" first(args) second(args))
and(invoke-operator("<=" first(args) second(args))
apply(<= second(args) rest(rest(args)))))))))
def(> fn(...args
if(empty?(args)
raise("> requires at least one argument")
if(identical?!(count(args) 1)
true
if(identical?!(count(args) 2)
invoke-operator(">" first(args) second(args))
and(invoke-operator(">" first(args) second(args))
apply(> second(args) rest(rest(args)))))))))
def(>= fn(...args
if(empty?(args)
raise(">= requires at least one argument")
if(identical?!(count(args) 1)
true
if(identical?!(count(args) 2)
invoke-operator(">=" first(args) second(args))
and(invoke-operator(">=" first(args) second(args))
apply(>= second(args) rest(rest(args)))))))))
def(inc fn(a +(a 1)))
def(dec fn(a -(a 1)))
def(true? fn(a identical?!(a true)))
def(false? fn(a identical?!(a false)))
def(truthy? fn(a
if(false?(a)
false
if(nil?(a)
false
if(identical?!(a undefined)
false
true)))))
def(falsey? fn(a if(truthy?(a)
false
true)))
def(defined? fn(a not(identical?!(a undefined))))
def(undefined? fn(a identical?!(undefined a)))
def(not falsey?)
def(even? fn(a zero?(%(a 2))))
def(odd? fn(a comp(not even?)(a)))
def(apply fn(f ...args
let([additionalArgs arr(butlast(args))
argsArray last(args)
args [...additionalArgs ...argsArray]]
f.apply(nil args))))
def(map fn(f coll
if(empty?(coll)
[]
let([el first(coll)]
cons(f(el) map(f rest(coll)))))))
def(filter fn(f coll
if(empty?(coll)
[]
let([el first(coll)]
if(f(el)
cons(el filter(f rest(coll)))
filter(f rest(coll)))))))
def(remove fn(f coll
filter(complement(f) coll)))
; Takes either [f coll] or [f val coll]
def(reduce fn(...args
if(identical?!(count(args) 2)
reduce(first(args) first(second(args)) rest(second(args)))
let([f first(args)
val second(args)
coll arr(third(args))]
if(empty?(coll)
val
let([item first(coll)
result f(val item)]
reduce(f result rest(coll))))))))
def(reverse fn(coll
if(empty?(coll)
[]
let([r reverse(rest(coll))]
conj(r first(coll))))))
def(comp fn(...fns
let([fns reverse(fns)]
fn(...args
reduce(fn(acc curr curr(acc))
apply(first(fns) args)
rest(fns))))))
def(partial fn(f ...args {
fn(...more apply(f ...args more))}))
def(thread fn(type
fn(val ...exprs
reduce(fn(acc curr
if(array?(curr)
let([f first(curr)
args rest(curr)]
if(identical?!(type "first")
apply(f acc args)
if(identical?!(type "last")
f(...args acc)
raise("Unknown thread type"))))
curr(acc)))
val exprs))))
def(-> thread("first"))
def(->> thread("last"))
def(raise fn(msg invoke-operator("throw" msg)))
def(get fn(m k
let([val if(array?(m)
lib.get(m k)
if(map?(m)
.get(m k)
if(nil?(m)
nil
raise("Can only get from array, map, or nil"))))]
if(defined?(val)
val
nil))))
def(get-in fn(m ks
if(empty?(ks)
m
let([k first(ks)
val get(m k)]
get-in(val rest(ks))))))
def(contains? fn(coll key
if(array?(coll)
<(key count(coll))
if(or(map?(coll) set?(coll))
.has(coll key)
if(nil?(coll)
false
raise("Can only use contain? on arrays, maps, sets, and nil"))))))
def(every? fn(pred coll
if(empty?(coll)
true
let([coll arr(coll)]
and(->(coll first pred)
every?(pred rest(coll)))))))
def(conj fn(coll ...vals
if(array?(coll)
[...coll ...vals]
if(set?(coll)
Set.([...coll ...vals])
if(map?(coll)
if(every? (fn(v and(array?(v) identical?!(count(v) 2))) vals)
Map.([...coll ...vals])
raise("Values conj'ed onto map must be two-valued arrays"))
if(nil?(coll)
[...vals]
raise("Can only conj onto arrays, maps, sets, and nil")))))))
def(disj fn(s v ...vs
if(nil?(s)
nil
if(set?(s)
let([result ->>(s arr [remove partial(= v)] set)]
if(empty?(vs)
result
apply(disj result first(vs) rest(vs))))
raise("disj expects a set or nil for the first argument")))))
def(drop fn(n coll
if(or(empty?(coll) <=(n 0))
coll
drop(dec(n) rest(coll)))))
def(take fn(n coll
if(or(empty?(coll) <=(n 0))
[]
cons(first(coll) take(dec(n) rest(coll))))))
def(flatten-1 fn(coll
if(array?(coll)
if(empty?(coll)
coll
let([curr first(coll)]
if(array?(curr)
apply(conj curr flatten-1(rest(coll)))
cons(curr flatten-1(rest(coll))))))
[])))
def(assoc fn(m ...kvs
if(empty?(kvs)
m
if(->(kvs count even?)
let([k first(kvs)
v second(kvs)]
if(array?(m)
if(number?(k)
if(and(<=(k count(m)) >=(k 0))
let([beginning take(k m)
end drop(inc(k) m)
result apply(conj [] ...beginning v end)]
apply(assoc result rest(rest(kvs))))
raise("Index out of bounds"))
raise("Index must be a number"))
if(map?(m)
let([entries ->(m arr flatten-1 [conj k v])
result apply(hash-map entries)]
apply(assoc result rest(rest(kvs))))
if(or(nil?(m) undefined?(m))
apply(assoc ~{} kvs)
raise("Expected one of array, map, or nil")))))
raise("Even number of keys and values required")))))
def(assoc-in fn(m path v
if(empty?(path)
m
if(->(path count [= 1])
assoc(m first(path) v)
let([next-item get(m first(path))
result assoc-in(next-item rest(path) v)]
assoc(m first(path) result))))))
def(dissoc fn(m k ...ks
if(map?(m)
let([result ->>(m arr [remove fn(tuple identical?!(first(tuple) k))] tuples->map)]
if(empty?(ks)
result
apply(dissoc result first(ks) rest(ks))))
if(nil?(m)
nil
raise("Expected a map nor nil for the first argument")))))
def(update fn(m k f ...args
let([v get(m k)
result apply(f v args)]
assoc(m k result))))
def(update-in fn(m path f ...args
if(empty?(path)
m
if(->(path count [= 1])
apply(update m first(path) f args)
let([next-item get(m first(path))
result apply(update-in next-item rest(path) f args)]
assoc(m first(path) result))))))
def(str fn(...args
let([start ""]
if(empty?(args)
start
if(->(args count [= 1])
.concat(start first(args))
.concat(.concat(start first(args)) apply(str rest(args))))))))
def(max fn(...args
if(empty?(args)
raise("max requires at least one argument")
apply(Math.max args))))
def(min fn(...args
if(empty?(args)
raise("min requires at least one argument")
apply(Math.min args))))
def(int? fn(n
Number.isInteger(n)))
def(float? fn(n
and(number?(n)
not(->(n [% 1] zero?)))))
def(neg? fn(n
<(n 0)))
def(pos? fn(n
>(n 0)))
def(rand Math.random)
def(rand-int fn(i
->(i Math.floor [* rand()] Math.floor)))
def(repeat fn(a b
if(>=(a 1)
cons(b repeat(dec(a) b))
[])))
def(repeatedly fn(a f ...args
if(>=(a 1)
cons(apply(f args) apply(repeatedly dec(a) f args))
[])))
def(complement fn(f
fn(...args not(apply(f args)))))
; Export the above functions
def(exp Object.())
map(
fn(curr {
set-property(exp curr eval(safe-symbol(curr)))
set-property(exp safe-symbol(curr) eval(safe-symbol(curr)))})
["="
"identical?!"
"identity!"
"coll?"
"primitive?"
"type"
"or"
"and"
"zero?"
"count"
"empty?"
"first"
"ffirst"
"second"
"third"
"last"
"butlast"
"rest"
"next"
"cons"
"string?"
"number?"
"keyword?"
"boolean?"
"nil?"
"set?"
"map?"
"array?"
"arr"
"array"
"set"
"hash-set"
"hash-map"
"tuples->map"
"keys"
"vals"
"pairwise"
"log"
"+"
"-"
"*"
"/"
"%"
"<"
"<="
">"
">="
"inc"
"dec"
"true?"
"false?"
"truthy?"
"falsey?"
"defined?"
"undefined?"
"not"
"even?"
"odd?"
"apply"
"map"
"filter"
"remove"
"reduce"
"reverse"
"comp"
"partial"
"->"
"->>"
"raise"
"get"
"get-in"
"contains?"
"every?"
"conj"
"disj"
"drop"
"take"
"flatten-1"
"assoc"
"assoc-in"
"dissoc"
"update"
"update-in"
"str"
"max"
"min"
"int?"
"float?"
"neg?"
"pos?"
"rand"
"rand-int"
"repeat"
"repeatedly"
"complement"])
set-property(module "exports" exp)
; how about a feature that lets you assign structures without evaluating them?
; e.g. def~(x [a 1 b console.log("foo")])
; the transpiler would treat this as
; def(x fn([a 1 b console.log("foo")]))
; another idea:
; I could have no map literal and simply have users use the hash-map fn instead.
; Same for arrays and sets, maybe. That would free up a lot of symbols to use for
; the compiler, but on the other hand, it would be a lot more typing for users.