These are chat archives for ramda/ramda

Aug 2017
Matthew Willhite
Aug 05 2017 03:35
@Rob0h Interesting…it looks like R.whereEq.length returns 2, which forces the compose to want 2…
const matcher = R.unary(R.compose(R.filter(R.__, sortedData), R.whereEq)); // <= This works
Nicholas Zaleski
Aug 05 2017 05:06
How do you use ramba with AMD? I am new with node and AMD sorry
Dustin Stiles
Aug 05 2017 17:25

Hi guys, I have a question, but I'm not sure if this is the right place, since it isn't really using ramda. Though, when I search for "functional javascript", this seems to be the only reasonable place.

Anyways, I have a use case where I need to pipe some promises together, and build an accumulation array of each intermediate result, so that I can use each "step" as a parameter to some other function.
This is kind of like if pipeP and scan had a baby. I wrote some helpers to get this done, but I'm wondering if it already exists in some way, either in JS or some other language.

I started with a "promisified" version of scan

const scanP = curry((callback, seed, array) =>
  array.reduce((prev, next) =>
    prev.then(acc => callback(last(acc), next)
      .then(v => [...acc, v]))
  , Promise.resolve([seed]), array));

With that, I was able to write pipeScan, which is really just pipe using scanP instead of reduce, which I restricted to one parameter.

const pipeScan = (...functions) => value =>
  scanP((a, b) => b(a), value, functions);

Now I can do something like this

// Each of these depends on the previous one.
const getA = blank => Promise.resolve(blank + 'A');
const getAB = A => Promise.resolve(A + 'B');
const getABC = AB => Promise.resolve(AB + 'C');

// Use pipe scan to see each intermediate value
const main = pipeScan(getA, getB, getC);

   .then(([blank, A, AB, ABC]) => {
    // Do something with all of them.

This is opposed to doing something like this

getA('').then(A =>
  getAB(A).then(AB =>
    getABC(AB).then((ABC) => {
      // Sweet, now I have all three of them!
      // But, this is quite a bit of nesting....

My question is, does this concept of pipeScan already exist?

I'm happy to move this ^^ to SO if that's a better place. I figured this would be the right crowd, apologies in advance if I'm in the wrong place.
Dustin Stiles
Aug 05 2017 17:30
Oh, and I see in error in that scanP, updating now!
Aug 05 2017 17:39
Could commenting code using keyboard shortcuts in ramda repel be added it is kind of annoying when trying out things in the repel
Aadi Deshpande
Aug 05 2017 21:35
Hi, this is probably my lack of understanding, but how can I turn this function int point-free style
const firstElementInPath  = (path) => R.compose(R.defaultTo(null), R.head, R.pathOr(([]), path));

I thought

const firstElementInPath  = R.compose(R.defaultTo(null), R.head, R.pathOr(([]), R.__ ));

would work, but I was wrong.