These are chat archives for ramda/ramda

26th
Sep 2017
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 01:14
@guidorice Can you create an example of the difference? What browser are you using?
Jonah
@jonahx
Sep 26 2017 08:22
does ramda have a “power” function, as in “apply function fn N number of times”. eg, power(2, double, 3) === 12 (double(double(3)))
Vasili Sviridov
@vsviridov
Sep 26 2017 09:18
const power = (n, fn) => compose(…range(1,n).map(always(fn))?
Denis Stoyanov
@xgrommx
Sep 26 2017 09:24
const power = curry((n, f, x) => reduce(o, identity, repeat(f, n))(x))
Vasili Sviridov
@vsviridov
Sep 26 2017 09:27
what’s o?
wasn’t aware of repeat, cerainly nicer than range/always
ah, the o function… is it preferable to compose in this case?
Kurt Milam
@kurtmilam
Sep 26 2017 09:33
I prefer o whenever I'm only composing two functions and don't need the result to be curried, and I prefer pipe, otherwise.
Vasili Sviridov
@vsviridov
Sep 26 2017 09:33
right...
still a bit unclear on how identity works as a second argument to reduce, doesn’t it usually expect the initial value? I guess since the output is a function we get a …[identity, fn, fn, fn] so it short-circuits itself, effectively
Denis Stoyanov
@xgrommx
Sep 26 2017 09:35
:smile:
const o2 = o(o)(o)
const power2 = o2(reduceRight(o, identity))(flip(repeat))

power2(5)(double)(2)
Vasili Sviridov
@vsviridov
Sep 26 2017 09:35
woudn’t it be better to do reduce(o, f, repeat(f, n - 1)) to avoid extra stack frame
^ ok, that’s black magic to me right now :D
Denis Stoyanov
@xgrommx
Sep 26 2017 10:13
:smile:
const iterate = curry(function * (f, a) {
  yield a
  // noprotect
  for(;;) {
    a = f(a)
    yield a
  }
})

const get_ = curry(function(n, stream) {
  let i = 0;
  for(let x of stream) {
    if(++i === n) {
      return x
    }
  }
})

const power = n => f => x => get_(n, iterate(f, x))

power(2)(x => x * 10)(1)
Vasili Sviridov
@vsviridov
Sep 26 2017 10:42
why am I reading on Monoidal Contravariant Functors at 4 am…? :D
Kurt Milam
@kurtmilam
Sep 26 2017 10:45
Sounds like you've been nerd sniped
Julio Borja Barra
@juboba
Sep 26 2017 12:01
xDD
guys, I know it may be simple, but I've been looking at this for a while and decided to ask.
I want to do the following in a cleaner way:
  const mergeFilters = (dest, src) => {
    if (src.active) {
      return pipe(
        merge(dest),
        omit(['archived', 'mine'])
      )(src)
    }

    if (src.archived) {
      return pipe(
        merge(dest),
        omit(['active', 'mine'])
      )(src)
    }

    if (src.mine) {
      return pipe(
        merge(dest),
        omit(['active', 'archived'])
      )(src)
    }

    return merge(dest, src)
  }
Kurt Milam
@kurtmilam
Sep 26 2017 12:13
@juboba do you have a little sample data?
Kurt Milam
@kurtmilam
Sep 26 2017 12:26
@juboba does this do the trick?
Kurt Milam
@kurtmilam
Sep 26 2017 13:24
@juboba I missed that last return if no matching keys were found. this should clear that up.
arian‮
@arian-swydo
Sep 26 2017 13:34
beautiful
Kurt Milam
@kurtmilam
Sep 26 2017 13:37
@juboba Slight improvement (don't call reduce if there are no matching keys).
@arian-swydo Thanks!
Kurt Milam
@kurtmilam
Sep 26 2017 13:50
@juboba last one - I got rid of hasMatchingKeys in this one.
Casey Link
@Ramblurr
Sep 26 2017 14:58
fighting with closure compiler to get ramda to work..
such a pain
Jonah
@jonahx
Sep 26 2017 16:21
@xgrommx thanks

i’m on three hours sleep, someone help see why my point free version isn’t the same as the point version in these two snippets:

//works, point version
var next = R.chain(x => {
  return R.map(R.concat(x), neighbors[R.last(x)])
})

// doesn’t work the same, but seems on no sleep like it should :(
var next = 
  R.chain(R.converge(R.map, [R.concat, R.pipe(R.last, R.nth(R.__, neighbors))]))

Working example: https://goo.gl/ZiX9jx

Brad Compton (he/him)
@Bradcomp
Sep 26 2017 16:35
concat is a binary function, so when it is used in converge it will make converge a binary function
@ram-bot
const neighbors = R.map(R.map(R.toString), {
  '0':[0,1,2],
  '1':[0,1,2,4],
  '2':[0,1,2,3,5],
  '3':[2,3,6],
  '4':[1,4,5,7],
  '5':[2,4,5,6,8],
  '6':[3,5,6,9],
  '7':[4,7,8],
  '8':[5,7,8,9],
  '9':[6,8,9]
})

var currConcat = (x) => (y) => x.concat(y)
var next2 = 
  R.chain(R.converge(R.map, [currConcat, R.pipe(R.last, R.nth(R.__, neighbors))]))


  next2(['0', '1'])
ram-bot
@ram-bot
Sep 26 2017 16:37
[ '00', '01', '02', '10', '11', '12', '14' ]
Jonah
@jonahx
Sep 26 2017 16:38
@Bradcomp tyvm, sir
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 16:38
converge takes on the arity of the highest arity function in the branching functions
Jonah
@jonahx
Sep 26 2017 16:38
right, i get it
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 16:38
:bowtie:
Jonah
@jonahx
Sep 26 2017 16:38
@Bradcomp ha, he matches your pic!
Denis Stoyanov
@xgrommx
Sep 26 2017 16:39
var next2 = chain(lift(map)(x => concat(x), compose(flip(nth)(neighbors), last)))
some problem with pf concat :smile:
Jonah
@jonahx
Sep 26 2017 16:42
@xgrommx, nice.
Rupesh Tiwari
@roopkt
Sep 26 2017 20:09
const someEvaluatedBoolean = false;
ifElse(()=>someEvaluatedBoolean, ()=>'yes', ()=> 'no' )();  //this works
ifElse( someEvaluatedBoolean, ()=>'yes', ()=> 'no' )(); //not works
is there any option to convert boolean into a function without writing expression ?
Vasili Sviridov
@vsviridov
Sep 26 2017 20:11
always(someEvaluatedBoolean)
Rupesh Tiwari
@roopkt
Sep 26 2017 20:14
@vsviridov Excellent thanks ! got it working with always :)
const someEvaluatedBoolean = false;
ifElse(always(someEvaluatedBoolean), ()=>'yes', ()=> 'no' )();
BobbieBarker
@BobbieBarker
Sep 26 2017 22:41
kind of a lame question, but how does one import the placeholder __ in typescript ?
i'm geting an error claiming that ramda has no exported member __
Neeraj Singh
@neerajdotname_twitter
Sep 26 2017 23:20
Folks I have been using Ramda for only a week so I might be missing something. I tried to make a function pointfree and I'm running into memory issue. Before and after code is here https://jsfiddle.net/neerajdotname/zo6ggwcj/
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 23:23
@neerajdotname_twitter What do the two functions in the middle look like?
Neeraj Singh
@neerajdotname_twitter
Sep 26 2017 23:24
@Bradcomp I have updated. new url is https://jsfiddle.net/neerajdotname/zo6ggwcj/1/
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 23:30
const whiteListAttributes = whiteList =>
  R.reduce((acc, attr) => {
    if (R.contains(attr.nodeName, whiteList)) {
      acc.push(attr);
    }
    return acc;
  }, []);

Your reducing function is mutating the accumulator instead of returning a new one.

When you wrap the main function in a lambda it creates a new whiteListAttributes each time it runs. When it's point free it uses the same function. That means acc just gets bigger and bigger each time it's called

try:

const whiteListAttributes = whiteList =>
  R.reduce((acc, attr) => R.contains(attr.nodeName, whiteList) ? R.append(attr, acc) : acc, []);
I typed that in here, hasn't been tested so there may be some typos
Neeraj Singh
@neerajdotname_twitter
Sep 26 2017 23:31
That makes sense. I'll try it out.
Thanks a bunch. Really appreciate it.
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 23:31
:bowtie:
Neeraj Singh
@neerajdotname_twitter
Sep 26 2017 23:35
Sweet that worked. Thanks again @Bradcomp
Brad Compton (he/him)
@Bradcomp
Sep 26 2017 23:36
:+1: