These are chat archives for ramda/ramda

3rd
Mar 2015
Danny Fritz
@dannyfritz
Mar 03 2015 03:52
how would i sort an array by frequency in the array? like [1, 2, 2, 1, 1] => [1, 1, 1, 2, 2] and [1, 2, 2, 3, 3, 3, 3] => [3, 3, 3, 3, 2, 2, 1]
Gil Birman
@gilbox
Mar 03 2015 06:01
probably easier with native Array.reduce b/c it supports sparse arrays
[1,2,3,2,1,2,2,1].reduce(function(prev,curr) { console.log(prev,curr); prev[curr] = (prev[curr] || 0)+1; return prev; }, [])
This message was deleted
Scott Sauyet
@CrossEye
Mar 03 2015 15:53
@gilbox something like that might be a useful first step, but doesn't solve the original problem. The same function could be used with Ramda's `reduce` BTW. The restrictions on sparse arrays have to do with what it will accept, not what it will generate. Nonetheless, I would suggest accumulating on an empty object instead of an array. That just seems more generally useful.
@dannyfritz: I don't see anything simple. One possibility is at http://tinyurl.com/q5ahjlk.
Scott Sauyet
@CrossEye
Mar 03 2015 15:58
``````var ls1 = [1, 2, 2, 1, 1];
var ls2 = [1, 2, 2, 3, 3, 3, 3];

var f = function(ls) {
var counts = R.countBy (R.identity) ;
var countFor = R.propOf(counts);
var order = R.reverse(R.sortBy(countFor, R.keys(counts)));
return R.flatten(R.map(R.converge(R.repeat, R.identity, countFor), order));
};

JSON.stringify([f(ls1), f(ls2)])``````
Ludwig Magnusson
@TheLudd
Mar 03 2015 16:09
Is there a function in ramda that takes one argument and returns an array containing that argument?
``````function(a) {
return [a];
}``````
Danny Fritz
@dannyfritz
Mar 03 2015 16:41
Thanks @CrossEye. This is how i ended up doing it:
``````function sortByFrequency (a) {
return R.reduce(function (sorted, pair) {
return R.concat(sorted, R.repeat(pair[0], pair[1]));
}, [], R.toPairs(R.countBy(R.identity, a)));
}``````
i like your use of flatten better than my reduce though
Gil Birman
@gilbox
Mar 03 2015 17:02
@CrossEye if you accumulate on an empty object there is an extra step of sorting the keys
David Chambers
@davidchambers
Mar 03 2015 17:19
@TheLudd: Yes, `R.of`.
Ludwig Magnusson
@TheLudd
Mar 03 2015 17:34
thanks
Raine Virta
@raine
Mar 03 2015 17:51
ramda/ramda-fantasy#22 if getters are bad, which I believe, can you show practical example of how to do control flow with ramda-fantasy `Maybe`?
if m.isJust doSomethingWith m.value; else whatever
Ludwig Magnusson
@TheLudd
Mar 03 2015 17:52
@raine isn't the alternative to always map over it?
I think that might work in Haskell (I wouldn't know because I haven't worked with it) but in JS it can be unpractical. Especially with external libraries that are not implemented with FP in mind
Raine Virta
@raine
Mar 03 2015 17:54
Ludwig Magnusson
@TheLudd
Mar 03 2015 17:55
What were you pointing to? The interface was not super clear...
Raine Virta
@raine
Mar 03 2015 17:55
here's what I actually ended up, using data.maybe's `cata` that allows pattern matching
but I'd be curious to know how to achieve the same with ramda-fantasy
David Chambers
@davidchambers
Mar 03 2015 18:08
`R.map` and `R.chain` are all one needs. Here's an example:
``````R.pipe(
S.get('parties'),
R.chain(S.get('merchant')),
R.chain(S.get('mcc')),
R.map(R.replace(/^0+/, '')),
R.chain(categories.fromMcc),
R.map(R.prop('hierarchy')),
R.map(category.TentativeCategory),
S.fromMaybe(category.unresolved)
)``````
Raine Virta
@raine
Mar 03 2015 18:10
well, that's all data
David Chambers
@davidchambers
Mar 03 2015 18:29
Sorry, I didn't look closely enough at your gists. `cata` is an interesting method.
Hardy Jones
@joneshf
Mar 03 2015 20:40
@raine there's no way to fold a `Maybe a` in ramda-fantasy yet, but it looks like your two alternatives in `Maybe.cata` don't have the same return type. So, that section there isn't well typed.