Skip to content

Commit

Permalink
Merge 1d13450 into e8cf52d
Browse files Browse the repository at this point in the history
  • Loading branch information
dalefrancis88 committed Jul 21, 2019
2 parents e8cf52d + 1d13450 commit 8d90393
Show file tree
Hide file tree
Showing 6 changed files with 193 additions and 0 deletions.
113 changes: 113 additions & 0 deletions docs/src/pages/docs/functions/combinators.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,119 @@ safeYell(42)
//=> Left 42
```

#### compose2

`crocks/combinators/compose2`

```haskell
compose2 :: (c -> d -> e) -> (a -> c) -> (b -> d) -> a -> b -> e
```

`compose2` allows for composition between a `binary` function and
two `unary` functions. `compose2` takes a `binary` function followed by
two `unary` functions and returns a `binary` function that maps the first
argument with the first `unary` and the second with the second, passing
the results to the given `binary` and returning the result.

```javascript
import compose2 from 'crocks/combinators/compose2'

import and from 'crocks/logic/and'
import applyTo from 'crocks/combinators/applyTo'
import flip from 'crocks/combinators/flip'
import hasProp from 'crocks/predicates/hasProp'
import isNumber from 'crocks/predicates/isNumber'
import liftA2 from 'crocks/helpers/liftA2'
import map from 'crocks/pointfree/map'
import prop from 'crocks/Maybe/prop'
import safe from 'crocks/Maybe/safe'
import safeLift from 'crocks/Maybe/safeLift'

// isNonZero :: Number -> Boolean
const isNonZero = x => x !== 0

// isValidDivisor :: Number -> Boolean
const isValidDivisor = and(isNumber, isNonZero)

// divideBy :: Number -> Number -> Number
const divideBy = x => y => y / x

// safeDivide :: Number -> Number -> Maybe Number
const safeDivide =
compose2(liftA2(divideBy), safe(isValidDivisor), safe(isNumber))

safeDivide(0.5, 21)
//=> Just 42

safeDivide('0.5', 21)
//=> Nothing

safeDivide(0.5, '21')
//=> Nothing

safeDivide(29, 0)
//=> Just 0

safeDivide(0, 29)
//=> Nothing

// Item :: { id: Integer }
// Items :: Array Item
const items =
[ { id: 2 }, { id: 1 } ]

// pluck :: String -> Array Object -> Maybe a
const pluck =
compose2(applyTo, prop, flip(map))

pluck('id', items)
//=> [ Just 2, Just 1 ]

// summarize :: String -> String -> String
const summarize = name => count =>
`${name} purchased ${count} items`

// getLength :: a -> Maybe Number
const getLength = safeLift(
hasProp('length'),
x => x.length
)

// createSummary :: Person -> Array Item -> String
const createSummary = compose2(
liftA2(summarize),
prop('name'),
getLength
)

createSummary({
name: 'Sam Smith'
}, items)
// => Just "Sam Smith purchased 2 items"

// capitalize :: String -> String
const capitalize = str =>
`${str.charAt(0).toUpperCase()}${str.slice(1)}`

// join :: String -> String -> String -> String
const join = delim => right => left =>
`${left}${delim}${right}`

// toUpper :: String -> String
const toUpper = x =>
x.toUpperCase()

// createName :: String -> String -> String
const createName =
compose2(join(', '), capitalize, toUpper)

createName('Jon', 'doe')
//=> DOE, Jon

