These are chat archives for ramda/ramda

16th
Apr 2015
Scott Christopher
@scott-christopher
Apr 16 2015 03:45
Does something like the following already exist somewhere in Ramda?
function dispatch(origFn, predicate, matchFn) {
  return function() {
    if (predicate.apply(this, arguments)) {
      return matchFn.apply(this, arguments);
    } else {
      return origFn.apply(this, arguments);
    }
  }
}
Scott Christopher
@scott-christopher
Apr 16 2015 04:04
I’m basically looking for a way to wrap an existing function, allowing it to call an alternative function if the predicate matches.
Ludwig Magnusson
@TheLudd
Apr 16 2015 07:10
@chadams Did the solution work for you?
Scott Sauyet
@CrossEye
Apr 16 2015 11:06
@scott-christopher: ifElse is almost the same, although the predicate comes first. If you flip it, it might be a perfect match.
@raine: perfectly clear, and clearly useful.
@scott-christopher: looking again, I guess this would also involve reversing the sense of your predicate.
Scott Christopher
@scott-christopher
Apr 16 2015 11:25
Argument order in this case isn't too important, so this will do nicely. Thanks
Irakli Safareli
@safareli
Apr 16 2015 18:19

Hi everyone,

a friend of mine asked how to write code like this in more functional way.
when i tried to solve this problem in functional way i wrote this little helper function propLens (there i'm using it to)
so i wont to ask you if i'm doing it in a right way

Raine Virta
@raine
Apr 16 2015 19:50
is there a way to use R.lens to operate on a subset of a list?
Scott Sauyet
@CrossEye
Apr 16 2015 20:16
@safareli: You might want to look at #971, which added a function very much like that one. It's implementation uses assoc, and is therefore a little simpler, but it's much the same idea. It'll be in the next release.
@raine: What do you mean by a subset? A slice? The result of a filter? Something else?
Raine Virta
@raine
Apr 16 2015 20:20
I tried to implement a "tailLens" to do something as follows
var mapTail = function(fn, arr) {
  return R.slice(0, 1, arr).concat(R.map(fn, R.tail(arr)));
};
but it wouldn't work. the setter didn't get the arguments in the right way after I implemented get as R.tail(arr);
Scott Sauyet
@CrossEye
Apr 16 2015 20:37
bit.ly/1G0ibwG is one experiment.
I don't quite follow hire you'd want to use that function.
Raine Virta
@raine
Apr 16 2015 20:41
how so?
Hardy Jones
@joneshf
Apr 16 2015 21:11
well part of the problem is that it wont behave well. In particular, if you try to use it on an empty list, things get wonky.
you'd expect taht if you get what you just set, it'd be the same thing right?
tailLens(tailLens.set([1,2,3], [])) //=> [2,3]
vs
tailLens(tailLens.set([1,2,3], [4,5,6])) //=> [1,2,3]
Raine Virta
@raine
Apr 16 2015 21:13
i see. looks like it's not a use case for lens
Hardy Jones
@joneshf
Apr 16 2015 21:16

similarly, if you set multiple times, the last set should be the same as just setting once,
but

tailLens.set([1,2,3], tailLens.set([4,5,6], [])) //=> [4,1,2,3]

vs

tailLens.set([1,2,3], []) //=> [1,2,3]
a related concept can work for what you want though
Scott Sauyet
@CrossEye
Apr 16 2015 21:21
sorry, i was typing on my phone before. Autocorrect decided "how" should be "hire" and I didn't notice.
Hardy Jones
@joneshf
Apr 16 2015 21:52
Actually, looking at the docs, headLens has similar problems with behavior, so maybe it helps to describe the expected input.
Or something.
Scott Sauyet
@CrossEye
Apr 16 2015 22:16

@joneshf: What problem do you see?

headLens(headLens.set([1,2,3], [])); //=> [1, 2, 3]
headLens(headLens.set([1,2,3], [4,5,6])); //=> [1, 2, 3]
headLens.set([1,2,3], headLens.set([4,5,6], [])); //=> [[1, 2, 3]]
headLens.set([1,2,3], []); //=> [[1, 2, 3]]

http://bit.ly/1yxnT6p

Hardy Jones
@joneshf
Apr 16 2015 22:24
Well you'd expect that if you immediately set what you just get-d? it wouldn't change things
headLens.set(headLens([]), []) //=> [undefined]
vs
headLens.set(headLens([1]), [1]) //=> [1]
Scott Sauyet
@CrossEye
Apr 16 2015 22:41
I'm afraid that's a general issue with nulls and undefined in JS. I don't know what Ramda could do differently.
Hardy Jones
@joneshf
Apr 16 2015 22:53
yeah, it probably helps to talk about the input expected, because if you pass it a non-array it's going to blow up as well. So I guess in that sense they're both well behaved since they work as expected from non-empty arrays.