These are chat archives for ramda/ramda

17th
Jun 2015
David Chambers
@davidchambers
Jun 17 2015 17:04
Is plaid/sanctuary#53 something we should consider for Ramda? The function can be defined exclusively in terms of Ramda functions, so it doesn’t seem too much of a stretch:
// $do :: Monad m => [(a -> m b), (b -> m c), ..., (y -> m z)] -> m a -> m z
const $do = R.pipe(R.map(R.chain), R.apply(R.pipe));
Raine Virta
@raine
Jun 17 2015 17:05
Does do go by other names?
I just saw this the other day, https://www.npmjs.com/package/delve
clearly not the same idea but solving the same problem
David Chambers
@davidchambers
Jun 17 2015 17:09
I don’t think so. delve makes it possible to chain one kind of operation which may fail: property access. R.chain allows chaining of any kinds of operation which may fail, provided they’re in the same Monad.
For example, what if rather than taking an object as input we were to take a string of (potentially invalid) JSON? delve has nothing to say about that: we’d be forced to invoke JSON.parse inside a try block.
With $do it’s trivial to accommodate this new requirement:
// safe5 :: Maybe String -> Maybe Number
const safe5 = $do([
  S.parseJson,
  S.get('a'),
  S.get('b'),
  S.get('c'),
  S.parseFloat,
  R.compose(S.Just, Math.sqrt),
]);
> safe5(S.Just('{"a":{"b":{"c":"2.25"}}}'))
Just(1.5)
> safe5(S.Just('[Invalid JSON]'))
Nothing()
Raine Virta
@raine
Jun 17 2015 17:16

delve has nothing to say about that

indeed no, but it would be helpful with the first example

compose(Maybe, delve)
David Chambers
@davidchambers
Jun 17 2015 17:16
Indeed, though it doesn’t handle the String -> Number operation which may also fail.
> naive({a: {b: {c: 'blah'}}})
NaN
Raine Virta
@raine
Jun 17 2015 17:18
i see the potential
David Chambers
@davidchambers
Jun 17 2015 17:19
I think using one specific example may have been a bad idea. I’ll tweak the post. Thank you for your feedback, @raine! I didn’t mean to be dismissive.
Raine Virta
@raine
Jun 17 2015 17:22
you're welcome