-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathglobalEnv.js
66 lines (65 loc) · 1.98 KB
/
globalEnv.js
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
const globalEnv = {
"+": (...args) => args.reduce((acc, it) => acc + it, 0),
"-": (...args) => args.slice(1).reduce((acc, it) => acc - it, args[0]),
"*": (...args) => args.reduce((acc, it) => acc * it, 1),
"/": (...args) => args.slice(1).reduce((acc, it) => acc / it, args[0]),
expt: (...args) => {
if (args.length !== 2) throw new Error("'expt'should get two arguments")
return args[0] ** args[1]
},
"=": (...args) => {
for (let i = 1; i < args.length; i++) {
if (args[i - 1] !== args[i]) return false
}
return true
},
"equal?": (x, y) => x === y,
print: (x) => x,
"<": (...args) => {
for (let i = 1; i < args.length; i++) {
if (args[i - 1] >= args[i]) return false
}
return true
},
">": (...args) => {
for (let i = 1; i < args.length; i++) {
if (args[i - 1] <= args[i]) return false
}
return true
},
"<=": (...args) => {
for (let i = 1; i < args.length; i++) {
if (args[i - 1] > args[i]) return false
}
return true
},
">=": (...args) => {
for (let i = 1; i < args.length; i++) {
if (args[i - 1] < args[i]) return false
}
return true
},
list: (...args) => args,
length: (list) => list.length,
min: (...args) =>
args.reduce((acc, it) => (it < acc ? it : acc), Number.POSITIVE_INFINITY),
max: (...args) =>
args.reduce((acc, it) => (it > acc ? it : acc), Number.NEGATIVE_INFINITY),
sum: (...args) => args.reduce((acc, it) => acc + it, 0),
product: (...args) => args.reduce((acc, it) => acc * it, 1),
abs: (x) => Math.abs(x),
round: (x) => Math.round(x),
floor: (x) => Math.floor(x),
ceil: (x) => Math.ceil(x),
not: (x) => !x,
pi: Math.PI,
head: (list) => list[0],
tail: (list) => list.slice(1),
cons: (head, tail) => [head, ...(Array.isArray(tail) ? tail : [tail])],
map: (mapper, arr) => arr.map(mapper),
isNumber: (x) => typeof x === "number",
isSymbol: function (x) {
return typeof x === "string" && x in this
},
}
module.exports = globalEnv