Skip to content

Commit

Permalink
New APIs
Browse files Browse the repository at this point in the history
 We have added new APIs: reduce.a, reduce.rollingmul, reduce.sum, ranges, last, resolveAddress and repeat.
  • Loading branch information
NareshPS authored Jun 10, 2021
1 parent ba54c92 commit 1690597
Showing 1 changed file with 89 additions and 1 deletion.
90 changes: 89 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,24 @@ const o = reduce.o(items, {value: v => v + 2})
// Output: {0:2, 1:3, 2:4, 3:5, 4:6}
```

**reduce.mul** multiplies elements of the input array together. When the input contains a non-numerical value, the output is **NaN**. The boolean values are converted to their numerical form (0 or 1).
**reduce.a** transforms an array. A value function is applied to the original values. A custom container can be used to store the results. Some examples:
```js
// Simple Example
const items = range(5)
const o = reduce.a(items)
// Output: [0, 1, 2, 3, 4]
```

```js
// Using value function and a container
const items = [1, 2, 5, 6]
const vfn = v => 2*v
const container = [20]
const o = reduce.a(items, {value: vfn, container})
// Output: [20, 2, 4, 10, 60]
```

**reduce.mul** multiplies together all the elements of an 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).
```js
const items = [1, 2, 5, 6]
const result = reduce.mul(items)
Expand All @@ -78,6 +95,22 @@ const result = reduce.mul([])
// Output: 1
```

**reduce.rollingmul** performs cumulative multiplication. Follow the following examples:
```
// Rolling Multiplication
const items = [1, 2, 5, 6]
const o = reduce.rollingmul(items)
// Output: [1, 2, 10, 60]
```

**reduce.sum** adds the elements together.
```
// Addition
const items = [1, 2, 3, 4, 6]
const o = reduce.sum(items)
// Output: 16
```

## map
*map* supports several operations.

Expand All @@ -87,3 +120,58 @@ const items = range(5)
const scaled = map.scale(items, 2)
// Output: [0, 2, 4, 6, 8]
```

## last
*last* gets the last element from an array without modification.
```js
// Action
const items = ['last-input']
const o = last(items)
// Output: 'last-input'
```

## ranges
*ranges* simplifies nested iterations over the deep arrays. It creates a list of addresses for individual elements. **resolveAddress** API resolves them. Follow the following examples:
```js
// Ranges Demonstration
const o = ranges(2, 3)
// Output: [[0, 0], [0, 1], [0, 2], [1, 0], [1, 1], [1, 2]]
```
```js
// Access Elements
const deepfruits = [['mango'], ['apple']]
const o = ranges(2, 2)
const fruits = o.map((address) => resolveAddress(deepfruits, address)
// Output: ['mango', 'apple']
```
## resolveAddress
*resolveAddress* resolves the ordered indices in a nested array or the ordered keys in a nested object. The order must follow the parent-child relationship.
```js
// Array Example
const deepfruits = [['mango'], ['apple']]
const o = resolveAddress(deepfruits, [1, 0])
// Output: 'apple'
```
```js
// Object Example
const fruits = {
tropical: {
summer: ['mango', 'orange']
},
wild: {
names: ['berries']
}
}
const o = resolveAddress(fruits, ['wild', 'names'])
// Output: ['berries]
```
## repeat
*repeat* repeats an object. The repeat count is configurable.
```js
// Basic Example
const input = {tree: 'tonmayi'}
const o = repeat(input, 2)
// Output: [{tree: 'tonmayi'}, {tree: 'tonmayi'}, {tree: 'tonmayi'}]
```

0 comments on commit 1690597

Please sign in to comment.