These are chat archives for ramda/ramda

20th
Apr 2016
Jim Fitzpatrick
@jimf
Apr 20 2016 01:04
anyone happen to know approximately how long the ramda mugs take to ship/arrive in the states? thinking about doing a giveaway at our next meetup, but its on may 4th, which might be cutting it close
Keith Nicholas
@keithn
Apr 20 2016 01:27
you need star wars cups if its on may 4th
Hardy Jones
@joneshf
Apr 20 2016 02:00
@joncfoo is that actually haskell syntax so that Set is a data constructor, or is that pseudo haskell syntax so that Set is the idea of a mathematical set?
If it's the former, you could look at daggy or something. which would give you your pattern matching.
Or just do Maybe Bool, since it's isomorphic to it.
If it's the later, daggy wouldn't be so bad an idea either.
Hell, just daggy it anyway, but remove the Bool entirely
Hardy Jones
@joneshf
Apr 20 2016 02:06
data FieldState = Set | UnSet | NA
or something
then you can definitely pattern match on it
Juan Soto
@sotojuan
Apr 20 2016 03:26
Can I keep asking here what to use to do stuff? Feel like it's annoying lol.
Scott Christopher
@scott-christopher
Apr 20 2016 03:30
@sotojuan Please do. It takes a decent effort to annoy most of us here :)
Juan Soto
@sotojuan
Apr 20 2016 03:35
Ok let's try: I have an array of objects of the form {key: string, message: string}, for example {key: 'A', message: 'Hello'}. There's objects with the same key property in them that are related.
What I want is to, for every set of objects with the same key value, reduce them down to a single object whose messageproperty is the message of the related objects concatenated together. Essentially:
let stuff = [{key: a, message: 'a'}, {key: a, message: 'hello'}, {key: b, message: 'bye'}, {key: b, message: 'good bye'}]
// => [{key: a, message: 'hihello'}, {key: b, message: 'byegood bye'}
One aprroach would be to group them by key and then just get the message of each, reduce that to a string I want, and just return a new object/merge with any and return that
Scott Christopher
@scott-christopher
Apr 20 2016 03:50
You should be able to use:
R.reduceBy
ram-bot
@ram-bot
Apr 20 2016 03:50
Error: No such function reduceBy
Scott Christopher
@scott-christopher
Apr 20 2016 03:50
Scott Christopher
@scott-christopher
Apr 20 2016 03:57
@ram-bot
const input = [{key: 'a', message: 'a'}, {key: 'a', message: 'hello'}, {key: 'b', message: 'bye'}, {key: 'b', message: 'good bye'}]
const fn = reduceBy((msg, obj) => concat(msg, obj.message), '', prop('key'));
fn(input);
ram-bot
@ram-bot
Apr 20 2016 03:57
{ a: 'ahello', b: 'byegood bye' }
Scott Christopher
@scott-christopher
Apr 20 2016 03:59
Ah, sorry. I just realised you wanted a different output.
Scott Christopher
@scott-christopher
Apr 20 2016 04:12
It'll end up pretty similar to what you were thinking.
@ram-bot
const input = [{key: 'a', message: 'a'}, {key: 'a', message: 'hello'}, {key: 'b', message: 'bye'}, {key: 'b', message: 'good bye'}]

const mergeFn = (k, l, r) => k === 'message' ? concat(l, r) : r
const fn = compose(map(reduce(mergeWithKey(mergeFn), {})), groupWith(eqProps('key')))

fn(input);
ram-bot
@ram-bot
Apr 20 2016 04:12
[ { key: 'a', message: 'ahello' },
  { key: 'b', message: 'byegood bye' } ]
