Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

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.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6937987
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T12:26:01+00:00 2026-05-27T12:26:01+00:00

I’m implementing a javascript ray-casting point-in-polygon algorithm in a purely functional fashion (no particular

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T12:26:02+00:00Added an answer on May 27, 2026 at 12:26 pm

    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 zip receives a variadic list of arguments instead of a array so you need to unpack things with apply in the unzip function.

    In Dojo, the library I am using, they implement zip and unzip as

    unzip: function(/*Array*/ a){
        // summary: similar to dojox.lang.functional.zip(), but takes
        // a single array of arrays as the input.
        // description: This function is similar to dojox.lang.functional.zip()
        // and can be used to unzip objects packed by
        // dojox.lang.functional.zip(). It is here mostly to provide
        // a short-cut for the different method signature.
    
        return df.zip.apply(null, a);
    }
    
    zip: function(){
        // summary: returns an array of arrays, where the i-th array
        // contains the i-th element from each of the argument arrays.
        // description: This is the venerable zip combiner (for example,
        //    see Python documentation for general details). The returned
        //    array is truncated to match the length of the shortest input
        //    array.
        var n = arguments[0].length,
            m = arguments.length,
            i = 1,
            t = new Array(n),
            j,
            p;
        for(; i < m; n = Math.min(n, arguments[i++].length));
        for(i = 0; i < n; ++i){
            p = new Array(m);
            for(j = 0; j < m; p[j] = arguments[j][i], ++j);
            t[i] = p;
        }
        return t;
    },
    

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I am reading a book about Javascript and jQuery and using one of the
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i got an object with contents of html markup in it, for example: string
I am trying to render a haml file in a javascript response like so:
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.