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

Add the Result Crock #100

Merged
merged 1 commit into from
Mar 7, 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
34 changes: 20 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ All `Crocks` are Constructor functions of the given type, with `Writer` being an
| `Pair` | --- | `ap`, `bimap`, `chain`, `concat`, `equals`, `fst`, `map`, `merge`, `of`, `snd`, `swap` |
| `Pred` * | `empty` | `concat`, `contramap`, `empty`, `runWith`, `value` |
| `Reader` | `ask`, `of`| `ap`, `chain`, `map`, `of`, `runWith` |
| `Result` | `Err`, `Ok`, `of`| `alt`, `ap`, `bimap`, `chain`, `coalesce`, `concat`, `either`, `equals`, `map`, `of`, `sequence`, `swap`, `traverse` |
| `Star` | -- | `both`, `concat`, `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` |
Expand Down Expand Up @@ -302,39 +303,39 @@ These functions provide a very clean way to build out very simple functions and
##### Datatypes
| Function | Datatypes |
|---|:---|
| `alt` | `Async`, `Either`, `Maybe` |
| `ap` | `Async`, `Const`, `Either`, `Identity`, `IO`, `List`, `Maybe`, `Pair`, `Reader`, `State`, `Unit`, `Writer` |
| `bimap` | `Async`, `Either`, `Pair` |
| `alt` | `Async`, `Either`, `Maybe`, `Result` |
| `ap` | `Async`, `Const`, `Either`, `Identity`, `IO`, `List`, `Maybe`, `Pair`, `Reader`, `Result`, `State`, `Unit`, `Writer` |
| `bimap` | `Async`, `Either`, `Pair`, `Result` |
| `both` | `Arrow`, `Function`, `Star` |
| `chain` | `Async`, `Const`, `Either`, `Identity`, `IO`, `List`, `Maybe`, `Pair`, `Reader`, `State`, `Unit`, `Writer` |
| `coalesce` | `Async`, `Maybe`, `Either` |
| `concat` | `All`, `Any`, `Array`, `Arrow`, `Assign`, `Const`, `Either`, `Identity`, `List`, `Max`, `Maybe`, `Min`, `Pair`, `Pred`, `Prod`, `Star`, `String`, `Sum`, `Unit` |
| `chain` | `Async`, `Const`, `Either`, `Identity`, `IO`, `List`, `Maybe`, `Pair`, `Reader`, `Result`, `State`, `Unit`, `Writer` |
| `coalesce` | `Async`, `Either`, `Maybe`, `Result` |
| `concat` | `All`, `Any`, `Array`, `Arrow`, `Assign`, `Const`, `Either`, `Identity`, `List`, `Max`, `Maybe`, `Min`, `Pair`, `Pred`, `Prod`, `Result`, `Star`, `String`, `Sum`, `Unit` |
| `cons` | `Array`, `List` |
| `contramap` | `Arrow`, `Pred`, `Star` |
| `either` | `Either`, `Maybe` |
| `either` | `Either`, `Maybe`, `Result` |
| `evalWith` | `State` |
| `execWith` | `State` |
| `filter` | `Array`, `List` |
| `first` | `Arrow`, `Function`, `Star` |
| `fst` | `Pair` |
| `head` | `Array`, `List` |
| `head` | `Array`, `List`, `String` |
| `log` | `Writer` |
| `map` | `Async`, `Array`, `Arrow`, `Const`, `Either`, `Function`, `Identity`, `IO`, `List`, `Maybe`, `Pair`, `Reader`, `Star`, `State`, `Unit`, `Writer` |
| `map` | `Async`, `Array`, `Arrow`, `Const`, `Either`, `Function`, `Identity`, `IO`, `List`, `Maybe`, `Pair`, `Reader`, `Result`, `Star`, `State`, `Unit`, `Writer` |
| `merge` | `Pair` |
| `option` | `Either`, `Maybe` |
| `option` | `Maybe` |
| `promap` | `Arrow`, `Star` |
| `read` | `Writer` |
| `reduce` | `Array`, `List` |
| `reject` | `Array`, `List` |
| `run` | `IO` |
| `runWith` | `Arrow`, `Pred`, `Reader`, `Star`, `State` |
| `second` | `Arrow`, `Function`, `Star` |
| `sequence` | `Array`, `Either`, `Identity`, `List`, `Maybe` |
| `sequence` | `Array`, `Either`, `Identity`, `List`, `Maybe`, `Result` |
| `snd` | `Pair` |
| `swap` | `Async`, `Either`, `Pair` |
| `swap` | `Async`, `Either`, `Pair`, `Result` |
| `tail` | `Array`, `List`, `String` |
| `traverse` | `Array`, `Either`, `Identity`, `List`, `Maybe` |
| `value` | `Arrow`, `Const`, `Identity`, `List`, `Pred`, `Unit`, `Writer` |
| `traverse` | `Array`, `Either`, `Identity`, `List`, `Maybe`, `Result` |
| `value` | `Const`, `Identity`, `Pred`, `Unit`, `Writer` |