David Chambers
@davidchambers
Apr 20 2016 05:53
@iamstarkov, S.pipe takes an array of functions. I did at one point make the case for R.pipe doing the same, but didn't win that battle. ;)
Ricardo Pallas
@RPallas92
Apr 20 2016 09:28
Thanks @Avaq, It works like a charm
Scott Sauyet
@CrossEye
Apr 20 2016 11:04
@jimf: the last one I ordered (Connecticut) took seven or eight days, I think.
Raine Virta
@raine
Apr 20 2016 11:05
R.reduceBy
Sebastien Daniel
@SebastienDaniel
Apr 20 2016 12:31
goooood morning
Are there any guides on function parameter ordering ?
It often seems like I do this at random, but that a certain order may make a significant difference.
afaik, Ramda made a point of this when comparing to underscore ?
Raine Virta
@raine
Apr 20 2016 12:36
generally things you're likely to know beforehand come first
data last
Sebastien Daniel
@SebastienDaniel
Apr 20 2016 12:42
Hi Raine
Jim Fitzpatrick
@jimf
Apr 20 2016 12:42
@CrossEye thanks! i'll place an order then and cross my fingers. worst case, we put it off til the june meetup
Sebastien Daniel
@SebastienDaniel
Apr 20 2016 12:42
yeah, I'm watching "Underscore, you're doing it wrong!"
just grasped the need to pass data last :)
is data-last only because of "currying"?
or partial application
Michael Maier
@maiermic
Apr 20 2016 15:40
is there an inverse function of invert?
var raceResultsByFirstName = {
  first: 'alice',
  second: 'jake',
  third: 'alice',
};
var inverted = R.invert(raceResultsByFirstName);
//=> { 'alice': ['first', 'third'], 'jake':['second'] }

inverseOfInvert(inverted) // should be equal to raceResultsByFirstName
how would you call this function if it does not exist?
James Forbes
@JAForbes
Apr 20 2016 15:44
Maybe
R.pipe(
   R.toPairs
   ,R.map(R.reverse)
   ,R.zipObject
)
Or something to that affect
It could be lossy though depending on your data
Oh wow, Ramda's invert works differently to what I expected
Jonathan Curran
@joncfoo
Apr 20 2016 15:56
thanks @keithn & @joneshf - I"ll have a look at daggy
LeonineKing1199
@LeonineKing1199
Apr 20 2016 18:15
Can someone check my understanding of transducers? I assumed this should spit out a 1 element array with a value of 1338 but it looks like the reduction part of transduction isn't working the way I thought it would:
http://goo.gl/fWr3Yf
Aldwin Vlasblom
@Avaq
Apr 20 2016 18:17
@LeonineKing1199 Transducers compose in reverse.
LeonineKing1199
@LeonineKing1199
Apr 20 2016 18:17
Lol my bad! Hey, thanks for the correction! I was reading the docs and I was like, I know this should work. You've now helped me make my code drastically more bullet-proof and 1/10 procedural
Okay, another stupid question, how does one incorporate R.all into a transducer stack?
I have an array of potentially undefined objects, I want to pick a property from the defined objects and then I want the result of R.all(R.equals(true)) mapped over the filtered elements
Do I instead just do,
R.all(R.equals(true), R.transduce(/* ... */))
Brad Compton (he/him)
@Bradcomp
Apr 20 2016 18:24
@LeonineKing1199 There is also a shortcut for transducing into an array http://goo.gl/2MVx9X
LeonineKing1199
@LeonineKing1199
Apr 20 2016 18:24
:+1:
Brad Compton (he/him)
@Bradcomp
Apr 20 2016 18:26
Looks like all can act as a transducer, so you might be able to just put it in there
Or this maybe? http://goo.gl/Z1N2iE
LeonineKing1199
@LeonineKing1199
Apr 20 2016 18:29

I was able to yolo incorporate all into the transformation stack and get back a 1 element array with a boolean so that's positive.

In my ideal world, I wouldn't get an array back but considering my ignorance on the accumulation behavior (not to mention the type change from Array to Boolean), I think this is "good enough"

