These are chat archives for ramda/ramda

15th
Apr 2015
Raine Virta
@raine
Apr 15 2015 08:42
I have trouble grokking the difference between transformer and transducer
going to read jlongster's article next
Raine Virta
@raine
Apr 15 2015 09:03
This message was deleted
Raine Virta
@raine
Apr 15 2015 10:36
ok. makes more sense after realizing the origin of the word transducer
Ludwig Magnusson
@TheLudd
Apr 15 2015 12:01
@paldepind If your streams are applicatives you only need to also implement chain in order to make them monads. I don't know if chain makes sense in the stream case however. Perhaps it does
Ludwig Magnusson
@TheLudd
Apr 15 2015 12:58
Would a count function be an interesting addition to ramda? Currently there is countBy which returns an object. I think I'd like a function
count = (a -> Boolean) -> [a] -> Number
Hardy Jones
@joneshf
Apr 15 2015 13:22
That seems like a good thing, and easy to implement.
count = (p) => R.reduce((acc, x)  =>  p(x) ? acc + 1 : acc, 0)
Or so
Ludwig Magnusson
@TheLudd
Apr 15 2015 13:23
Or R.compose(R.length, R.filter) :D
Hardy Jones
@joneshf
Apr 15 2015 13:23
even better
Scott Sauyet
@CrossEye
Apr 15 2015 13:39
Looks useful to me. Implementation should be simple, even if we don't choose one of these elegant options.
Ludwig Magnusson
@TheLudd
Apr 15 2015 13:42
@CrossEye Any preference?
Say between reusing ramda functions and doing a for loop or something
Scott Sauyet
@CrossEye
Apr 15 2015 14:44
We usually use while loops internally instead of the most elegant code in ordet to aid performance. But when there's not a substantial difference, I'm all for the elegance.
Chad Adams
@chadams
Apr 15 2015 14:57

var self = this;
    var renderPages = R.mapIndexed(function(entity, index){
      return (
        <BoxElement key={index} data={entity} position={self.state.position}></BoxElement>
      );
    });
see how "self" is outside the renderPages function? is there a pattern for passing the "this" reference into the renderPages functions in order to make it more pure?
Hardy Jones
@joneshf
Apr 15 2015 15:00
what is impure about using self as you have?
Scott Sauyet
@CrossEye
Apr 15 2015 15:16
I don't think there's anything to help. The obvious choice of bind won't do, as mapIndexed does not pass the context along to its callback function.
Chad Adams
@chadams
Apr 15 2015 15:25
so it's considered ok to do this?
Hardy Jones
@joneshf
Apr 15 2015 15:29
is it doing something that is impure? I feel like i'm missing something
Chad Adams
@chadams
Apr 15 2015 15:39
impure as it's using variables (state) outside the function, whereas passing in the variables (or composing them) makes the function more "pure". I can;t really reuse "renderPages" on another because its dependent on self. but if I pass it in I can reuse it more
lluft
@lluft
Apr 15 2015 15:42
@raine could you share the link to the article you are refearing to?
Ludwig Magnusson
@TheLudd
Apr 15 2015 15:56
@chadams
var createElement = R.curry(function(ref, entity, index) {
      return (
        <BoxElement key={index} data={entity} position={ref.state.position}></BoxElement>
      );
});
var renderPages = R.mapIndexed(createElement(this), list);
Michael Hurley
@buzzdecafe
Apr 15 2015 15:57
:+1: @TheLudd
Kevin Wallace
@kedashoe
Apr 15 2015 16:03
@lluft probably http://jlongster.com/Transducers.js--A-JavaScript-Library-for-Transformation-of-Data (there are also links in that post to more good transducer material)
lluft
@lluft
Apr 15 2015 16:07
@kedashoe yeah I think so as well. That’s the one I just read. Just wanted to make sure :)
Hardy Jones
@joneshf
Apr 15 2015 16:13
@chadams oh, I see what you're saying. So I think there's more to this than just taking self as an argument. Just because a function references a "global" value doesn't make it impure. Most functions do this. What makes it impure is if the "global" value it references is mutable, and then is mutated. Also, just because a function takes a value as an argument doesn't make it pure. The two ideas (function and purity) are different.

for instance

var k = function(x) {
  return function(y) {
    return x;
  };
}

Here the inner function in k references a "global" value, though some might call it a free variable. But we'd still assume k to be pure right?

in your example, It doesn't seem like self is being used impurely is the point I'm trying to make
Ludwig Magnusson
@TheLudd
Apr 15 2015 16:16
There might be a mixup here of the concepts pure and reusable
Hardy Jones
@joneshf
Apr 15 2015 16:16
yeah
Scott Sauyet
@CrossEye
Apr 15 2015 17:00
I would guess that this function could be used in an impure manner, possibly associated somehow with this and returning a different result whenever this.state.position changes.
I'm guessing the answer from @TheLudd will do well for the OP,
Scott Sauyet
@CrossEye
Apr 15 2015 18:01
... and it might be even better if the actual position value were supplied rather than the ref, because then it might really be pure. But I wouldn't be surprised if the goal is to keep a local reference to some externally mutable state.
Ludwig Magnusson
@TheLudd
Apr 15 2015 18:03
@CrossEye Just passing in position would not be a problem
Scott Sauyet
@CrossEye
Apr 15 2015 18:08
@TheLudd Yes, that's clear. What wasn't clear is if this is the full extent of the need or just a simplified example. If the OP just needs position, then that quick change to your function looks perfect.