Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move isType out of internal and make it a predicate named isSameType #50

Merged
merged 1 commit into from
Feb 4, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 21 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ All `Crocks` are Constructor functions of the given type, with `Writer` being an

| Crock | Constructor | Instance |
|---|:---|:---|
| `Arrow` | `empty` | `concat`, `contramap`, `empty`, `first`, `map`, `promap`, `runWith`, `second`, `value` |
| `Arrow` | `empty` | `both`, `concat`, `contramap`, `empty`, `first`, `map`, `promap`, `runWith`, `second`, `value` |
| `Async` | `all`, `fromNode`, `fromPromise`, `of`, `rejected` | `ap`, `bimap`, `chain`, `coalesce`, `fork`, `map`, `of`, `swap`, `toPromise` |
| `Const` | -- | `ap`, `chain`, `concat`, `equals`, `map`, `value` |
| `Either` | `Left`, `Right`, `of`| `ap`, `bimap`, `chain`, `coalesce`, `either`, `equals`, `map`, `of`, `sequence`, `swap`, `traverse`, `value` |
Expand All @@ -81,7 +81,7 @@ All `Crocks` are Constructor functions of the given type, with `Writer` being an
| `Pair` | `of` | `ap`, `bimap`, `chain`, `concat`, `equals`, `fst`, `map`, `merge`, `of`, `snd`, `swap`, `value` |
| `Pred`[*] | `empty` | `concat`, `contramap`, `empty`, `runWith`, `value` |
| `Reader` | `ask`, `of`| `ap`, `chain`, `map`, `of`, `runWith` |
| `Star` | -- | `contramap`, `map`, `promap`, `runWith` |
| `Star` | -- | `both`, `contramap`, `map`, `promap`, `runWith` |
| `State` | `get`, `gets`, `modify` `of`, `put`| `ap`, `chain`, `evalWith`, `execWith`, `map`, `of`, `runWith` |
| `Unit` | `empty`, `of` | `ap`, `chain`, `concat`, `empty`, `equals`, `map`, `of`, `value` |
| `Writer`| `of` | `ap`, `chain`, `equals`, `log`, `map`, `of`, `read`, `value` |
Expand Down Expand Up @@ -198,25 +198,26 @@ There may come a time when you need to adjust a value when a condition is true,
All functions in this group have a signature of `* -> Boolean` and are used with the many predicate based functions that ship with `crocks`, like `safe`, `ifElse` and `filter` to name a few. They also fit naturally with the `Pred` ADT. Below is a list of all the current predicates that are included with a description of their truth:

* `hasKey : (String | Number) -> a -> Boolean`: An Array or Object that contains the provided index or key
* `isApplicative`: an ADT that provides `map`, `ap` and `of` functions
* `isApply`: an ADT that provides `map` and `ap` functions
* `isArray`: Array
* `isBoolean`: Boolean
* `isApplicative : a -> Boolean`: an ADT that provides `map`, `ap` and `of` functions
* `isApply : a -> Boolean`: an ADT that provides `map` and `ap` functions
* `isArray : a -> Boolean`: Array
* `isBoolean : a -> Boolean`: Boolean
* `isDefined : a -> Boolean`: Every value that is not `undefined`, `null` included
* `isEmpty`: Empty Object, Array or String
* `isFoldable`: Array, List or any structure with a `reduce` function
* `isFunction`: Function
* `isFunctor`: an ADT that provides a `map` function
* `isInteger`: Integer
* `isMonad`: an ADT that provides `map`, `ap`, `chain` and `of` functions
* `isMonoid`: an ADT that provides `concat` and `empty` functions
* `isNil`: undefined or null
* `isNumber`: Number that is not a NaN value, Infinity included
* `isObject`: Plain Old Javascript Object (POJO)
* `isSemigroup`: an ADT that provides a `concat` function
* `isSetoid`: an ADT that provides an `equals` function
* `isString`: String
* `isTraversable`: an ADT that provides `map` and `traverse` functions
* `isEmpty : a -> Boolean`: Empty Object, Array or String
* `isFoldable : a -> Boolean`: Array, List or any structure with a `reduce` function
* `isFunction : a -> Boolean`: Function
* `isFunctor : a -> Boolean`: an ADT that provides a `map` function
* `isInteger : a -> Boolean`: Integer
* `isMonad : a -> Boolean`: an ADT that provides `map`, `ap`, `chain` and `of` functions
* `isMonoid : a -> Boolean`: an ADT that provides `concat` and `empty` functions
* `isNil : a -> Boolean`: undefined or null
* `isNumber : a -> Boolean`: Number that is not a NaN value, Infinity included
* `isObject : a -> Boolean`: Plain Old Javascript Object (POJO)
* `isSameType : a -> b -> Boolean`: both ADTs are of the same type (Instance or TypeRep)
* `isSemigroup : a -> Boolean`: an ADT that provides a `concat` function
* `isSetoid : a -> Boolean`: an ADT that provides an `equals` function
* `isString : a -> Boolean`: String
* `isTraversable : a -> Boolean`: an ADT that provides `map` and `traverse` functions

