Press n or j to go to the next uncovered block, b, p or k for the previous block.
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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 | 29x 119x 61x 61x 119x 119x 89x 89x 89x 89x 89x 89x 30x 61x 61x 72x 72x 72x 30x 30x 30x 42x 42x 91x 89x 89x 15x 2x 42x 42x 103x 103x 28x 42x 42x 15x 27x 42x 61x 120x 120x 120x 120x 5x 5x 115x 115x 217x 217x 217x 217x 217x 217x 217x 217x 184x 8x 17x 17x 17x 2x 6x 176x 209x 61x 107x 74x 107x 28x 79x | import { LazyResult } from './_reduceLazy'; /** * Perform left-to-right function composition. * @param value The initial value. * @param operations the list of operations to apply. * @signature R.pipe(data, op1, op2, op3) * @example * R.pipe( * [1, 2, 3, 4], * R.map(x => x * 2), * arr => [arr[0] + arr[1], arr[2] + arr[3]], * ) // => [6, 14] * * * @data_first * @category Function */ export function pipe<A, B>(value: A, op1: (input: A) => B): B; export function pipe<A, B, C>( value: A, op1: (input: A) => B, op2: (input: B) => C ): C; export function pipe<A, B, C, D>( value: A, op1: (input: A) => B, op2: (input: B) => C, op3: (input: C) => D ): D; export function pipe<A, B, C, D, E>( value: A, op1: (input: A) => B, op2: (input: B) => C, op3: (input: C) => D, op4: (input: D) => E ): E; export function pipe<A, B, C, D, E, F>( value: A, op1: (input: A) => B, op2: (input: B) => C, op3: (input: C) => D, op4: (input: D) => E, op5: (input: E) => F ): F; export function pipe<A, B, C, D, E, F, G>( value: A, op1: (input: A) => B, op2: (input: B) => C, op3: (input: C) => D, op4: (input: D) => E, op5: (input: E) => F, op6: (input: F) => G ): G; export function pipe<A, B, C, D, E, F, G, H>( value: A, op1: (input: A) => B, op2: (input: B) => C, op3: (input: C) => D, op4: (input: D) => E, op5: (input: E) => F, op6: (input: F) => G, op7: (input: G) => H ): H; export function pipe( value: any, ...operations: Array<(value: any) => any> ): any { let ret = value; const lazyOps = operations.map(op => { const { lazy, lazyArgs } = op as LazyOp; if (lazy) { const fn: any = lazy(...lazyArgs); fn.indexed = lazy.indexed; fn.single = lazy.single; fn.index = 0; fn.items = []; return fn; } return null; }); let opIdx = 0; while (opIdx < operations.length) { const op = operations[opIdx]; const lazyOp = lazyOps[opIdx]; if (!lazyOp) { ret = op(ret); opIdx++; continue; } const lazySeq: LazyFn[] = []; for (let j = opIdx; j < operations.length; j++) { if (lazyOps[j]) { lazySeq.push(lazyOps[j]); if (lazyOps[j].single) { break; } } else { break; } } let acc: any[] = []; for (let j = 0; j < ret.length; j++) { let item = ret[j]; if (_processItem({ item, acc, lazySeq })) { break; } } const lastLazySeq = lazySeq[lazySeq.length - 1]; if ((lastLazySeq as any).single) { ret = acc[0]; } else { ret = acc; } opIdx += lazySeq.length; } return ret; } type LazyFn = ((value: any, index?: number, items?: any) => LazyResult<any>); type LazyOp = ((input: any) => any) & { lazy: ((...args: any[]) => LazyFn) & { indexed: boolean; single: boolean; }; lazyArgs: any[]; }; function _processItem({ item, lazySeq, acc, }: { item: any; lazySeq: any[]; acc: any[]; }): boolean { if (lazySeq.length === 0) { acc.push(item); return false; } let lazyResult: LazyResult<any> = { done: false, hasNext: false }; for (let i = 0; i < lazySeq.length; i++) { const lazyFn = lazySeq[i]; const indexed = lazyFn.indexed; const index = lazyFn.index; const items = lazyFn.items; items.push(item); lazyResult = indexed ? lazyFn(item, index, items) : lazyFn(item); lazyFn.index++; if (lazyResult.hasNext) { if (lazyResult.hasMany) { const nextValues: any[] = lazyResult.next; for (const subItem of nextValues) { const subResult = _processItem({ item: subItem, acc, lazySeq: lazySeq.slice(i + 1), }); if (subResult) { return true; } } return false; } else { item = lazyResult.next; } } if (!lazyResult.hasNext || lazyResult.done) { break; } } if (lazyResult.hasNext) { acc.push(item); } if (lazyResult.done) { return true; } return false; } |