Skip to content

Commit

Permalink
Moved reduce.a to map.a. The packae version is bumped to 2.0.0 to acc…
Browse files Browse the repository at this point in the history
…ount for the breakingg change
  • Loading branch information
ludak committed Aug 25, 2021
1 parent b56512a commit ad7c11d
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 54 deletions.
34 changes: 17 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,6 @@ const o = reduce.o(items, {value: v => v + 2})
// Output: {0:2, 1:3, 2:4, 3:5, 4:6}
```

**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]
Expand Down Expand Up @@ -121,6 +104,23 @@ const scaled = map.scale(items, 2)
// Output: [0, 2, 4, 6, 8]
```

**map.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 = map.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 = map.a(items, {value: vfn, container})
// Output: [20, 2, 4, 10, 60]
```

## last
*last* gets the last element from an array without modification.
```js
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "orb-array",
"version": "1.5.0",
"version": "2.0.0",
"description": "Concise Array Programming",
"main": "./src/index.js",
"files": [
Expand Down
6 changes: 3 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ const reduce = {
o: (items = [], {key: kfn = self, value: vfn = self} = {}) =>
items.reduce((c /** container */, v, index) => (c[kfn(v, index)] = vfn(v, index), c), {}),

a: (items = [], {value: vfn = self, container = []} = {}) =>
items.reduce((c /** container */, v, index) => (c.push(vfn(v, index)), c), container),

mul: (items = []) => items.reduce((v, vi) => v*vi, 1),
rollingmul: ([first, ...rest] = []) => rest.reduce(
(container, item) => (
Expand All @@ -39,6 +36,9 @@ const reduce = {
}

const map = {
a: (items = [], {value: vfn = self, container = []} = {}) =>
items.reduce((c /** container */, v, index) => (c.push(vfn(v, index)), c), container),

scale: (items = [], factor = 1) => items.map((item) => item*factor),
}

Expand Down
66 changes: 33 additions & 33 deletions src/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,39 @@ test('map.scale-list-with-scaling-factor', t => {
})
/////////////////////////// map.scale [end] ///////////////////////////

/////////////////////////// map.a [start] ///////////////////////////
test('map.a-no-args', t => {
const o = map.a()

t.deepEqual(o, [])
})

test('map.a-with-items', t => {
const items = [1, 2, 5, 6]
const o = map.a(items)

t.deepEqual(o, items)
})

test('map.a-with-items-and-valuefn', t => {
const items = [1, 2, 5, 6]
const vfn = v => 2*v
const o = map.a(items, {value: vfn})

t.deepEqual(o, map.scale(items, 2))
})

test('map.a-with-items-and-valuefn-and-container', t => {
const items = [1, 2, 5, 6]
const vfn = v => 2*v
const container = [20]
const o = map.a(items, {value: vfn, container})

t.deepEqual(container, [20, ...map.scale(items, 2)])
t.deepEqual(o, container)
})
/////////////////////////// map.a [end] ///////////////////////////

/////////////////////////// reduce.mul [start] /////////////////////////
test('reduce.mul-no-args', t => {
const o = reduce.mul()
Expand Down Expand Up @@ -100,39 +133,6 @@ test('reduce.o-list-with-keyfn-and-valuefn-using-indices', t => {
})
/////////////////////////// reduce.o [end] ///////////////////////////

/////////////////////////// reduce.a [start] ///////////////////////////
test('reduce.a-no-args', t => {
const o = reduce.a()

t.deepEqual(o, [])
})

test('reduce.a-with-items', t => {
const items = [1, 2, 5, 6]
const o = reduce.a(items)

t.deepEqual(o, items)
})

test('reduce.a-with-items-and-valuefn', t => {
const items = [1, 2, 5, 6]
const vfn = v => 2*v
const o = reduce.a(items, {value: vfn})

t.deepEqual(o, map.scale(items, 2))
})

test('reduce.a-with-items-and-valuefn-and-container', t => {
const items = [1, 2, 5, 6]
const vfn = v => 2*v
const container = [20]
const o = reduce.a(items, {value: vfn, container})

t.deepEqual(container, [20, ...map.scale(items, 2)])
t.deepEqual(o, container)
})
/////////////////////////// reduce.a [end] ///////////////////////////

/////////////////////////// reduce.rollingmul [start] ///////////////////////////
test('reduce.rollingmul-no-args', t => {
const o = reduce.rollingmul()
Expand Down

0 comments on commit ad7c11d

Please sign in to comment.