0

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!

2 Answers 2

1

It's normally called unzip (with zip being the operation going in the other direction).

Sign up to request clarification or add additional context in comments.

Comments

0

In ramda transpose works both ways. You basically want to:

turn a list of n lists of length x, into a list of x lists of length n.

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:

  1. zip

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.