|
1 | | -# orb-array |
| 1 | +# orb-array |
| 2 | +*orb-array* aims to simplify common array operations that use functional programming constructs. The usage of map, reduce, filter, fill etc. APIs is often verbose. Our goal is to make their usage *less-verbose* |
| 3 | + |
| 4 | +## APIs |
| 5 | +### split |
| 6 | +It splits an array into specified number of pieces. When the number of pieces is larger than the input size, it creates empty pieces. It always returns the specified number of pieces. Following are some examples: |
| 7 | +```js |
| 8 | +// Default is splitting from the middle. |
| 9 | +const items = [1, 2, 3, 4, 5] |
| 10 | +const pieces = split(items) |
| 11 | +// Output: [[1, 2, 3], [4, 5]] |
| 12 | +``` |
| 13 | + |
| 14 | +```js |
| 15 | +const items = [1, 2, 3, 4, 5] |
| 16 | +const pieces = split(items, 10) |
| 17 | +// Output: [[1], [2], [3], [4], [5], [], [], [], [], []] |
| 18 | +``` |
| 19 | + |
| 20 | +### range |
| 21 | +It generates numbers in a given range, starting with 0. |
| 22 | +```js |
| 23 | +const items = range(5) |
| 24 | +// Output: [0, 1, 2, 3, 4] |
| 25 | +``` |
| 26 | + |
| 27 | +### fill |
| 28 | +It generates a range of values using a function. |
| 29 | +```js |
| 30 | +const items = fill(5, v => v*2) // v is an item index |
| 31 | +// Output: [0, 2, 4, 6, 8] |
| 32 | +``` |
| 33 | + |
| 34 | +### zip |
| 35 | +It zips a bunch of arrays together. If the arrays vary in size, the output size is equal to the shortest array. |
| 36 | +```js |
| 37 | +const items = range(5) |
| 38 | +const values = range(10) |
| 39 | +const zipped = zip(items, values) |
| 40 | +// Output: [[0, 0], [1, 1], [2, 2], [3, 3], [4, 4]] |
| 41 | +``` |
| 42 | +### reduce |
| 43 | +*reduce* support several operations. |
| 44 | + |
| 45 | +**reduce.o** reduces an array to an object. It supports customizations using key and value functions. In the absence of customizations, key and value are the input array items. |
| 46 | +```js |
| 47 | +const items = range(5) |
| 48 | +const o = reduce.o(items) |
| 49 | +// Output: {0:0, 1:1, 2:2, 3:3, 4:4} |
| 50 | +``` |
| 51 | +```js |
| 52 | +const items = range(5) |
| 53 | +const o = reduce.o(items, {value: v => v + 2}) |
| 54 | +// Output: {0:2, 1:3, 2:4, 3:5, 4:6} |
| 55 | +``` |
| 56 | + |
| 57 | +**reduce.mul* multiplies together the elements of the input array. When the input contains a non-numerical value, the output is **NaN**. The boolean values are converted to their numerical form (0 or 1). |
| 58 | +```js |
| 59 | +const items = [1, 2, 5, 6] |
| 60 | +const result = reduce.mul(items) |
| 61 | +// Output: 60 |
| 62 | +``` |
| 63 | +```js |
| 64 | +// It returns 1 for an empty input. |
| 65 | +const result = reduce.mul([]) |
| 66 | +// Output: 1 |
| 67 | +``` |
| 68 | + |
| 69 | +### map |
| 70 | +*map* supports several operations. |
| 71 | + |
| 72 | +**map.scale** uses the input factor to scale elements. |
| 73 | +```js |
| 74 | +const items = range(5) |
| 75 | +const scaled = map.scale(items, 2) |
| 76 | +// Output: [0, 2, 4, 6, 8] |
| 77 | +``` |
0 commit comments