### Transformation Functions
Transformation functions are mostly used to reduce unwanted nesting of similar types. Take for example the following structure:
Expand Down Expand Up @@ -452,6 +453,11 @@ bad
| `arrayToList` | `[ a ] -> List a` | `(a -> [ b ]) -> a -> List b` |
| `eitherToAsync` | `Either e a -> Async e a` | `(a -> Either e b) -> a -> Async e b` |
| `eitherToMaybe` | `Either b a -> Maybe a` | `(a -> Either c b) -> a -> Maybe b` |
| `eitherToResult` | `Either e a -> Result e a` | `(a -> Either e b) -> a -> Result e b` |
| `listToArray` | `List a -> [ a ]` | `(a -> List b) -> a -> [ b ]` |
| `maybeToAsync` | `e -> Maybe a -> Async e a` | `e -> (a -> Maybe b) -> a -> Async e b` |
| `maybeToEither` | `c -> Maybe a -> Either c a` | `c -> (a -> Maybe b) -> a -> Either c b` |
| `maybeToResult` | `c -> Maybe a -> Result c a` | `c -> (a -> Maybe b) -> a -> Result c b` |
| `resultToAsync` | `Result e a -> Async e a` | `(a -> Result e b) -> a -> Async e b` |
| `resultToEither` | `Result e a -> Either e a` | `(a -> Result e b) -> a -> Either e b` |
| `resultToMaybe` | `Result b a -> Maybe a` | `(a -> Result c b) -> a -> Maybe b` |
10 changes: 9 additions & 1 deletion crocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const crocks = {
Pair: require('./crocks/Pair'),
Pred: require('./crocks/Pred'),
Reader: require('./crocks/Reader'),
Result: require('./crocks/Result'),
Star: require('./crocks/Star'),
State: require('./crocks/State'),
Unit: require('./crocks/Unit'),
Expand Down Expand Up @@ -132,10 +133,17 @@ const predicates = {
}

const transforms = {
arrayToList: require('./transforms/arrayToList'),
eitherToAsync: require('./transforms/eitherToAsync'),
eitherToMaybe: require('./transforms/eitherToMaybe'),
eitherToResult: require('./transforms/eitherToResult'),
listToArray: require('./transforms/listToArray'),
maybeToAsync: require('./transforms/maybeToAsync'),
maybeToEither: require('./transforms/maybeToEither')
maybeToEither: require('./transforms/maybeToEither'),
maybeToResult: require('./transforms/maybeToResult'),
resultToAsync: require('./transforms/resultToAsync'),
resultToEither: require('./transforms/resultToEither'),
resultToMaybe: require('./transforms/resultToMaybe')
}

module.exports = Object.assign(
Expand Down
16 changes: 16 additions & 0 deletions crocks.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ const Maybe = require('./crocks/Maybe')
const Pair = require('./crocks/Pair')
const Pred = require('./crocks/Pred')
const Reader = require('./crocks/Reader')
const Result = require('./crocks/Result')
const Star = require('./crocks/Star')
const State = require('./crocks/State')
const Unit = require('./crocks/Unit')
Expand Down Expand Up @@ -119,10 +120,17 @@ const isSetoid = require('./predicates/isSetoid')
const isString = require('./predicates/isString')
const isTraversable = require('./predicates/isTraversable')

const arrayToList = require('./transforms/arrayToList')
const eitherToAsync = require('./transforms/eitherToAsync')
const eitherToMaybe = require('./transforms/eitherToMaybe')
const eitherToResult = require('./transforms/eitherToResult')
const listToArray = require('./transforms/listToArray')
const maybeToAsync = require('./transforms/maybeToAsync')
const maybeToEither = require('./transforms/maybeToEither')
const maybeToResult = require('./transforms/maybeToResult')
const resultToAsync = require('./transforms/resultToAsync')
const resultToEither = require('./transforms/resultToEither')
const resultToMaybe = require('./transforms/resultToMaybe')

test('entry', t => {
t.equal(crocks.toString(), '[object Object]', 'is an object')
Expand Down Expand Up @@ -201,6 +209,7 @@ test('entry', t => {
t.equal(crocks.Pair, Pair, 'provides the Pair function')
t.equal(crocks.Pred, Pred, 'provides the Pred function')
t.equal(crocks.Reader, Reader, 'provides the Reader function')
t.equal(crocks.Result, Result, 'provides the Result function')
t.equal(crocks.Star, Star, 'provides the Star function')
t.equal(crocks.State, State, 'provides the State function')
t.equal(crocks.Unit, Unit, 'provides the Unit function')
Expand Down Expand Up @@ -244,10 +253,17 @@ test('entry', t => {
t.equal(crocks.isString, isString, 'provides the isString function')
t.equal(crocks.isTraversable, isTraversable, 'provides the isTraversable function')

t.equal(crocks.arrayToList, arrayToList, 'provides the arrayToList function')
t.equal(crocks.eitherToAsync, eitherToAsync, 'provides the eitherToAsync function')
t.equal(crocks.eitherToMaybe, eitherToMaybe, 'provides the eitherToMaybe function')
t.equal(crocks.eitherToResult, eitherToResult, 'provides the eitherToResult function')
t.equal(crocks.listToArray, listToArray, 'provides the listToArray function')
t.equal(crocks.maybeToAsync, maybeToAsync, 'provides the maybeToAsync function')
t.equal(crocks.maybeToEither, maybeToEither, 'provides the maybeToEither function')
t.equal(crocks.maybeToResult, maybeToResult, 'provides the maybeToResult function')
t.equal(crocks.resultToAsync, resultToAsync, 'provides the resultToAsync function')
t.equal(crocks.resultToEither, resultToEither, 'provides the resultToEither function')
t.equal(crocks.resultToMaybe, resultToMaybe, 'provides the resultToMaybe function')

t.end()
})
4 changes: 2 additions & 2 deletions crocks/Either.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */

const ifElse = require('../logic/ifElse')
const isApplicative = require('../predicates/isApplicative')
const isFunction = require('../predicates/isFunction')
const isSameType = require('../predicates/isSameType')
Expand Down Expand Up @@ -43,8 +44,7 @@ function Either(u) {
throw new TypeError('Either: Must wrap something, try using Left or Right constructors')
}

const x = (u && isFunction(u.tag) && (u.tag() === 'Left' || u.tag() === 'Right'))
? u : Right(u)
const x = ifElse(_either.includes, identity, Right, u)

const type =
_type
Expand Down
2 changes: 1 addition & 1 deletion crocks/Either.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,7 @@ test('Either chain properties (Chain)', t => {
t.ok(isFunction(Right(0).ap), 'Right implements the Apply spec')

t.ok(isFunction(Left(0).chain), 'Left provides a chain function')
t.ok(isFunction(Left(0).ap), 'Leftimplements the Apply spec')
t.ok(isFunction(Left(0).ap), 'Left implements the Apply spec')

const f = x => Right(x + 2)
const g = x => Right(x + 10)
Expand Down
4 changes: 2 additions & 2 deletions crocks/Maybe.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @license ISC License (c) copyright 2016 original and current authors */
/** @author Ian Hofmann-Hicks (evil) */

const ifElse = require('../logic/ifElse')
const isApplicative = require('../predicates/isApplicative')
const isFunction = require('../predicates/isFunction')
const isSameType = require('../predicates/isSameType')
Expand Down Expand Up @@ -38,8 +39,7 @@ function Maybe(u) {
throw new TypeError('Maybe: Must wrap something, try using Nothing or Just constructors')
}

const x = (u && isFunction(u.tag) && (u.tag() === 'Nothing' || u.tag() === 'Just'))
? u : Just(u)
const x = ifElse(_maybe.includes, identity, Just, u)

const of =
_of
Expand Down
Loading