I want to zip even and odd elements in a list to make a list of pairs, like that:
["A", "B", "C", "D", "E", "F"] -> [("A", "B"), ("C", "D"), ("E", "F")]
What is the most concise expression to do this in elegant in functional way?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
In 2.8, you’d probably use methods:
(This is 2.8.0 Beta1; the latest trunk has
collectin place ofpartialMap.)In 2.7–and not a bad runner-up in 2.8–you could create a recursive method as legoscia did:
Edit: here’s another briefer approach that works on 2.7 also:
(Note the use of
drop(1)instead oftailso it works with empty lists.)