These are chat archives for ramda/ramda

9th
Dec 2015
Jethro Larson
@jethrolarson
Dec 09 2015 02:16
Anyone else think this is useful?
const findLens = pred => R.lens(
  R.find(pred)
  , (v, ar) => R.adjust(R.always(v), R.findIndex(pred, ar), ar)
)
BTW I was looking for something to replace a value at an index in array and I didn't see anything in ramda. R.adjust(R.always(x) was the closest I found
David Chambers
@davidchambers
Dec 09 2015 02:50
@jethrolarson, it sounds as though R.update fits the bill.
Niloy Mondal
@niloy
Dec 09 2015 08:33
is there a version of splitEvery that works with predicate?
Tobias Pflug
@gilligan
Dec 09 2015 08:42
splitWhen has been or will be merged to master soon I think
Niloy Mondal
@niloy
Dec 09 2015 08:43
ok
Asaf
@asaf-romano
Dec 09 2015 09:08
Any ideas on how to write the lamda here in point-free style: http://bit.ly/1PYf0ul ?
I was messing around with converge, zipObj and unapply an flip, but things got nasty when I had to unapply everything: zipObj, difference and flip(difference)
Jayson Harshbarger
@Hypercubed
Dec 09 2015 09:52
Does anyone know of a better way to write this (_) => R.comparator(R.useWith(_, [_getter, _getter])). I want to take a binary function and return a new binary function that first applies _getter to each argument.
Tobias Pflug
@gilligan
Dec 09 2015 09:59
Hm, i am somehow stuck right now - I want to write a function that wraps functions which might throw and returns either Right(result) or Left(errorObject) : https://gist.github.com/gilligan/66a1ac0431a0e77fdaae - this is not quite right. Can anyone help out ?
Tobias Pflug
@gilligan
Dec 09 2015 10:04
@raine ah yes right
driving me bonkers that i am not getting my stuff right though
Tobias Pflug
@gilligan
Dec 09 2015 10:10
wrapEither(R.inc)(0); // --> Either { value: '1[object Object]' }
x__X
Tobias Pflug
@gilligan
Dec 09 2015 10:15
D'OH
arguments is not available in arrow functions
Stefano Vozza
@svozza
Dec 09 2015 10:21
probably best off using rest parameters anyway
Jayson Harshbarger
@Hypercubed
Dec 09 2015 10:45
FYI... I'm working on replacing an utility library I created a few years ago with a Ramda based version: https://github.com/Hypercubed/florida
It's kind of like a hyperactive version of a lens... but only the getter. Any feedback would be appreciated.
Hardy Jones
@joneshf
Dec 09 2015 12:41
@Hypercubed it sounds like you want something like: http://hackage.haskell.org/package/base-4.8.1.0/docs/Data-Function.html#v:on
Given the type (b -> b -> c) -> (a -> b) -> a -> a -> c it almost writes itself.
const on = curry((f, g, x, y) => f(g(x), g(y)));
Since you need a value of type c, you only have one place to get it, the f function. Well, the f function needs two values of type b, and you can only get those from the g function. The g function needs a value of type a, and you have two of those: x, and y. So you be fair and send both values x, and y to g, then send that to f, and your function wrote itself.
Hardy Jones
@joneshf
Dec 09 2015 12:48
There's only two other implementations of that function so you've got a 1 in 3 chance of getting it right :)
And once you add in the law on(f, identity) == f you have no other choice for an implementation.
This message was deleted
Jakub Korzeniowski
@kujon
Dec 09 2015 14:57
Hi All,
my brain isn't in a good shape today and I got stuck on trying to do the following:
I want something like R.ap, but a variant which is not going to do every permutation, but only apply first function to the first argument in the list, the second function to the second argument in the list, and so on:
R.ap([R.multiply(2), R.add(3)], [1,2]); // => [2, 5]
any ideas?
Hardy Jones
@joneshf
Dec 09 2015 14:59
zipWith(curry((f, x) => f(x)), [R.multiply(2), R.add(3)], [1,2])
maybe avoid the curry in this case?
zipWith((f, x) => f(x), [R.multiply(2), R.add(3)], [1,2])
Jakub Korzeniowski
@kujon
Dec 09 2015 15:03
that is one glorious solution, much appreciated!
Hardy Jones
@joneshf
Dec 09 2015 15:37
Use a data type that encodes this behavior is another possibility.
const ZipArray = xs => ({
  toArray: xs,
  map(f) {
    return ZipArray(R.map(f, xs));
  },
  ap({toArray: ys}) {
    if (xs.length == 0 || ys.length == 0) {
      return ZipArray([]);
    } else {
      const y = R.head(ys);
      const x = R.head(xs);
      const yss = R.tail(ys);
      const xss = R.tail(xs);

      return ZipArray(R.prepend(x(y), ZipArray(xss).ap(ZipArray(yss)).toArray));
    }
  },
});

