I’m implementing a javascript ray-casting point-in-polygon algorithm in a purely functional fashion (no particular reason behind it).
I got stuck as i needed to get two arrays from a 2-dimentional array (replicating a list of tuples); something akin to Haskell’s unzip.
Is it possible, starting from something like [[a,b],[c,d],[e,f]] to obtain [[a,c,e],[b,d,f]] without using procedural-style iterators?
(I know it’s a trivial question, and I could just implement the function procedurally and then forget about it, but I was curious to know if there was a solution)
EDIT: To clarify, I know how to implement zip and unzip: I was wondering wether it might be possible to implement them without for loops and variable reassignments.
Your unzip is just a zip but with multiple arguments. The only reason most people don’t just use the same function is that most of the time
zipreceives a variadic list of arguments instead of a array so you need to unpack things withapplyin the unzip function.In Dojo, the library I am using, they implement zip and unzip as
Note that zip receives multiple arguments so it is more like the Python zip and less like the Haskell one.
It should not be hard to conver this code to a “purely functional” style without variable assignments. Your existing code should already be handling the job of the first two fors in the example I posted (truncating the zip at the minimum length and iterating through the indices of one of the lists). All that is left is doing a similar thing for the third for – collecting the i-th value from a list of lists instead of collecting two values from two lists.