| 60 | }; |
| 61 | |
| 62 | export const map = <T = any, U = any>(xs: ArrayLike<T>, f: ArrayMorphism<T, U>): U[] => { |
| 63 | // pre-allocating array size when it's guaranteed to be known |
| 64 | // http://jsperf.com/push-allocated-vs-dynamic/22 |
| 65 | const len = xs.length; |
| 66 | const r = new Array(len); |
| 67 | for (let i = 0; i < len; i++) { |
| 68 | const x = xs[i]; |
| 69 | r[i] = f(x, i); |
| 70 | } |
| 71 | return r; |
| 72 | }; |
| 73 | |
| 74 | // Unwound implementing other functions in terms of each. |
| 75 | // The code size is roughly the same, and it should allow for better optimisation. |