These are chat archives for ramda/ramda

19th
Aug 2016
James Forbes
@JAForbes
Aug 19 2016 02:18
hey all, any recommended reading on lenses?
Scott Christopher
@scott-christopher
Aug 19 2016 02:32
@JAForbes: If you don't mind reading Haskell, this tutorial is quite good: https://hackage.haskell.org/package/lens-tutorial-1.0.1/docs/Control-Lens-Tutorial.html
Otherwise I'd be happy to answer any questions you may have.
You could potentially even skip over the code in that link if you're not too familiar with Haskell syntax and just read the text in between and it would be useful.
James Forbes
@JAForbes
Aug 19 2016 02:50

Thanks @scott-christopher !

Happy to read Haskell, even if I'm not too familiar with it yet

James Forbes
@JAForbes
Aug 19 2016 03:19

@scott-christopher

Say you had a nested structure, where each level was wrapped in some container. It could be a Future

So

Future.of({
  a: Future.of({
    b: Future.of({
       c: Future.of(4)
    })
  })
})

I know you could define a lens specific to that structure. Something like R.lens( a => a.chain( a => a.b.chain( b => b.c.chain(identity) ) ), ... )

But is there a way to just use R.path(['a','b','c']) or compose( prop('c'),prop('b'),prop('a')) with a R.over to achieve the same thing?

So that if the structure changes, you don't need to define a completely new lens?

That lens I posted is completely wrong, but hopefully you understand what I mean
Scott Christopher
@scott-christopher
Aug 19 2016 04:08
@JAForbes You'll be able to create something that is a Setter, but not a Getter. So you'll be able to compose these Setters together with other lenses, but you'll only be able to use set and over with it, not view.
You can see the family tree of these things (sometimes known as optics) here: http://i.imgur.com/ALlbPRa.png
I've got meetings for the rest of the afternoon :( but I'll see if I can get an example together a little later.
James Forbes
@JAForbes
Aug 19 2016 04:32
thanks @scott-christopher :)
Scott Christopher
@scott-christopher
Aug 19 2016 05:17
@JAForbes something like this:
// a `Setter` optic that can update the values within a functor and
// can be composed with other lenses
const mapped = toFunctorFn =>
  compose(Identity.of, map(compose(x => x.value, toFunctorFn)))

// creates a `Setter` that updates the values within the functor at the given prop
const mappedProp = prop =>
  compose(lensProp(prop), mapped)

// a setter that focusses on the nested functor at `a` => `b` => `c`.
const abcSetter =
  compose(mapped, mappedProp('a'), mappedProp('b'), mappedProp('c'))

// some utils for flattening out the structure
const joinProp = p => m => chain(prop(p), m)
const flattenEm = pipe(joinProp('a'), joinProp('b'), joinProp('c'))

const nestedFuture = Future.of({
  a: Future.of({
    b: Future.of({
      c: Future.of(4)
    })
  })
});

// A nested future with the value focussed by `abcSetter` incremented by 1
const incremented = over(abcSetter, inc, nestedFuture)

// Flatten then fork the future
flattenEm(incremented).fork(console.log, console.log) //=> 5
James Forbes
@JAForbes
Aug 19 2016 06:38
@scott-christopher Thank you, I'll re-read this a few hundred times until it clicks :D
Denis Stoyanov
@xgrommx
Aug 19 2016 11:59
anybody know how will be equivalent expression in Ramda for haskell expression with left section? ((:) .)
claudiotx
@claudiotx
Aug 19 2016 12:05

Good afternoon channel. :D
I've got a quick question.

If I need to use a promise anywhere inside a R.compose, I 'm currently returning promises in all functions until the end of the chain, so that I can have the payload of the composition assigned to the variable holding the call to the R.compose function (ie. all chained functions will interface with a promise).

const asyncComposition = R.compose(
   fnc3, //returns the fnc1Async promise,  performs data operations on then()
   fnc2, //returns the fnc1Async promise, performs data operations on then()
   fnc1Async //returns a promise
);

// check the promise then
asyncComposition.then(...)

Gets quite messy.

Is there any way that I could avoid passing a promise through all the functions to the end of the chain?
A quick example will be highly appreciated.

Please let me know.

Cheers

Denis Stoyanov
@xgrommx
Aug 19 2016 12:07
@claudiotx use composeP
Denis Stoyanov
@xgrommx
Aug 19 2016 12:15
@claudiotx general version composeK what is kleisli composition, but you need add synonym for Promise.prototype.chain = Promise.prototype.then
claudiotx
@claudiotx
Aug 19 2016 12:21
@xgrommx Do you have a quick example of the composeP?
actually just go it working :D
cheers mate
Drew
@dtipson
Aug 19 2016 18:13
composeP for promises is essentially just reducing a list of promise--or-value-returning functions with (p,fn)=>p.then(fn), right?
Drew
@dtipson
Aug 19 2016 18:27
that's the 2 func case, right?
James Forbes
@JAForbes
Aug 19 2016 22:38

@claudiotx I quite like this:

var async =  f  => p => p.then(f)


var f = pipe(
  ,Promise.resolve
  ,async( R.toUppercase )
  ,async( R.reverse )
)

f('hello') //=> 'OLLEH' : Promise String

But pipeP, composeP works great as well as others have said. And if you can use something like a future, you can replace async with R.chain or R.map

Brad Compton (he/him)
@Bradcomp
Aug 19 2016 22:43
@JAForbes I like that too!
James Forbes
@JAForbes
Aug 19 2016 23:04
:)