Skip to content

Instantly share code, notes, and snippets.

@davidchambers
Created October 17, 2014 18:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save davidchambers/c952e2e27d12058b618b to your computer and use it in GitHub Desktop.
Save davidchambers/c952e2e27d12058b618b to your computer and use it in GitHub Desktop.
zipMany implementation with Ramda, created with @benperez
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' ] ]
@bijoythomas
Copy link

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:

var R = require('ramda');
var zipMany = lists => R.compose(
  R.map(R.flatten),
  R.splitEvery(R.length(R.head(lists))),
  R.ap(R.map(R.pluck, R.range(0, R.length(R.head(lists))))),
  R.map(R.of)
)(lists)

zipMany([[1,2,3,4], ['a', 'b', 'c', 'd'], ['foo', 'bar', 'baz', 'bar']]) // [[1, "a", "foo", 2], ["b", "bar", 3, "c"], ["baz", 4, "d", "bar"]]
zipMany([]) // []

@ivan-kleshnin
Copy link

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)

@henriquekano
Copy link

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