-
Notifications
You must be signed in to change notification settings - Fork 0
/
interpret.js
87 lines (82 loc) · 2.06 KB
/
interpret.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
87
/* UMD.define */ (typeof define=="function"&&define||function(d,f,m){m={module:module,require:require};module.exports=f.apply(null,d.map(function(n){return m[n]||require(n)}))})
([], function(){
"use strict";
//TODO: add asserts
// code should be a function,
// value-taking methods should have physical values supplied
function interpret(pipe, array){
var stages = pipe.stages, len = stages.length, temp;
for(var i = 0; i < len; ++i){
var stage = stages[i], f = stage.code;
switch(stage.type){
case "forEach":
array.forEach(f);
break;
case "transform":
array.forEach(function(value, index, array){
array[index] = f(value, index);
});
break;
case "map":
array = array.map(f);
break;
case "filter":
array = array.filter(f);
break;
case "indexOf":
return array.indexOf(stage.value);
case "every":
return array.every(f);
case "some":
return array.some(f);
case "fold":
return array.reduce(f, stage.value);
case "scan":
temp = stage.value; // accumulator
array = array.map(function(value, index){
return temp = f(temp, value, index);
});
temp = null;
break;
case "skip":
temp = stage.value;
array = array.filter(function(_, index){
return index >= temp;
});
break;
case "take":
temp = stage.value;
array = array.filter(function(_, index){
return index < temp;
});
break;
case "skipWhile":
temp = false;
array = array.filter(function(value, index){
return temp || (temp = !f(value, index));
});
break;
case "takeWhile":
temp = true;
array = array.filter(function(value, index){
return temp && (temp = f(value, index));
});
break;
case "voidResult":
return;
case "find":
return array.find(f);
case "findIndex":
return array.findIndex(f);
}
}
return array;
}
function curry(pipe){
return function(array){
return interpret(pipe, array);
};
}
interpret.curry = curry;
return interpret;
});