And boom goes the dynamite
Ty @Bradcomp
Dave Tonge
@davidgtonge
Apr 20 2016 19:52
Is it possible to do recursion in a point-free style, e.g. http://goo.gl/d5hFZ4
The function declaration above is only needed to make the recursion work
Ricardo Pallas
@RPallas92
Apr 20 2016 19:56
@davidgtonge good example
Dave Tonge
@davidgtonge
Apr 20 2016 19:58
thanks - I'm experimenting with extreme point-free style and finding it frustrating that I have to use function declaration to enable recursion
Ricardo Pallas
@RPallas92
Apr 20 2016 20:04
Yes, maybe using es6 syntax http://goo.gl/jGIkjn
Dave Tonge
@davidgtonge
Apr 20 2016 20:12
Thanks - but that still requires declaring the argument
Ricardo Pallas
@RPallas92
Apr 20 2016 20:12
yes :worried:
Brad Compton (he/him)
@Bradcomp
Apr 20 2016 20:14
@davidgtonge I'm not sure what you're trying to do is possible in Javascript. When you put it in a function (arrow or regular) you are creating a new scope, that doesn't get evaluated until the function is invoked. That guarantees the fibonacci will be defined at the time it gets called. Without that scope, the variable doesn't get bound to the value until after you define the function, so it will always be undefined when you pass it into converge
Dave Tonge
@davidgtonge
Apr 20 2016 20:15
thanks @Bradcomp thought so
Michael Hurley
@buzzdecafe
Apr 20 2016 21:36

finding it frustrating that I have to use function declaration to enable recursion

you could do anonymous recursion using the Y combinator. But why would you? You can name things and refer to them by name. That's pretty handy.

function Y(f) {
      var p = function(h) {
        return function(x) {
          return f(h(h))(x);
        };
      };
      return p(p);
    }

Y(function(f) {
      return function(n) {
        return n < 2 ? n : f(n-1) + f(n-2);  // guts of the fibonacci sequence, anonymously
      };
    })(10) //=> 55
Chet Harrison
@ChetHarrison
Apr 20 2016 21:49
I'm going bat shit crazy trying to get converge to work this example was lifted from the docs it is wrapped in a Jasmine test
This message was deleted
it( 'converge', function() {
        var add = (a, b) => a + b;
        var multiply = (a, b) => a * b;
        var subtract = (a, b) => a - b;

        //≅ multiply( add(1, 2), subtract(1, 2) );
        const test = R.converge(multiply, [add, subtract])
        expect( test( 1, 2 ) ).toEqual( -3 );
    } );
and I get a Error: First argument to _arity must be a non-negative integer no greater than ten
Scott Christopher
@scott-christopher
Apr 20 2016 21:58
@ChetHarrison that's strange. It seems to work for me.
This message was deleted
@ram-bot
var add_ = (a, b) => a + b;
var multiply_ = (a, b) => a * b;
var subtract_ = (a, b) => a - b;

R.converge(multiply_, [add_, subtract_])(1, 2)
ram-bot
@ram-bot
Apr 20 2016 21:58
-3
Chet Harrison
@ChetHarrison
Apr 20 2016 21:59
Yeah. Maybe I should have a martini and see if that helps.
Scott Christopher
@scott-christopher
Apr 20 2016 21:59
If I were to guess, I'd say that converge can't work out the arity of your multiply function.
Are you using anything to translate the fat arrow functions that could potentially be messing with the arity?
Chet Harrison
@ChetHarrison
Apr 20 2016 22:00
Thanks for checking
hmm I am transpiling my tests with babel. Thats a good thought let me see what the ES5 looks like
Scott Christopher
@scott-christopher
Apr 20 2016 22:01
e.g. try the following and see whether it gives you the same error
@ram-bot
var add_ = function(a, b) { return a + b };
var multiply_ = function(a, b) { return a * b };
var subtract_ = function(a, b) { return a - b };

