Created
October 17, 2014 18:07
-
-
Save davidchambers/c952e2e27d12058b618b to your computer and use it in GitHub Desktop.
zipMany implementation with Ramda, created with @benperez
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var R = require('ramda'); | |
var zipMany = | |
R.cond(R.isEmpty, | |
R.always([]), | |
R.converge(R.map, | |
R.flip(R.pluck), | |
R.pipe(R.head, R.length, R.range(0)))); | |
var zipMany2 = function(lists) { | |
return R.isEmpty(lists) ? [] : | |
R.map(R.rPartial(R.pluck, lists), R.range(0, R.length(R.head(lists)))); | |
}; | |
zipMany([['foo', 'bar', 'baz'], [1, 2, 3], ['a', 'b', 'c']]); | |
// => [ [ 'foo', 1, 'a' ], [ 'bar', 2, 'b' ], [ 'baz', 3, 'c' ] ] |
let zipManyWith = R.curry((fn, ars) => {
let n = Math.min(...R.map(R.length, ars))
return R.map((i) => {
return fn(...R.map(as => as[i], ars))
}, R.range(0, n))
})
let zipMany = (ars) => zipManyWith((...args) => args,ars)
Kinda of an old thread, but if someone end up here:
https://ramdajs.com/docs/#transpose
// from 0.27 docs:
R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']]
R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']]
:)
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hello .. ran into this when looking around for a zip function that takes more than 2 lists .. it seems like some of the Ramda functions have perhaps changed their behavior and hence it doesn't work. The code below works: