58 lines
No EOL
1.4 KiB
TypeScript
58 lines
No EOL
1.4 KiB
TypeScript
// compose :: (b->c) -> (a->b) -> (a->c)
|
|
function compose<A, B, C>(f: (b: B) => C, g: (a:A) => B): (a:A) => C {
|
|
return function (a:A) : C {
|
|
return f(g.apply(null, a));
|
|
};
|
|
}
|
|
|
|
// forEach :: [a] -> (a -> ()) -> ()
|
|
function forEach<A>(list: A[], f: (a: A, n?: number) => void ): void {
|
|
for (var i = 0; i < list.length; ++i) {
|
|
f(list[i], i);
|
|
}
|
|
}
|
|
|
|
// filter :: (a->bool) -> [a] -> [a]
|
|
function filter<A>(f: (a: A) => boolean, ar: A[]): A[] {
|
|
var ret = [];
|
|
forEach(ar, (el) => {
|
|
if (f(el)) {
|
|
ret.push(el);
|
|
}
|
|
} );
|
|
|
|
return ret;
|
|
}
|
|
|
|
// length :: [a] -> Num
|
|
function length2<A>(ar: A[]): number {
|
|
return ar.length;
|
|
}
|
|
|
|
// curry1 :: ((a,b)->c) -> (a->(b->c))
|
|
function curry1<A, B, C>(f: (a: A, b: B) => C): (ax: A) => (bx: B) => C {
|
|
return function (ay: A) {
|
|
return function (by: B) {
|
|
return f(ay, by);
|
|
};
|
|
};
|
|
}
|
|
|
|
var cfilter = curry1(filter);
|
|
|
|
// compose :: (b->c) -> (a->b) -> (a->c)
|
|
// length :: [a] -> Num
|
|
// cfilter :: {} -> {} -> [{}]
|
|
// pred :: a -> Bool
|
|
// cfilter(pred) :: {} -> [{}]
|
|
// length2 :: [a] -> Num
|
|
// countWhere :: (a -> Bool) -> [a] -> Num
|
|
|
|
function countWhere_1<A>(pred: (a: A) => boolean): (a: A[]) => number {
|
|
return compose(length2, cfilter(pred));
|
|
}
|
|
|
|
function countWhere_2<A>(pred: (a: A) => boolean): (a: A[]) => number {
|
|
var where = cfilter(pred);
|
|
return compose(length2, where);
|
|
} |