-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.js
90 lines (74 loc) · 1.84 KB
/
support.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
88
89
90
function inspect(x) {
return (typeof x === 'function') ? inspectFn(x) : inspectArgs(x);
}
function inspectFn(f) {
return (f.name) ? f.name : f.toString();
}
function inspectArgs(args) {
return args.reduce(function(acc, x){
return acc += inspect(x);
}, '(') + ')';
}
function curry(fx) {
var arity = fx.length;
return function f1() {
var args = Array.prototype.slice.call(arguments, 0);
if (args.length >= arity) {
return fx.apply(null, args);
}
else {
var f2 = function f2() {
var args2 = Array.prototype.slice.call(arguments, 0);
return f1.apply(null, args.concat(args2));
}
f2.toString = function() {
return inspectFn(fx) + inspectArgs(args);
}
return f2;
}
};
}
compose = function() {
var fns = toArray(arguments),
arglen = fns.length;
return function(){
for(var i=arglen;--i>=0;) {
var fn = fns[i]
, args = fn.length ? Array.prototype.slice.call(arguments, 0, fn.length) : arguments
, next_args = Array.prototype.slice.call(arguments, (fn.length || 1)); //not right with *args
next_args.unshift(fn.apply(this,args));
arguments = next_args;
}
return arguments[0];
}
}
add = curry(function(x, y) {
return x + y;
});
match = curry(function(what, x) {
return x.match(what);
});
replace = curry(function(what, replacement, x) {
return x.replace(what, replacement);
});
filter = curry(function(f, xs) {
return xs.filter(f);
});
map = curry(function map(f, xs) {
return xs.map(f);
});
reduce = curry(function(f, a, xs) {
return xs.reduce(f, a);
});
split = curry(function(what, x) {
return x.split(what);
});
join = curry(function(what, x) {
return x.join(what);
});
toUpperCase = function(x) {
return x.toUpperCase()
};
toLowerCase = function(x) {
return x.toLowerCase()
};