In order to understand the examples in this book, I usually console.log the compositions in node but whenever an ADT encapsulates another ADT, I get confusing output. I sometimes thought I was looking at a bug but this PR will fix that :)
Not only that but it will fix a case that currently will throw a TypeError.
// Without this PR, the following will throw:
// TypeError: Cannot convert undefined or null to object
console.log(
new List([Maybe.of(1), Maybe.of(null)]), // List([Just(1), Nothing])
)
Here is what I used to test the improved inspect function with:
'use strict'
const { Identity, Either, List, Left, Right, Maybe, Task, Map, IO,
curry, compose, sequence, map, traverse } = require('@mostly-adequate/support')
const identity1 = compose(sequence(Identity.of), map(Identity.of))
const identity2 = Identity.of
// fromPredicate :: (a -> Bool) -> a -> Either e a
const fromPredicate = curry((predicate, a) => predicate(a)
? new Right(a)
: new Left("predicate failed"))
// partition :: (a -> Bool) -> [a] -> [Either e a]
const partition = f => map(fromPredicate(f))
// validate :: (a -> Bool) -> [a] -> Either e [a]
const validate = f => traverse(Either.of, fromPredicate(f))
// isEven :: Number -> Boolean
const isEven = n => n % 2 === 0
const array = [1,2,3,4,5]
console.log(
// test it out with Right
identity1(Either.of('stuff')), // Requires #605 - missing return in Right.traverse
// Identity(Right('stuff'))
identity2(Either.of('stuff')),
// Identity(Right('stuff'))
)
console.log(
partition(isEven)(new List(array)),
// List([Left('predicate failed'), Right(2), Left('predicate failed'), Right(4), Left('predicate failed')])
)
console.log(
Either.of(Identity.of('stuff')), // Right(Identity('stuff'))
Maybe.of(IO.of('something')), // Just(IO(?))
)
In order to understand the examples in this book, I usually
console.log
the compositions in node but whenever an ADT encapsulates another ADT, I get confusing output. I sometimes thought I was looking at a bug but this PR will fix that :)Not only that but it will fix a case that currently will throw a
TypeError
.Here is what I used to test the improved
inspect
function with:Currently the output will be:
Contrast that with the following, with this patch applied:
If both PR #605 and #606 is applied then the following is also possible (and
identity1(Either.of('stuff'))
is no longerundefined
):The output is now:
That is all of the ADT's except
Compose
which is in a closure, so I can not pattern match on theCompose
constructor at the moment.