R.ap(ZipArray([R.multiply(2), R.add(3)]), ZipArray([1,2])).toArray //=> [2, 5]
Or I guess you could clean it up...
const ZipArray = xs => ({
  toArray: xs,
  map(f) {
    return ZipArray(R.map(f, xs));
  },
  ap({toArray: ys}) {
    return ZipArray(R.zipWith((f, x) => f(x), xs, ys));
  },
});

R.ap(ZipArray([R.multiply(2), R.add(3)]), ZipArray([1,2])).toArray //=> [2, 5]
probably better that way, since tco.
Sean Sartell
@ssartell
Dec 09 2015 16:17
Hey everyone, is it possible to do recursion with strictly pointfree style syntax? I'm guessing not, but thought I'd ask here anyways.
David Chambers
@davidchambers
Dec 09 2015 16:58
@ssartell, does the Y combinator make this possible?
Scott Christopher
@scott-christopher
Dec 09 2015 17:03
var Y = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y)));
var factorial = Y(go => n => n === 0 ? 1 : n * go(n - 1));
factorial(6); // 720
I'm sure it's probably possible to define the function passed to Y in a points-free fashion, but it wouldn't be pretty.
David Chambers
@davidchambers
Dec 09 2015 17:04
@scott-christopher, that's cool!
Scott Christopher
@scott-christopher
Dec 09 2015 17:05
Scott Sauyet
@CrossEye
Dec 09 2015 17:06
The Y-combinator allows recursion even where the language doesn't explicitly account for it. But it had little to do with being points-free.
s/had/has
There should be no issues doing points-free recursion, but I'm not going to try an example on my phone.
David Chambers
@davidchambers
Dec 09 2015 17:11

@CrossEye, have you ever found yourself writing code like this?

const f = R.pipe(
  a,
  b,
  c,
  d,
  e,
  x => f(x),
  g,
  h
);

I was wondering whether the Y combinator could obviate the need for the lambda.

Scott Sauyet
@CrossEye
Dec 09 2015 17:13
I'm afraid it won't.
David Chambers
@davidchambers
Dec 09 2015 17:13
No worries. :)
Scott Christopher
@scott-christopher
Dec 09 2015 17:31
It's a silly example, but to show that the Y combinator allows for points-free recursion:
var min10 = Y(R.ifElse(R.compose(R.gte(R.__, 10), R.nthArg(1)),
                       R.nthArg(1),
                       R.useWith(R.call, [R.identity, R.inc])));

min10(12); // 12
min10(-3); // 10
the use of R.nthArg might be cheating a little, depending on your rules of points-free golf :D
Scott Christopher
@scott-christopher
Dec 09 2015 17:38
it does also emphasise that you're probably not going to have something readable by the time you manage to get the recursive function to be points-free.
Scott Sauyet
@CrossEye
Dec 09 2015 18:17
Yes, I was once again missing the point. It sounded easy to do recursion points-free until I tried it. Of course is not possible to do naively, but something like the Y-comb would allow it. But as always with the Y-combinator, it feels mostly like a trick. You prove that it can be done, but you don't use it for anything real.