|
| 1 | +type Mapper<T, U> = (x: T) => U; |
| 2 | + |
| 3 | +declare function wrap<T, U>(cb: Mapper<T, U>): Mapper<T, U>; |
| 4 | + |
| 5 | +declare function arrayize<T, U>(cb: Mapper<T, U>): Mapper<T, U[]>; |
| 6 | + |
| 7 | +declare function combine<A, B, C>(f: (x: A) => B, g: (x: B) => C): (x: A) => C; |
| 8 | + |
| 9 | +declare function foo(f: Mapper<string, number>): void; |
| 10 | + |
| 11 | +let f1: Mapper<string, number> = s => s.length; |
| 12 | +let f2: Mapper<string, number> = wrap(s => s.length); |
| 13 | +let f3: Mapper<string, number[]> = arrayize(wrap(s => s.length)); |
| 14 | +let f4: Mapper<string, boolean> = combine(wrap(s => s.length), wrap(n => n >= 10)); |
| 15 | + |
| 16 | +foo(wrap(s => s.length)); |
| 17 | + |
| 18 | +let a1 = ["a", "b"].map(s => s.length); |
| 19 | +let a2 = ["a", "b"].map(wrap(s => s.length)); |
| 20 | +let a3 = ["a", "b"].map(wrap(arrayize(s => s.length))); |
| 21 | +let a4 = ["a", "b"].map(combine(wrap(s => s.length), wrap(n => n > 10))); |
| 22 | +let a5 = ["a", "b"].map(combine(identity, wrap(s => s.length))); |
| 23 | +let a6 = ["a", "b"].map(combine(wrap(s => s.length), identity)); |
| 24 | + |
| 25 | +// This is a contrived class. We could do the same thing with Observables, etc. |
| 26 | +class SetOf<A> { |
| 27 | + _store: A[]; |
| 28 | + |
| 29 | + add(a: A) { |
| 30 | + this._store.push(a); |
| 31 | + } |
| 32 | + |
| 33 | + transform<B>(transformer: (a: SetOf<A>) => SetOf<B>): SetOf<B> { |
| 34 | + return transformer(this); |
| 35 | + } |
| 36 | + |
| 37 | + forEach(fn: (a: A, index: number) => void) { |
| 38 | + this._store.forEach((a, i) => fn(a, i)); |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +function compose<A, B, C, D, E>( |
| 43 | + fnA: (a: SetOf<A>) => SetOf<B>, |
| 44 | + fnB: (b: SetOf<B>) => SetOf<C>, |
| 45 | + fnC: (c: SetOf<C>) => SetOf<D>, |
| 46 | + fnD: (c: SetOf<D>) => SetOf<E>, |
| 47 | +):(x: SetOf<A>) => SetOf<E>; |
| 48 | +/* ... etc ... */ |
| 49 | +function compose<T>(...fns: ((x: T) => T)[]): (x: T) => T { |
| 50 | + return (x: T) => fns.reduce((prev, fn) => fn(prev), x); |
| 51 | +} |
| 52 | + |
| 53 | +function map<A, B>(fn: (a: A) => B): (s: SetOf<A>) => SetOf<B> { |
| 54 | + return (a: SetOf<A>) => { |
| 55 | + const b: SetOf<B> = new SetOf(); |
| 56 | + a.forEach(x => b.add(fn(x))); |
| 57 | + return b; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +function filter<A>(predicate: (a: A) => boolean): (s: SetOf<A>) => SetOf<A> { |
| 62 | + return (a: SetOf<A>) => { |
| 63 | + const result = new SetOf<A>(); |
| 64 | + a.forEach(x => { |
| 65 | + if (predicate(x)) result.add(x); |
| 66 | + }); |
| 67 | + return result; |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +const testSet = new SetOf<number>(); |
| 72 | +testSet.add(1); |
| 73 | +testSet.add(2); |
| 74 | +testSet.add(3); |
| 75 | + |
| 76 | +const t1 = testSet.transform( |
| 77 | + compose( |
| 78 | + filter(x => x % 1 === 0), |
| 79 | + map(x => x + x), |
| 80 | + map(x => x + '!!!'), |
| 81 | + map(x => x.toUpperCase()) |
| 82 | + ) |
| 83 | +) |
| 84 | + |
| 85 | +declare function identity<T>(x: T): T; |
| 86 | + |
| 87 | +const t2 = testSet.transform( |
| 88 | + compose( |
| 89 | + filter(x => x % 1 === 0), |
| 90 | + identity, |
| 91 | + map(x => x + '!!!'), |
| 92 | + map(x => x.toUpperCase()) |
| 93 | + ) |
| 94 | +) |
0 commit comments