Support more operators on yslashn?
In some personal projects I've got utils for maybe/either. Now that the API is consolidating it might be nice to extend the stdlib.
Functions for Maybe
:
// Equivalent to sanctuaries `S.maybe`
const discard = (b,a) =>
bifold(() => b, K(a))
const isY =
bifold( () => false, () => true )
// like filter, but doesn't require wrapping `a` in a Maybe initially.
const assert = f => a =>
f(a)
? Y(a)
: N()
const filter = o(chain, assert)
const alt = (Ma, Na) =>
bifold(
// we use fold for type checking
// could just be a `Na` otherwise
() => bifold( Na, () => Na )(Na)
,() => Ma
)(Ma)
const and = (Ma, Na) =>
$T.chain( K(Na) )(Ma)
const toBoolean = isY
const fromNull = Unknown =>
Unknown != null
? Y( Unknown )
: N()
const toNull =
$T.fold( null, i => i )
const swap = b =>
bifold(
() => Y(b)
,() => N()
)
const zero = N()
const join =
bifold(
() => zero()
,bifold( () => zero, Y )
)
const all = Ms =>
Ms.some( M => M._name == 'N' )
? zero()
: Y( Ms.map( M => M.value ) )
const any = Ms =>
Ms.some( M => M._name == 'Y' )
? Y( Ms.filter( x => x.case == 'Y' ).map( M => M.value ) )
: zero()
Either
is much the same, just doesn't need otherwise
args.
tagBy
is a stand out, like assert
for Maybe
.
const tagBy = f => x => f(x) ? Y(x) : N(x)
I think with esm
there's less of a reason to be shy having these built in.