|
| 1 | +//// [genericTypeParameterEquivalence2strict.ts] |
| 2 | +// compose :: (b->c) -> (a->b) -> (a->c) |
| 3 | +function compose<A, B, C>(f: (b: B) => C, g: (a:A) => B): (a:A) => C { |
| 4 | + return function (a:A) : C { |
| 5 | + return f(g.apply(null, a)); |
| 6 | + }; |
| 7 | +} |
| 8 | + |
| 9 | +// forEach :: [a] -> (a -> ()) -> () |
| 10 | +function forEach<A>(list: A[], f: (a: A, n?: number) => void ): void { |
| 11 | + for (var i = 0; i < list.length; ++i) { |
| 12 | + f(list[i], i); |
| 13 | + } |
| 14 | +} |
| 15 | + |
| 16 | +// filter :: (a->bool) -> [a] -> [a] |
| 17 | +function filter<A>(f: (a: A) => boolean, ar: A[]): A[] { |
| 18 | + var ret: A[] = []; |
| 19 | + forEach(ar, (el) => { |
| 20 | + if (f(el)) { |
| 21 | + ret.push(el); |
| 22 | + } |
| 23 | + } ); |
| 24 | + |
| 25 | + return ret; |
| 26 | +} |
| 27 | + |
| 28 | +// length :: [a] -> Num |
| 29 | +function length2<A>(ar: A[]): number { |
| 30 | + return ar.length; |
| 31 | +} |
| 32 | + |
| 33 | +// curry1 :: ((a,b)->c) -> (a->(b->c)) |
| 34 | +function curry1<A, B, C>(f: (a: A, b: B) => C): (ax: A) => (bx: B) => C { |
| 35 | + return function (ay: A) { |
| 36 | + return function (by: B) { |
| 37 | + return f(ay, by); |
| 38 | + }; |
| 39 | + }; |
| 40 | +} |
| 41 | + |
| 42 | +var cfilter = curry1(filter); |
| 43 | + |
| 44 | +declare function strBool(str: string): boolean |
| 45 | +const filterer = cfilter(strBool); |
| 46 | +const expectFilterer: (a: string[]) => string[] = filterer; |
| 47 | + |
| 48 | +const filtered = filterer(["hello"]); |
| 49 | +const expectFiltered: string[] = filtered; |
| 50 | + |
| 51 | +// compose :: (b->c) -> (a->b) -> (a->c) |
| 52 | +// length :: [a] -> Num |
| 53 | +// cfilter :: {} -> {} -> [{}] |
| 54 | +// pred :: a -> Bool |
| 55 | +// cfilter(pred) :: {} -> [{}] |
| 56 | +// length2 :: [a] -> Num |
| 57 | +// countWhere :: (a -> Bool) -> [a] -> Num |
| 58 | + |
| 59 | +function countWhere_1<A>(pred: (a: A) => boolean): (a: A[]) => number { |
| 60 | + return compose(length2, cfilter(pred)); |
| 61 | +} |
| 62 | + |
| 63 | +function countWhere_2<A>(pred: (a: A) => boolean): (a: A[]) => number { |
| 64 | + var where = cfilter(pred); |
| 65 | + return compose(length2, where); |
| 66 | +} |
| 67 | + |
| 68 | +//// [genericTypeParameterEquivalence2strict.js] |
| 69 | +"use strict"; |
| 70 | +// compose :: (b->c) -> (a->b) -> (a->c) |
| 71 | +function compose(f, g) { |
| 72 | + return function (a) { |
| 73 | + return f(g.apply(null, a)); |
| 74 | + }; |
| 75 | +} |
| 76 | +// forEach :: [a] -> (a -> ()) -> () |
| 77 | +function forEach(list, f) { |
| 78 | + for (var i = 0; i < list.length; ++i) { |
| 79 | + f(list[i], i); |
| 80 | + } |
| 81 | +} |
| 82 | +// filter :: (a->bool) -> [a] -> [a] |
| 83 | +function filter(f, ar) { |
| 84 | + var ret = []; |
| 85 | + forEach(ar, function (el) { |
| 86 | + if (f(el)) { |
| 87 | + ret.push(el); |
| 88 | + } |
| 89 | + }); |
| 90 | + return ret; |
| 91 | +} |
| 92 | +// length :: [a] -> Num |
| 93 | +function length2(ar) { |
| 94 | + return ar.length; |
| 95 | +} |
| 96 | +// curry1 :: ((a,b)->c) -> (a->(b->c)) |
| 97 | +function curry1(f) { |
| 98 | + return function (ay) { |
| 99 | + return function (by) { |
| 100 | + return f(ay, by); |
| 101 | + }; |
| 102 | + }; |
| 103 | +} |
| 104 | +var cfilter = curry1(filter); |
| 105 | +var filterer = cfilter(strBool); |
| 106 | +var expectFilterer = filterer; |
| 107 | +var filtered = filterer(["hello"]); |
| 108 | +var expectFiltered = filtered; |
| 109 | +// compose :: (b->c) -> (a->b) -> (a->c) |
| 110 | +// length :: [a] -> Num |
| 111 | +// cfilter :: {} -> {} -> [{}] |
| 112 | +// pred :: a -> Bool |
| 113 | +// cfilter(pred) :: {} -> [{}] |
| 114 | +// length2 :: [a] -> Num |
| 115 | +// countWhere :: (a -> Bool) -> [a] -> Num |
| 116 | +function countWhere_1(pred) { |
| 117 | + return compose(length2, cfilter(pred)); |
| 118 | +} |
| 119 | +function countWhere_2(pred) { |
| 120 | + var where = cfilter(pred); |
| 121 | + return compose(length2, where); |
| 122 | +} |
0 commit comments