createName('sara', 'smith')
//=> SMITH, Sara
```

#### constant

`crocks/combinators/constant`
Expand Down
16 changes: 16 additions & 0 deletions src/combinators/compose2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/** @license ISC License (c) copyright 2019 original and current authors */
/** @author Dale Francis (dalefrancis88) */

const curry = require('../core/curry')
const isFunction = require('../core/isFunction')

// compose2 :: (c -> d -> e) -> (a -> c) -> (b -> d) -> a -> b -> e
function compose2(f, g, h, x, y) {
if(!isFunction(f) || !isFunction(g) || !isFunction(h)) {
throw new TypeError('compose2: First, second and third arguments must be functions')
}

return curry(f)(g(x), h(y))
}

module.exports = curry(compose2)
59 changes: 59 additions & 0 deletions src/combinators/compose2.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const test = require('tape')
const helpers = require('../test/helpers')

const bindFunc = helpers.bindFunc

const isFunction = require('../core/isFunction')

const compose2 = require('./compose2')

test('compose2', t => {
const fn = bindFunc(compose2)
const f = x => y => x * y
const g = x => x - 1
const h = x => x + 1
const x = 22
const y = 1

t.ok(isFunction(compose2), 'is a function')

const err = /TypeError: compose2: First, second and third arguments must be functions/
t.throws(fn(undefined, g, h, x, y), err, 'throws with first arg undefined')
t.throws(fn(null, g, h, x, y), err, 'throws with first arg null')
t.throws(fn(0, g, h, x, y), err, 'throws with first arg falsey number')
t.throws(fn(1, g, h, x, y), err, 'throws with first arg truthy number')
t.throws(fn('', g, h, x, y), err, 'throws with first arg falsey string')
t.throws(fn('string', g, h, x, y), err, 'throws with first arg truthy string')
t.throws(fn(false, g, h, x, y), err, 'throws with first arg false')
t.throws(fn(true, g, h, x, y), err, 'throws with first arg true')
t.throws(fn({}, g, h, x, y), err, 'throws with first arg an object')
t.throws(fn([], g, h, x, y), err, 'throws with first arg an array')

t.throws(fn(f, undefined, h, x, y), err, 'throws with second arg undefined')
t.throws(fn(f, null, h, x, y), err, 'throws with second arg null')
t.throws(fn(f, 0, h, x, y), err, 'throws with second arg falsey number')
t.throws(fn(f, 1, h, x, y), err, 'throws with second arg truthy number')
t.throws(fn(f, '', h, x, y), err, 'throws with second arg falsey string')
t.throws(fn(f, 'bling', h, x, y), err, 'throws with second arg truthy string')
t.throws(fn(f, false, h, x, y), err, 'throws with second arg false')
t.throws(fn(f, true, h, x, y), err, 'throws with second arg true')
t.throws(fn(f, {}, h, x, y), err, 'throws with second arg an object')
t.throws(fn(f, [], h, x, y), err, 'throws with second arg an array')

t.throws(fn(f, g, undefined, x, y), err, 'throws with third arg undefined')
t.throws(fn(f, g, null, x, y), err, 'throws with third arg null')
t.throws(fn(f, g, 0, x, y), err, 'throws with third arg falsey number')
t.throws(fn(f, g, 1, x, y), err, 'throws with third arg truthy number')
t.throws(fn(f, g, '', x, y), err, 'throws with third arg falsey string')
t.throws(fn(f, g, 'string', x, y), err, 'throws with third arg truthy string')
t.throws(fn(f, g, false, x, y), err, 'throws with third arg false')
t.throws(fn(f, g, true, x, y), err, 'throws with third arg true')
t.throws(fn(f, g, {}, x, y), err, 'throws with third arg an object')
t.throws(fn(f, g, [], x, y), err, 'throws with third arg an array')

const result = fn(f, g, h, x, y)

t.equal(result(), 42, 'returns expected result')

t.end()
})
1 change: 1 addition & 0 deletions src/combinators/index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = {
applyTo: require('./applyTo'),
composeB: require('./composeB'),
compose2: require('./compose2'),
constant: require('./constant'),
converge: require('./converge'),
flip: require('./flip'),
Expand Down
2 changes: 2 additions & 0 deletions src/combinators/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const index = require('.')

const applyTo = require('./applyTo')
const composeB = require('./composeB')
const compose2 = require('./compose2')
const constant = require('./constant')
const converge = require('./converge')
const flip = require('./flip')
Expand All @@ -14,6 +15,7 @@ const substitution = require('./substitution')
test('combinators entry', t => {
t.equal(index.applyTo, applyTo, 'provides the T combinator (applyTo)')
t.equal(index.composeB, composeB, 'provides the B combinator (composeB)')
t.equal(index.compose2, compose2, 'provides the compose2 combinator')
t.equal(index.constant, constant, 'provides the K combinator (constant)')
t.equal(index.converge, converge, 'provides the S\' combinator (converge)')
t.equal(index.flip, flip, 'provides the C combinator (flip)')
Expand Down
2 changes: 2 additions & 0 deletions src/index.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const crocks = require('./index')
// combinators
const applyTo = require('./combinators/applyTo')
const composeB = require('./combinators/composeB')
const compose2 = require('./combinators/compose2')
const constant = require('./combinators/constant')
const flip = require('./combinators/flip')
const identity = require('./combinators/identity')
Expand Down Expand Up @@ -244,6 +245,7 @@ test('entry', t => {
// combinators
t.equal(crocks.applyTo, applyTo, 'provides the T combinator (applyTo)')
t.equal(crocks.composeB, composeB, 'provides the B combinator (composeB)')
t.equal(crocks.compose2, compose2, 'provides the compose2 combinator')
t.equal(crocks.constant, constant, 'provides the K combinator (constant)')
t.equal(crocks.flip, flip, 'provides the C combinator (flip)')
t.equal(crocks.identity, identity, 'provides the I combinator (identity)')
Expand Down

0 comments on commit 8d90393

Please sign in to comment.