-
Notifications
You must be signed in to change notification settings - Fork 1
/
eloquent.js
86 lines (64 loc) · 1.56 KB
/
eloquent.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
const fn = 0;
// add for spread
function print(...val) {
let output = val.length == 1 ?
val[0] :
val.toString().replace(",", " ");
console.log(output);
}
let obj = {a:1, b:2};
print(1);
// function sum(vals) {
// // add if vals[0] is a number
// let total = 0;
// for (let val of vals) {
// total += val;
// }
// return total;
// }
function prototypes() {
Array.prototype.sum = function(start=0, end=this.length) {
if (typeof this[0] !== 'number' || end >= this.length) return;
let total = 0;
for (let i = start; i <= end; i++) total += this[i];
return total;
};
}
function range(start, end, step=1) {
let vals = [];
for(let i = start; i <= end; i += step) {
vals.push(i);
}
return vals;
}
// function* range(start, end, step = 1) {
// let current = start;
// while (current < end) {
// yield current;
// current += step;
// }
// }
// see if that dumb lambda can be cleaned up
function repeat(n, action, args=undefined) {
for (let i = 0; i < n; i++) {
action(args);
}
}
//repeat(5, print, "YO")
// reusable lambda?
function unless(test, then) {
if (!test) return then();
}
// unless(1 == 0, () => {
// print("yo");
// })
function loop(spec, action) {
for(let i = spec[0]; i < spec[1]; i += spec[2]) {
action();
}
}
//loop([0, 10, 1], fn => print("hello world"))
const when = (condition, fn) => {
if (condition) fn();
};
export {print, range, repeat, unless, loop, when, prototypes};