Reputation: 394
I am looking for the functional programming term (if it exists) of a function which converts an array of several 2-tuples into one 2-tuple array.
I think the best way to illustrate my question is through an example. I want to convert array
to newArray
:
const array = [[10, "A"], [20, "B"], [30, "C"], [40, "D"]]
const newArray = [[10, 20, 30, 40], ["A", "B", "C", "D"]]
My favorite language is javascript and I know I could do this through hacking with reduce
but I would like to know if I can find a function like this in functional programming libraries like for example Ramda.
Thank you!
Upvotes: 0
Views: 238
Reputation: 14199
In ramda transpose works both ways. You basically want to:
turn a list of
n
lists of lengthx
, into a list ofx
lists of lengthn
.
const initial = [[1, 'a'], [2, 'b'], [3, 'c']];
const result = R.transpose(initial);
console.log('result is', result);
const data = R.transpose(result);
console.log('data is', data);
<script src="https://cdnjs.cloudflare.com/ajax/libs/ramda/0.26.1/ramda.js" integrity="sha256-xB25ljGZ7K2VXnq087unEnoVhvTosWWtqXB4tAtZmHU=" crossorigin="anonymous"></script>
Also look at:
Upvotes: 0
Reputation: 850
It's normally called unzip
(with zip
being the operation going in the other direction).
Upvotes: 1