These are chat archives for ramda/ramda

6th
Aug 2016
James Forbes
@JAForbes
Aug 06 2016 09:33

If you've got several layers of monad, e.g. Right(Right(Left('This is an error message'))) is there a polite way to unwrap that to just
Left('This is an error message')

chain/map seems to let you access the value within those layers, but it ultimately returns the same number of layers afterwards.

I suppose each container is there for a reason. And removing them could be considered unsafe. But if there is a Left deep down in several layers of Right it seems relatively useful to be able to identify that easily.

I suppose you could just lie to chain and return the raw value to remove some layers. But that is why I say polite.

Rafe
@rjmk
Aug 06 2016 10:45
@JAForbes There's R.unnest. That just uses your 'impolite' method under the hood, but at least it's a bit of wrapping around it
James Forbes
@JAForbes
Aug 06 2016 13:01
@rjmk thank you, that helps. :)
.chain(R.unnest) is pretty great, sanctuary (for example) will still complain if you unnest too far
James Forbes
@JAForbes
Aug 06 2016 13:46

wait... chain(unnest) is removing 2 levels, so either map(unnest) or chain(I)to remove one level.
Which is right there in the docs

Shorthand for R.chain(R.identity)

So good on me for skimming over that :clap:

But it seems flatten doesn't use chain under the hood. Seems a shame.
James Forbes
@JAForbes
Aug 06 2016 14:16

Is there a util for lifting a list or hash of monads into a shared context?

So this awkward paren monster:

return R.lift(R.zipObj(['cursor', 'size']))([model.cursor, model.size])
  .chain(function({cursor, size}){
     ...
  })

Becomes this tranquil utility

liftObj({ cursor: model.cursor, size: model.size })
  .chain(({cursor,size} => ... )

Or

liftAll([model.cursor, model.size])
   .chain( ([cursor, size]) => ... )
redwyn
@redwyn
Aug 06 2016 14:37
Is it the desired behaviour of set/over/assoc, not to work on objects coming from a mongoose query, either as a whole or as items in a stream?
Because it does not work, which i find peculiar as just normal mutation like ' obj.property = whatever ' on the same things works.
zeecher Game
@zeecher
Aug 06 2016 15:10
This message was deleted
Hi all! How to optimize this:
const a =  "Name: {param1} Surname: {param2}"

const b =  "{Alex},{Grey},";


const splitParams = R.compose(
R.split(','),
R.replace(/{|}/g, '')
)


const replaceParamOne = R.curry((pr, str) => R.test(/{param1}/, str) ? R.replace('{param1}', pr[0], str) : str)

const replaceParamTwo = R.curry((pr, str) => R.test(/{param2}/, str) ? R.replace('{param2}', pr[1], str) : str)

const replaceAllParams = R.curry((pr, str) => R.compose(

  replaceParamTwo(str),
  replaceParamOne(str)

)(pr))


replaceAllParams(a, splitParams(b))


//"Name: Alex Surname: Grey"
Brad Compton (he/him)
@Bradcomp
Aug 06 2016 16:08
@redwyn Mongoose doesn't return normal objects, it has wrappers and methods and all sorts of things that don't play nicely with Ramda. Best to either use .lean() on your queries if you don't need that stuff, or else call toObject() on the mongoose object before passing it into your Ramda function.
Brad Compton (he/him)
@Bradcomp
Aug 06 2016 16:49
@zeecher Here's a way that doesn't require you to create new functions for each parameter: http://goo.gl/4XHdnk
redwyn
@redwyn
Aug 06 2016 17:54
@Bradcomp That did it. Thank you so much!
zeecher Game
@zeecher
Aug 06 2016 18:12
@Bradcomp thanks, learned much from your solution
Brad Compton (he/him)
@Bradcomp
Aug 06 2016 19:22
Awesome!