### Point-free Functions
While it can seem natural to work with all these containers in a fluent fashion, it can get cumbersome and hard to get a lot of reuse out of. A way to really get the most out of reusability in Javascript is to take what is called a point-free approach. Below is a small code same to contrast the difference between the two calling styles:
Expand Down
8 changes: 7 additions & 1 deletion crocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -105,17 +105,23 @@ const predicates = {
isApplicative: require('./predicates/isApplicative'),
isApply: require('./predicates/isApply'),
isArray: require('./predicates/isArray'),
isBoolean: require('./predicates/isBoolean'),
isDefined: require('./predicates/isDefined'),
isEmpty: require('./predicates/isEmpty'),
isFoldable: require('./predicates/isFoldable'),
isFunction: require('./predicates/isFunction'),
isFunctor: require('./predicates/isFunctor'),
isInteger: require('./predicates/isInteger'),
isMonad: require('./predicates/isMonad'),
isMonoid: require('./predicates/isMonoid'),
isNil: require('./predicates/isNil'),
isNumber: require('./predicates/isNumber'),
isObject: require('./predicates/isObject'),
isSameType: require('./predicates/isSameType'),
isSetoid: require('./predicates/isSetoid'),
isSemigroup: require('./predicates/isSemigroup'),
isString: require('./predicates/isString')
isString: require('./predicates/isString'),
isTraversable: require('./predicates/isTraversable')
}

