These are chat archives for ramda/ramda

27th
Oct 2017
Sten-H
@Sten-H
Oct 27 2017 01:33
Is there a short hand for something like this: inc(inc(inc(1))) //4 Something like R.times, but I want it to keep using the return value of previous call
James Forbes
@JAForbes
Oct 27 2017 01:56

Untested, but maybe something like @Sten-H

(n,f) => reduce( compose, times( always( f ), n ))

or

(n,f) => o( apply(compose), times( always(f), n ) )

Might be a better variant but that's what comes to mind for me

James Forbes
@JAForbes
Oct 27 2017 02:06
oh wait there's repeat isn't there!
(n,f) => pipe( ... repeat( f, n ) )
or the less syntaxy: (n,f) => apply(pipe, repeat(f,n))
which could just become... compose( apply(pipe), repeat ) ...maybe
James Forbes
@JAForbes
Oct 27 2017 02:13
wait I'm getting what I think is a bug in the repl: repeat( inc(1) , 3) //=> [2,2,2] but should be repeat( inc(1), 3 ) //=> [inc(1), inc(1), inc(1) ]
wait no I'm thinking of add
Ok this works:
const f = 
  compose( apply(pipe), repeat )

f( add(1), 3 ) (0) //=> 3

f( add(1), 3 ) (1) //=> 4
Charles F. Munat
@chasm
Oct 27 2017 06:45
const f = compose(apply(pipe), repeat)

f(inc, 3)(0) // 3
f(inc, 7)(1) // 8
James Forbes
@JAForbes
Oct 27 2017 07:53
Thanks @chasm forgot to circle back to inc :D
Pygmalion
@PygmalionPolymorph
Oct 27 2017 09:22
Hey folks, i've written a function which unfolds objects, with keys with dots in them becoming nested objects, e.g. {'a.b.c': 1} -> {a: {b: {c: 1}}}
Is there any way this could be simplified?
const unfoldObject = (obj) => {
  const hasDotInKey = R.compose(R.lt(1), R.length, R.split('.'));
  const firstKey = R.compose(R.head, R.split('.'));
  const restKeys = R.compose(R.join('.'), R.slice(1, Infinity), R.split('.'));
  const currentValue = R.prop(R.__, obj);
  const newObject = R.converge(R.partialRight(R.assoc, [{}]));

  return R.mergeAll(R.map(R.ifElse(
      hasDotInKey,
      newObject([firstKey, R.compose(unfoldObject, newObject([restKeys,  currentValue]))]),
      newObject([R.identity, currentValue])
  ), R.keys(obj)))
};
Philipp Wille
@Yord
Oct 27 2017 10:54
@PygmalionPolymorph How about:
const unfoldObject = o(
  reduce((acc, [key, value]) => merge(
    acc,
    o(reduceRight(objOf, value), split('.'))(key)
  ), {}),
  toPairs
)

unfoldObject({'a.b.c': 1}) // {a: {b: {c: 1}}}
unfoldObject({'a.b.c': 1, 'd.e.f': 2}) // {a: {b: {c: 1}}, d: {e: {f: 2}}}
unfoldObject({'a.b.c': 1, 'd.e.f': 2, 'g': 3}) // {a: {b: {c: 1}}, d: {e: {f: 2}}, g: 3}
Pygmalion
@PygmalionPolymorph
Oct 27 2017 11:15
@Yord Wow, that's much more elegant! Thanks a lot. :)
Sten-H
@Sten-H
Oct 27 2017 19:07
@JAForbes @chasm Thanks a lot guys! So the apply(pipe) part basically wraps the array of functions that repeat creates in a pipe if I understand correctly?