R.converge(multiply_, [add_, subtract_])(1, 2)
ram-bot
@ram-bot
Apr 20 2016 22:02
-3
Chet Harrison
@ChetHarrison
Apr 20 2016 22:03
hmm same error
Scott Christopher
@scott-christopher
Apr 20 2016 22:03
Which version of Ramda are you using?
Chet Harrison
@ChetHarrison
Apr 20 2016 22:05
0.17.1
Scott Sauyet
@CrossEye
Apr 20 2016 22:05
It sounds like the error you get when you forget the square braces.
Scott Christopher
@scott-christopher
Apr 20 2016 22:06
I think converge was variadic in 0.17
Scott Sauyet
@CrossEye
Apr 20 2016 22:06
So my error in reverse. Could be.
Scott Christopher
@scott-christopher
Apr 20 2016 22:06
Try
const test = R.converge(multiply, add, subtract)
Chet Harrison
@ChetHarrison
Apr 20 2016 22:07
I should yank the trailing underscores?
Scott Christopher
@scott-christopher
Apr 20 2016 22:08
Sorry, I just had those in there for @ram-bot to prevent clashing with functions in Ramda with the same name.
Chet Harrison
@ChetHarrison
Apr 20 2016 22:08
k. I have it namespaced

I tried to simplify it down to

R.converge( R.add, [ R.add, R.add] )( 1, 1 );

and got the same error

Scott Christopher
@scott-christopher
Apr 20 2016 22:10
Yep, you'll need to remove the array and just pass them as separate arguments.
Chet Harrison
@ChetHarrison
Apr 20 2016 22:10
ahh gotcha
Scott Christopher
@scott-christopher
Apr 20 2016 22:10
In 0.17, converge and useWith were still variadic.
Chet Harrison
@ChetHarrison
Apr 20 2016 22:10
it worked
Scott Christopher
@scott-christopher
Apr 20 2016 22:11
Chet Harrison
@ChetHarrison
Apr 20 2016 22:11
:sparkles:
Scott Christopher
@scott-christopher
Apr 20 2016 22:11
:thumbsup: :
Chet Harrison
@ChetHarrison
Apr 20 2016 22:11
looks like we need to update the docs
var add = (a, b) => a + b;
var multiply = (a, b) => a * b;
var subtract = (a, b) => a - b;

//≅ multiply( add(1, 2), subtract(1, 2) );
R.converge(multiply, [add, subtract])(1, 2); //=> -3

var add3 = (a, b, c) => a + b + c;
R.converge(add3, [multiply, add, subtract])(1, 2); //=> 4
Scott Christopher
@scott-christopher
Apr 20 2016 22:12
Sorry, what in particular needs to be updated?
Chet Harrison
@ChetHarrison
Apr 20 2016 22:13
that example code has the [ ]
Scott Christopher
@scott-christopher
Apr 20 2016 22:13
converge and useWith were converted to binary functions at some stage (I think 0.18) so they accepted an array of functions as the second argument.
Chet Harrison
@ChetHarrison
Apr 20 2016 22:14
ah gotcha. Serves me right for not staying current
Scott Christopher
@scott-christopher
Apr 20 2016 22:14
:)
Chet Harrison
@ChetHarrison
Apr 20 2016 22:15
so my version will only take 2 branching functions?
Scott Christopher
@scott-christopher
Apr 20 2016 22:15
It should take as many as you give it.
Chet Harrison
@ChetHarrison
Apr 20 2016 22:16
k good to know
Scott Christopher
@scott-christopher
Apr 20 2016 22:16
So the add3 example above should work in 0.17 if you remove the [].
Chet Harrison
@ChetHarrison
Apr 20 2016 22:16
roger that
Chet Harrison
@ChetHarrison
Apr 20 2016 22:32
I just updated to the latest and got a Error: define: reduce: list must be array or iterable. I grepped on reduce and found one line where I am using it but looks ok. Any Ideas on where to go looking?
Scott Christopher
@scott-christopher
Apr 20 2016 22:46
Hard to say. You could have a squiz through the various upgrade guides to see what has changed. https://github.com/ramda/ramda/issues?q=label%3A"upgrade+guide"
Otherwise you could set a breakpoint where that error is thrown and have a look back up the stack for clues.
Brad Compton (he/him)
@Bradcomp
Apr 20 2016 22:47
@ChetHarrison It could also be some other function that uses reduce internally
Chet Harrison
@ChetHarrison
Apr 20 2016 22:47
k thanks! Super excited to move past that one. I have been refactoring away on some old code and I like the way it looks now