Skip to content

Commit

Permalink
All the combine functions
Browse files Browse the repository at this point in the history
  • Loading branch information
mgreystone committed Jan 11, 2018
1 parent daeefeb commit 80939a9
Show file tree
Hide file tree
Showing 14 changed files with 277 additions and 16 deletions.
74 changes: 74 additions & 0 deletions API.md
Expand Up @@ -4,6 +4,12 @@
| -------- | --------- |
| [`all`](#all) | `[Promise a] -> Promise [a]` |
| [`backoff`](#backoff) | `Number -> Number -> (a... -> Promise b) -> a... -> Promise b` |
| [`combine`](#combine) | `({ k: v } -> { k: v }) -> { k: v }` |
| [`combineAll`](#combineall) | `[a... -> { k: v }] -> { k: v } -> { k: v }` |
| [`combineAllP`](#combineallp) | `[a... -> Promise { k: v }] -> { k: v } -> Promise { k: v }` |
| [`combineP`](#combinep) | `({ k: v } -> Promise { k: v }) -> Promise { k: v }` |
| [`combineWith`](#combinewith) | `(c -> b -> d) (a -> b) -> c -> d` |
| [`combineWithP`](#combinewithp) | `(c -> b -> d) (a -> Promise b) -> Promise c -> Promise d` |
| [`convergeP`](#convergep) | `(b -> c -> Promise d) -> [(a -> Promise b), (a -> Promise c)] -> a -> Promise d` |
| [`copyProp`](#copyprop) | `String -> String -> { k: v } -> { k: v }` |
| [`evolveP`](#evolvep) | `{ k: (v -> Promise v) } -> { k: v } -> Promise { k: v }` |
Expand Down Expand Up @@ -47,6 +53,74 @@ const fetchImage = opts => { /* async, and might fail sometimes */ }
backoff(250, 5, fetchImage) //=> a new function that tries at most 5 times before rejecting
```

### combine

```haskell
combine : ({ k: v } -> { k: v }) -> { k: v }
```

Accepts a function & an object. Merges the results of the function into the object.

```js
combine(always({ foo: 1 }), { foo: 2, bar: 3 }) //=> { foo: 1, baz: 3 }
```

### combineAll

```haskell
combineAll : combineAll : [a... -> { k: v }] -> { k: v } -> { k: v }
```

Accepts a list of functions & an object. Merges the results of all the functions into the object left-to-right

```js
combineAll([ always({ foo: 1, bar: 2 }), always({ bar: 3 }) ], { foo: 4, baz: 5 }) //=> { foo: 1, bar: 3, baz: 5 }
```

### combineP

```haskell
combineP : ({ k: v } -> Promise { k: v }) -> Promise { k: v }
```

Async version of [`combine`](#combine)

Accepts an async function & an object. Merges the results of the function into the object.

```js
combineP(always(resolve({ foo: 1 })), { foo: 2, bar: 3 }) //=> Promise { foo: 1, baz: 3 }
```

### combineWith

```haskell
combineWith : (c -> b -> d) (a -> b) -> c -> d
```

Accepts a merging function, a transformation function, and an value. Uses the merging function to merge the results of the transformation function into the value.

```js
combineWith(multiply, add(2), 3) //=> 15
combineWith(mergeDeepLeft, always({ foo: { bar: 1, bip: 2 } }), { foo: { bar: 3, baz: 4 } })
//=> { foo: { bar: 3, baz: 4, bip: 2 } }
```

### combineWithP

```haskell
combineWithP : (c -> b -> d) (a -> Promise b) -> Promise c -> Promise d
```

Async version of [`combineWith`](#combinewith).

Accepts a merging function, an async transformation function, and an value. Uses the merging function to merge the results of the transformation function into the value.

```js
combineWith(multiply, compose(resolve, add(2)), 3) //=> Promise 15
combineWith(mergeDeepLeft, always(resolve({ foo: { bar: 1, bip: 2 } })), { foo: { bar: 3, baz: 4 } })
//=> Promise { foo: { bar: 3, baz: 4, bip: 2 } }
```

### convergeP

```haskell
Expand Down
38 changes: 22 additions & 16 deletions index.js
@@ -1,16 +1,22 @@
exports.all = require('./lib/all')
exports.backoff = require('./lib/backoff')
exports.convergeP = require('./lib/convergeP')
exports.copyProp = require('./lib/copyProp')
exports.evolveP = require('./lib/evolveP')
exports.juxtP = require('./lib/juxtP')
exports.mapP = require('./lib/mapP')
exports.move = require('./lib/move')
exports.normalizeBy = require('./lib/normalizeBy')
exports.overP = require('./lib/overP')
exports.promisify = require('./lib/promisify')
exports.reject = require('./lib/reject')
exports.rename = require('./lib/rename')
exports.resolve = require('./lib/resolve')
exports.tapP = require('./lib/tapP')
exports.validate = require('./lib/validate')
exports.all = require('./lib/all')
exports.backoff = require('./lib/backoff')
exports.combine = require('./lib/combine')
exports.combineAll = require('./lib/combineAll')
exports.combineAllP = require('./lib/combineAllP')
exports.combineP = require('./lib/combineP')
exports.combineWith = require('./lib/combineWith')
exports.combineWithP = require('./lib/combineWithP')
exports.convergeP = require('./lib/convergeP')
exports.copyProp = require('./lib/copyProp')
exports.evolveP = require('./lib/evolveP')
exports.juxtP = require('./lib/juxtP')
exports.mapP = require('./lib/mapP')
exports.move = require('./lib/move')
exports.normalizeBy = require('./lib/normalizeBy')
exports.overP = require('./lib/overP')
exports.promisify = require('./lib/promisify')
exports.reject = require('./lib/reject')
exports.rename = require('./lib/rename')
exports.resolve = require('./lib/resolve')
exports.tapP = require('./lib/tapP')
exports.validate = require('./lib/validate')
5 changes: 5 additions & 0 deletions src/combine.js
@@ -0,0 +1,5 @@
const merge = require('ramda/src/merge')
const combineWith = require('./combineWith')

// combine : ({ k: v } -> { k: v }) -> { k: v }
module.exports = combineWith(merge)
8 changes: 8 additions & 0 deletions src/combineAll.js
@@ -0,0 +1,8 @@
const compose = require('ramda/src/compose')
const identity = require('ramda/src/identity')
const juxt = require('ramda/src/juxt')
const mergeAll = require('ramda/src/mergeAll')

// combineAll : [a... -> { k: v }] -> { k: v } -> { k: v }
module.exports = fns =>
compose(mergeAll, juxt([ identity, ...fns ]))
9 changes: 9 additions & 0 deletions src/combineAllP.js
@@ -0,0 +1,9 @@
const composeP = require('ramda/src/composeP')
const identity = require('ramda/src/identity')
const mergeAll = require('ramda/src/mergeAll')

const juxtP = require('./juxtP')

// combineAllP : [a... -> Promise { k: v }] -> { k: v } -> Promise { k: v }
module.exports = fns =>
composeP(mergeAll, juxtP([ identity, ...fns ]))
5 changes: 5 additions & 0 deletions src/combineP.js
@@ -0,0 +1,5 @@
const merge = require('ramda/src/merge')
const combineWithP = require('./combineWithP')

// combineP : ({ k: v } -> Promise { k: v }) -> Promise { k: v }
module.exports = combineWithP(merge)
9 changes: 9 additions & 0 deletions src/combineWith.js
@@ -0,0 +1,9 @@
const converge = require('ramda/src/converge')
const curry = require('ramda/src/curry')
const identity = require('ramda/src/identity')

// combineWith : (c -> b -> d) (a -> b) -> c -> d
const combineWith = (mf, f) =>
converge(mf, [ identity, f ])

module.exports = curry(combineWith)
10 changes: 10 additions & 0 deletions src/combineWithP.js
@@ -0,0 +1,10 @@
const curry = require('ramda/src/curry')
const identity = require('ramda/src/identity')

const convergeP = require('./convergeP')

// combineWithP : (c -> b -> d) (a -> Promise b) -> Promise c -> Promise d
const combineWithP = (mf, f) =>
convergeP(mf, [ identity, f ])

module.exports = curry(combineWithP)
22 changes: 22 additions & 0 deletions test/combine.js
@@ -0,0 +1,22 @@
const { expect } = require('chai')
const property = require('prop-factory')
const always = require('ramda/src/always')

const { combine } = require('..')

const whatevs = combine(always({ foo: 'bar' }))

describe('combine', () => {
const res = property()

beforeEach(() =>
res(whatevs({ baz: 'bip' }))
)

it('merges with the results of the function', () =>
expect(res()).to.eql({
foo: 'bar',
baz: 'bip',
})
)
})
26 changes: 26 additions & 0 deletions test/combineAll.js
@@ -0,0 +1,26 @@
const { expect } = require('chai')
const property = require('prop-factory')
const always = require('ramda/src/always')

const { combineAll } = require('..')

const whatevs = combineAll([
always({ foo: 1 }),
always({ bar: 2 }),
])

describe('combineAll', () => {
const res = property()

beforeEach(() =>
res(whatevs({ baz: 3 }))
)

it('combines the results of all functions', () =>
expect(res()).to.eql({
foo: 1,
bar: 2,
baz: 3,
})
)
})
26 changes: 26 additions & 0 deletions test/combineAllP.js
@@ -0,0 +1,26 @@
const { expect } = require('chai')
const property = require('prop-factory')
const always = require('ramda/src/always')

const { combineAllP } = require('..')

const whatevs = combineAllP([
always(Promise.resolve({ foo: 1 })),
always(Promise.resolve({ bar: 2 })),
])

describe('combineAll', () => {
const res = property()

beforeEach(() =>
whatevs({ baz: 3 }).then(res)
)

it('combines the results of all functions', () =>
expect(res()).to.eql({
foo: 1,
bar: 2,
baz: 3,
})
)
})
22 changes: 22 additions & 0 deletions test/combineP.js
@@ -0,0 +1,22 @@
const { expect } = require('chai')
const property = require('prop-factory')
const always = require('ramda/src/always')

const { combineP } = require('..')

const whatevs = combineP(always(Promise.resolve({ foo: 'bar' })))

describe('combine', () => {
const res = property()

beforeEach(() =>
whatevs({ baz: 'bip' }).then(res)
)

it('merges with the results of the function', () =>
expect(res()).to.eql({
foo: 'bar',
baz: 'bip',
})
)
})
20 changes: 20 additions & 0 deletions test/combineWith.js
@@ -0,0 +1,20 @@
const { expect } = require('chai')
const property = require('prop-factory')
const add = require('ramda/src/add')
const multiply = require('ramda/src/multiply')

const { combineWith } = require('..')

const whatevs = combineWith(multiply, add(2))

describe('combineWith', () => {
const res = property()

beforeEach(() =>
res(whatevs(3))
)

it('combines with the results of the function', () =>
expect(res()).to.equal(15)
)
})
19 changes: 19 additions & 0 deletions test/combineWithP.js
@@ -0,0 +1,19 @@
const { expect } = require('chai')
const property = require('prop-factory')

const { add, mult } = require('./lib/async')
const { combineWithP } = require('..')

const whatevs = combineWithP(mult, add(2))

describe('combineWith', () => {
const res = property()

beforeEach(() =>
whatevs(3).then(res)
)

it('combines with the results of the function', () =>
expect(res()).to.equal(15)
)
})

0 comments on commit 80939a9

Please sign in to comment.