These are chat archives for ramda/ramda

22nd
Mar 2015
Michael Hurley
@buzzdecafe
Mar 22 2015 00:39
James Forbes
@JAForbes
Mar 22 2015 04:17
James Forbes
@JAForbes
Mar 22 2015 04:24
I only just realised how great `ap` is after reading that article.
Hardy Jones
@joneshf
Mar 22 2015 04:31
yeah, it's hard to get an intuition for it at first
like, why would you put a function inside an `Either` or some such?
but then you see it just makes `map` more powerful and it makes sense.
James Forbes
@JAForbes
Mar 22 2015 04:55

Yeah it helped make sense of monads. But also, on unrelated note showed me the point of `R.ap`

I've always wondered when to use `R.ap` since seeing it.

R.ap is great for generating tables of combinations.
I know this is a contrived example. But it really made `ap` click for me.

``````// 0-12
var range = R.range(0,13)

// A list of functions that multiply by i
var multiplyBy = R.ap([R.multiply],range)

// The 4th function, multiplies by 3
multiplyBy[3](4) //=> 12
// The 6th function multiplies by 5
multiplyBy[5](5) //=> 25
// The 12th function multiplies by 12
multiplyBy[12](12) //=> 144

// Values 0x0 though 12*12
var flat = R.ap(multiplyBy,range)

// Split each "row" into an array
// just for illustration
var matrix = R.times(function(i){
return all.slice(i*13,i*13 + 13)
},13)

matrix[12][12] //=> 144
matrix[2][4] //=> 8
matrix[5][5] //=> 25``````
That probably seems quite obvious, but it never occured to me
Hardy Jones
@joneshf
Mar 22 2015 07:44
That's not necessarily the power of `ap`, but more the implementation for array that is doing that.
James Forbes
@JAForbes
Mar 22 2015 08:49

I suppose there is much more to it than I am grasping right now. But I didn't even realise `ap` was a Haskell thing. I just knew it was a function in ramda that applied a list of functions to a list of arguments.

I'd often need to apply a list of arguments to a single function, so I'd look at `R.ap` and then the fact it took a list of functions instead of a single function often got in the way in certain contexts. (At the time there was no `R.apply` I believe). Now I see a ton of uses. One of which would be generating combinations.

But as far as Haskell goes, no idea...

Raine Virta
@raine
Mar 22 2015 11:16
yay, new docs
Michael Hurley
@buzzdecafe
Mar 22 2015 11:42
@JAForbes example of `ap` from when i was figuring it out: http://buzzdecafe.github.io/code/2014/08/12/applicatives-ramda-style/
Raine Virta
@raine
Mar 22 2015 22:50
I have an object with a large number of kv pairs {str: num} and want to get the largest 10 by value, is my best bet to use some kind of sorted map or just convert to pairs and sort?
David Chambers
@davidchambers
Mar 22 2015 22:52
I'd use `R.toPairs`. What do you mean by a sorted map?
Raine Virta
@raine
Mar 22 2015 22:54
i suppose there's some construct that would keep the values sorted
that might be awkward to use in javascript though
David Chambers
@davidchambers
Mar 22 2015 22:59
Yeah. A list of pairs is probably the way to go.
Hardy Jones
@joneshf
Mar 22 2015 23:36
might also want to look at `invert` in case you have multiple keys with the same number
Hardy Jones
@joneshf
Mar 22 2015 23:43
actually, that'd probably just make things worse