const transforms = {
Expand Down
12 changes: 12 additions & 0 deletions crocks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,23 @@ const hasKey = require('./predicates/hasKey')
const isApplicative = require('./predicates/isApplicative')
const isApply = require('./predicates/isApply')
const isArray = require('./predicates/isArray')
const isBoolean = require('./predicates/isBoolean')
const isDefined = require('./predicates/isDefined')
const isEmpty = require('./predicates/isEmpty')
const isFoldable = require('./predicates/isFoldable')
const isFunction = require('./predicates/isFunction')
const isFunctor = require('./predicates/isFunctor')
const isInteger = require('./predicates/isInteger')
const isMonad = require('./predicates/isMonad')
const isMonoid = require('./predicates/isMonoid')
const isNil = require('./predicates/isNil')
const isNumber = require('./predicates/isNumber')
const isObject = require('./predicates/isObject')
const isSameType = require('./predicates/isSameType')
const isSemigroup = require('./predicates/isSemigroup')
const isSetoid = require('./predicates/isSetoid')
const isString = require('./predicates/isString')
const isTraversable = require('./predicates/isTraversable')

const eitherToAsync = require('./transforms/eitherToAsync')
const eitherToMaybe = require('./transforms/eitherToMaybe')
Expand Down Expand Up @@ -210,17 +216,23 @@ test('entry', t => {
t.equal(crocks.isApplicative, isApplicative, 'provides the isApplicative function')
t.equal(crocks.isApply, isApply, 'provides the isApply function')
t.equal(crocks.isArray, isArray, 'provides the isArray function')
t.equal(crocks.isBoolean, isBoolean, 'provides the isBoolean function')
t.equal(crocks.isDefined, isDefined, 'provides the isDefined function')
t.equal(crocks.isEmpty, isEmpty, 'provides the isEmpty function')
t.equal(crocks.isFoldable, isFoldable, 'provides the isFoldable function')
t.equal(crocks.isFunction, isFunction, 'provides the isFunction function')
t.equal(crocks.isFunctor, isFunctor, 'provides the isFunctor function')
t.equal(crocks.isInteger, isInteger, 'provides the isInteger function')
t.equal(crocks.isMonad, isMonad, 'provides the isMonad function')
t.equal(crocks.isMonoid, isMonoid, 'provides the isMonoid function')
t.equal(crocks.isNil, isNil, 'provides the isNil function')
t.equal(crocks.isNumber, isNumber, 'provides the isNumber function')
t.equal(crocks.isObject, isObject, 'provides the isObject function')
t.equal(crocks.isSameType, isSameType, 'provides the isSameType function')
t.equal(crocks.isSemigroup, isSemigroup, 'provides the isSemigroup function')
t.equal(crocks.isSetoid, isSetoid, 'provides the isSetoid function')
t.equal(crocks.isString, isString, 'provides the isString function')
t.equal(crocks.isTraversable, isTraversable, 'provides the isTraversable function')

t.equal(crocks.eitherToAsync, eitherToAsync, 'provides the eitherToAsync function')
t.equal(crocks.eitherToMaybe, eitherToMaybe, 'provides the eitherToMaybe function')
Expand Down
4 changes: 2 additions & 2 deletions crocks/Arrow.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

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

const isType = require('../internal/isType')
const isSameType = require('../predicates/isSameType')
const _inspect = require('../internal/inspect')

const compose = require('../helpers/compose')
Expand Down Expand Up @@ -37,7 +37,7 @@ function Arrow(runWith) {
constant(`Arrow${_inspect(value())}`)

function concat(m) {
if(!(m && isType(type(), m))) {
if(!(isSameType(Arrow, m))) {
throw new TypeError('Arrow.concat: Arrow required')
}

Expand Down
8 changes: 4 additions & 4 deletions crocks/Async.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const isFunction = require('../predicates/isFunction')

const _inspect = require('../internal/inspect')
const argsArray = require('../internal/argsArray')
const isType = require('../internal/isType')
const isSameType = require('../predicates/isSameType')

const constant = require('../combinators/constant')
const composeB = require('../combinators/composeB')
Expand All @@ -19,7 +19,7 @@ const mreduceMap = require('../helpers/mreduceMap')
const All = require('../monoids/All')

const allAsyncs =
mreduceMap(All, x => isType(Async.type(), x))
mreduceMap(All, x => isSameType(Async, x))

const _type =
constant('Async')
Expand Down Expand Up @@ -163,7 +163,7 @@ function Async(fn) {
var fnDone = false
var valueDone = false

if(!isType(type(), m)) {
if(!isSameType(Async, m)) {
throw new TypeError('Async.ap: Async required')
}

Expand Down Expand Up @@ -203,7 +203,7 @@ function Async(fn) {
fork(reject, function(x) {
const m = fn(x)

if(!isType(type(), m)) {
if(!isSameType(Async, m)) {
throw new TypeError('Async.chain: Function must return another Async')
}

Expand Down
8 changes: 4 additions & 4 deletions crocks/Const.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/** @author Ian Hofmann-Hicks (evil) */

const isFunction = require('../predicates/isFunction')
const isType = require('../internal/isType')
const isSameType = require('../predicates/isSameType')

const _inspect = require('../internal/inspect')

Expand All @@ -17,7 +17,7 @@ function Const(x) {
}

const equals =
m => isType(type(), m) && x === m.value()
m => isSameType(Const, m) && x === m.value()

const inspect =
constant(`Const${_inspect(x)}`)
Expand All @@ -29,7 +29,7 @@ function Const(x) {
_type

function concat(m) {
if(!(m && isType(type(), m))) {
if(!isSameType(Const, m)) {
throw new TypeError('Const.concat: Const required')
}

Expand All @@ -45,7 +45,7 @@ function Const(x) {
}

function ap(m) {
if(!isType(type(), m)) {
if(!isSameType(Const, m)) {
throw new TypeError('Const.ap: Const required')
}

Expand Down
8 changes: 4 additions & 4 deletions crocks/Either.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@

const isApplicative = require('../predicates/isApplicative')
const isFunction = require('../predicates/isFunction')
const isSameType = require('../predicates/isSameType')

const isType = require('../internal/isType')
const _inspect = require('../internal/inspect')
const defineUnion = require('../internal/defineUnion')

Expand Down Expand Up @@ -52,7 +52,7 @@ function Either(u) {
constant(either(identity, identity))

const equals =
m => isType(type(), m) && either(
m => isSameType(Either, m) && either(
constant(m.either(constant(true), constant(false))),
constant(m.either(constant(false), constant(true)))
) && value() === m.value()
Expand Down Expand Up @@ -120,7 +120,7 @@ function Either(u) {
if(!either(constant(true), isFunction)) {
throw new TypeError('Either.ap: Wrapped value must be a function')
}
else if(!either(constant(true), constant(isType(type(), m)))) {
else if(!either(constant(true), constant(isSameType(Either, m)))) {
throw new TypeError('Either.ap: Either required')
}

Expand All @@ -134,7 +134,7 @@ function Either(u) {

const m = either(Either.Left, fn)

if(!(m && isType(type(), m))) {
if(!isSameType(Either, m)) {
throw new TypeError('Either.chain: function must return an Either')
}

Expand Down
4 changes: 2 additions & 2 deletions crocks/IO.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
const isFunction = require('../predicates/isFunction')

const _inspect = require('../internal/inspect')
const isType = require('../internal/isType')
const isSameType = require('../predicates/isSameType')

const composeB = require('../combinators/composeB')
const constant = require('../combinators/constant')
Expand Down Expand Up @@ -38,7 +38,7 @@ function IO(run) {
}

function ap(m) {
if(!isType(type(), m)) {
if(!isSameType(IO, m)) {
throw new TypeError('IO.ap: IO required')
}

Expand Down
8 changes: 4 additions & 4 deletions crocks/Identity.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@

const isApplicative = require('../predicates/isApplicative')
const isFunction = require('../predicates/isFunction')
const isSameType = require('../predicates/isSameType')

const _inspect = require('../internal/inspect')
const isType = require('../internal/isType')

const composeB = require('../combinators/composeB')
const constant = require('../combinators/constant')
Expand All @@ -32,7 +32,7 @@ function Identity(x) {
_of

const equals =
m => isType(type(), m) && x === m.value()
m => isSameType(Identity, m) && x === m.value()

const inspect =
constant(`Identity${_inspect(x)}`)
Expand All @@ -49,7 +49,7 @@ function Identity(x) {
if(!isFunction(x)) {
throw new TypeError('Identity.ap: Wrapped value must be a function')
}
else if(!isType(type(), m)) {
else if(!isSameType(Identity, m)) {
throw new TypeError('Identity.ap: Identity required')
}

Expand All @@ -63,7 +63,7 @@ function Identity(x) {

const m = fn(x)

if(!(m && isType(type(), m))) {
if(!isSameType(Identity, m)) {
throw new TypeError('Identity.chain: function must return an Identity')
}

Expand Down
13 changes: 7 additions & 6 deletions crocks/List.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
const isApplicative = require('../predicates/isApplicative')
const isArray = require('../predicates/isArray')
const isFunction = require('../predicates/isFunction')
const isSameType = require('../predicates/isSameType')

const _inspect = require('../internal/inspect')
const isType = require('../internal/isType')

const constant = require('../combinators/constant')

const _concat = require('../pointfree/concat')

const Maybe = require('./Maybe')
const Pred = require('./Pred')

const Nothing = Maybe.Nothing
const Just = Maybe.Just
Expand Down Expand Up @@ -59,7 +60,7 @@ function List(xs) {
return function(y, x) {
const m = fn(x)

if(!(m && isType(type(), m))) {
if(!isSameType(List, m)) {
throw new TypeError('List.chain: Function must return a List')
}

Expand Down Expand Up @@ -96,7 +97,7 @@ function List(xs) {
x => List([x].concat(xs))

function equals(m) {
if(m && isType(type(), m)) {
if(isSameType(List, m)) {
const mxs = m.value()

return xs.length === mxs.length
Expand All @@ -107,7 +108,7 @@ function List(xs) {
}

function concat(m) {
if(!(m && isType(type(), m))) {
if(!isSameType(List, m)) {
throw new TypeError('List.concat: List required')
}

Expand All @@ -123,7 +124,7 @@ function List(xs) {
}

function filter(pred) {
if(!(isFunction(pred) || isType('Pred', pred))) {
if(!(isFunction(pred) || isSameType(Pred, pred))) {
throw new TypeError('List.filter: Pred or predicate function required')
}

Expand Down Expand Up @@ -151,7 +152,7 @@ function List(xs) {
if(!allFuncs) {
throw new TypeError('List.ap: Wrapped values must be all be functions')
}
else if(!(m && isType(type(), m))) {
else if(!isSameType(List, m)) {
throw new TypeError('List.ap: List required')
}

Expand Down
Loading