These are chat archives for ramda/ramda

25th
Jul 2015
David Chambers
@davidchambers
Jul 25 2015 00:43
How does one “lift” a function of type a -> b -> Maybe c?
R.lift is close, but will give me a function of type Maybe a -> Maybe b -> Maybe (Maybe c).
Scott Christopher
@scott-christopher
Jul 25 2015 01:50
@davidchambers what do you expect the signature to be? Something like the following?
(a -> b -> Maybe c) -> Maybe a -> Maybe b -> Maybe c
Scott Christopher
@scott-christopher
Jul 25 2015 02:20
safeDiv = R.curry((a, b) => b !== 0 ? Just(a / b) : Nothing);
maybeSafeDiv = R.compose(R.chain(R.identity), R.lift(safeDiv));

maybeSafeDiv(Just(10), Just(2)); //=> Just(5)
maybeSafeDiv(Just(10), Just(0)); //=> Nothing
maybeSafeDiv(Nothing, Just(2)); //=> Nothing
David Chambers
@davidchambers
Jul 25 2015 02:22
Thanks, @scott-christopher. I ended up using R.chain(R.identity) (AKA R.unnest). I was wondering whether there’s a nicer solution.
Scott Sauyet
@CrossEye
Jul 25 2015 02:24
I was playing with R.chain(R.identity) (and already forgetting unnest!) but it wasn't yet as clean as this.
Scott Christopher
@scott-christopher
Jul 25 2015 02:29
Does unnest delegate for monads, or is it specialised to lists?
Jethro Larson
@jethrolarson
Jul 25 2015 02:31
R.flatten don't do it?
David Chambers
@davidchambers
Jul 25 2015 03:02
@scott-christopher, ramda/ramda#1303 changed the implementation to chain(_identity) to support things other than lists.
@jethrolarson, it’d be cool if R.flatten(Just(Just(Just(42)))) evaluated to Just(42), but it doesn’t currently.