|
| 1 | +//// [inferFromGenericFunctionReturnTypes2.ts] |
| 2 | +type Mapper<T, U> = (x: T) => U; |
| 3 | + |
| 4 | +declare function wrap<T, U>(cb: Mapper<T, U>): Mapper<T, U>; |
| 5 | + |
| 6 | +declare function arrayize<T, U>(cb: Mapper<T, U>): Mapper<T, U[]>; |
| 7 | + |
| 8 | +declare function combine<A, B, C>(f: (x: A) => B, g: (x: B) => C): (x: A) => C; |
| 9 | + |
| 10 | +declare function foo(f: Mapper<string, number>): void; |
| 11 | + |
| 12 | +let f1: Mapper<string, number> = s => s.length; |
| 13 | +let f2: Mapper<string, number> = wrap(s => s.length); |
| 14 | +let f3: Mapper<string, number[]> = arrayize(wrap(s => s.length)); |
| 15 | +let f4: Mapper<string, boolean> = combine(wrap(s => s.length), wrap(n => n >= 10)); |
| 16 | + |
| 17 | +foo(wrap(s => s.length)); |
| 18 | + |
| 19 | +let a1 = ["a", "b"].map(s => s.length); |
| 20 | +let a2 = ["a", "b"].map(wrap(s => s.length)); |
| 21 | +let a3 = ["a", "b"].map(wrap(arrayize(s => s.length))); |
| 22 | +let a4 = ["a", "b"].map(combine(wrap(s => s.length), wrap(n => n > 10))); |
| 23 | +let a5 = ["a", "b"].map(combine(identity, wrap(s => s.length))); |
| 24 | +let a6 = ["a", "b"].map(combine(wrap(s => s.length), identity)); |
| 25 | + |
| 26 | +// This is a contrived class. We could do the same thing with Observables, etc. |
| 27 | +class SetOf<A> { |
| 28 | + _store: A[]; |
| 29 | + |
| 30 | + add(a: A) { |
| 31 | + this._store.push(a); |
| 32 | + } |
| 33 | + |
| 34 | + transform<B>(transformer: (a: SetOf<A>) => SetOf<B>): SetOf<B> { |
| 35 | + return transformer(this); |
| 36 | + } |
| 37 | + |
| 38 | + forEach(fn: (a: A, index: number) => void) { |
| 39 | + this._store.forEach((a, i) => fn(a, i)); |
| 40 | + } |
| 41 | +} |
| 42 | + |
| 43 | +function compose<A, B, C, D, E>( |
| 44 | + fnA: (a: SetOf<A>) => SetOf<B>, |
| 45 | + fnB: (b: SetOf<B>) => SetOf<C>, |
| 46 | + fnC: (c: SetOf<C>) => SetOf<D>, |
| 47 | + fnD: (c: SetOf<D>) => SetOf<E>, |
| 48 | +):(x: SetOf<A>) => SetOf<E>; |
| 49 | +/* ... etc ... */ |
| 50 | +function compose<T>(...fns: ((x: T) => T)[]): (x: T) => T { |
| 51 | + return (x: T) => fns.reduce((prev, fn) => fn(prev), x); |
| 52 | +} |
| 53 | + |
| 54 | +function map<A, B>(fn: (a: A) => B): (s: SetOf<A>) => SetOf<B> { |
| 55 | + return (a: SetOf<A>) => { |
| 56 | + const b: SetOf<B> = new SetOf(); |
| 57 | + a.forEach(x => b.add(fn(x))); |
| 58 | + return b; |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +function filter<A>(predicate: (a: A) => boolean): (s: SetOf<A>) => SetOf<A> { |
| 63 | + return (a: SetOf<A>) => { |
| 64 | + const result = new SetOf<A>(); |
| 65 | + a.forEach(x => { |
| 66 | + if (predicate(x)) result.add(x); |
| 67 | + }); |
| 68 | + return result; |
| 69 | + } |
| 70 | +} |
| 71 | + |
| 72 | +const testSet = new SetOf<number>(); |
| 73 | +testSet.add(1); |
| 74 | +testSet.add(2); |
| 75 | +testSet.add(3); |
| 76 | + |
| 77 | +const t1 = testSet.transform( |
| 78 | + compose( |
| 79 | + filter(x => x % 1 === 0), |
| 80 | + map(x => x + x), |
| 81 | + map(x => x + '!!!'), |
| 82 | + map(x => x.toUpperCase()) |
| 83 | + ) |
| 84 | +) |
| 85 | + |
| 86 | +declare function identity<T>(x: T): T; |
| 87 | + |
| 88 | +const t2 = testSet.transform( |
| 89 | + compose( |
| 90 | + filter(x => x % 1 === 0), |
| 91 | + identity, |
| 92 | + map(x => x + '!!!'), |
| 93 | + map(x => x.toUpperCase()) |
| 94 | + ) |
| 95 | +) |
| 96 | + |
| 97 | + |
| 98 | +//// [inferFromGenericFunctionReturnTypes2.js] |
| 99 | +var f1 = function (s) { return s.length; }; |
| 100 | +var f2 = wrap(function (s) { return s.length; }); |
| 101 | +var f3 = arrayize(wrap(function (s) { return s.length; })); |
| 102 | +var f4 = combine(wrap(function (s) { return s.length; }), wrap(function (n) { return n >= 10; })); |
| 103 | +foo(wrap(function (s) { return s.length; })); |
| 104 | +var a1 = ["a", "b"].map(function (s) { return s.length; }); |
| 105 | +var a2 = ["a", "b"].map(wrap(function (s) { return s.length; })); |
| 106 | +var a3 = ["a", "b"].map(wrap(arrayize(function (s) { return s.length; }))); |
| 107 | +var a4 = ["a", "b"].map(combine(wrap(function (s) { return s.length; }), wrap(function (n) { return n > 10; }))); |
| 108 | +var a5 = ["a", "b"].map(combine(identity, wrap(function (s) { return s.length; }))); |
| 109 | +var a6 = ["a", "b"].map(combine(wrap(function (s) { return s.length; }), identity)); |
| 110 | +// This is a contrived class. We could do the same thing with Observables, etc. |
| 111 | +var SetOf = (function () { |
| 112 | + function SetOf() { |
| 113 | + } |
| 114 | + SetOf.prototype.add = function (a) { |
| 115 | + this._store.push(a); |
| 116 | + }; |
| 117 | + SetOf.prototype.transform = function (transformer) { |
| 118 | + return transformer(this); |
| 119 | + }; |
| 120 | + SetOf.prototype.forEach = function (fn) { |
| 121 | + this._store.forEach(function (a, i) { return fn(a, i); }); |
| 122 | + }; |
| 123 | + return SetOf; |
| 124 | +}()); |
| 125 | +/* ... etc ... */ |
| 126 | +function compose() { |
| 127 | + var fns = []; |
| 128 | + for (var _i = 0; _i < arguments.length; _i++) { |
| 129 | + fns[_i] = arguments[_i]; |
| 130 | + } |
| 131 | + return function (x) { return fns.reduce(function (prev, fn) { return fn(prev); }, x); }; |
| 132 | +} |
| 133 | +function map(fn) { |
| 134 | + return function (a) { |
| 135 | + var b = new SetOf(); |
| 136 | + a.forEach(function (x) { return b.add(fn(x)); }); |
| 137 | + return b; |
| 138 | + }; |
| 139 | +} |
| 140 | +function filter(predicate) { |
| 141 | + return function (a) { |
| 142 | + var result = new SetOf(); |
| 143 | + a.forEach(function (x) { |
| 144 | + if (predicate(x)) |
| 145 | + result.add(x); |
| 146 | + }); |
| 147 | + return result; |
| 148 | + }; |
| 149 | +} |
| 150 | +var testSet = new SetOf(); |
| 151 | +testSet.add(1); |
| 152 | +testSet.add(2); |
| 153 | +testSet.add(3); |
| 154 | +var t1 = testSet.transform(compose(filter(function (x) { return x % 1 === 0; }), map(function (x) { return x + x; }), map(function (x) { return x + '!!!'; }), map(function (x) { return x.toUpperCase(); }))); |
| 155 | +var t2 = testSet.transform(compose(filter(function (x) { return x % 1 === 0; }), identity, map(function (x) { return x + '!!!'; }), map(function (x) { return x.toUpperCase(); }))); |
0 commit comments