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

  • Home
  • SEARCH
  • 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 6789107
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:33:52+00:00 2026-05-26T17:33:52+00:00

I am trying to build an elegant transpose function using functions mapn and zip

  • 0

I am trying to build an elegant transpose function using functions mapn and zip in Lua.

The mapn and zip are as follows (From the lua book):

function map(func, array)
 local new_array = {}
 for i,v in ipairs(array) do
   new_array[i] = func(v)
 end
 return new_array
end


function mapn(func, ...)
 local new_array = {}
 local i=1
 local arg_length = table.getn(arg)
 while true do
   local arg_list = map(function(arr) return arr[i] end, arg)
   if table.getn(arg_list) < arg_length then return new_array end
   new_array[i] = func(unpack(arg_list))
   i = i+1
 end
end

These work as expected.

I then define zip and transpose as:

function zip(...)
  return mapn(function(...) return {...} end,...)
end

function transpose(...)
  return zip(unpack(...))
end

Now transpose({{1,2},{3,4},{5,6}}) produces {{1,3,5},{2,4,6}} as expected.

But transpose({{1,2},{3,4},{5}}) does not produce {{1,3,5},{2,4}}. It only produces one row.

How can I get it to produce the result I wish for?


I just decided to write an “inelegant” function instead. It seems there’s no smooth way to use mapn and friends.

function transp(L)
  local n=#L


  local m,M=1e42,0
  --Get the beginning and end of resultant transpose list.
  for i=1,n do
    for k,v in pairs(L[i]) do
      if M<k then M=k end
      if m>k then m=k end
    end
  end

  local nt={}
  for i=m,M do
    local rt={}
    for j=1,n do
      rt[j]=L[j][i]
    end
    table.insert(nt,rt)
  end
  return nt
end

Please critique and improve this candidate solution.

  • 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-26T17:33:53+00:00Added an answer on May 26, 2026 at 5:33 pm

    I fixed a few things in your code and I think it works now as intended, I’ve added comments inline.

    function map(func, array)
      local new_array = {}
      for i, v in ipairs(array) do
        new_array[#new_array + 1] = func(v)
      end 
      return new_array
    end
    
    function mapn(func, ...)
      -- Variadic arguments bound to an array.
      local arrays = {...}
      local new_array = {}
      -- Simple for-loop.
      local i = 1 
      while true do
        local arg_list = map(function(arr) return arr[i] end, arrays)
        if #arg_list == 0 then
          break
        end 
        new_array[i] = func(unpack(arg_list))
        i = i + 1 
      end 
      return new_array
    end
    
    
    -- Using 'mapn' instead of 'map' (probably how you intended).
    function zip(...)
      return mapn(function(...) return {...} end,...)
    end
    
    -- Same as before.
    function transpose(...)
      return zip(unpack(...))
    end
    

    Usage example:

    for _, row in pairs(transpose({{1,2},{3,4},{5}})) do
      for _, col in pairs(row) do io.write(col .. ' ') end
      io.write('\n')
    end
    -- Output: 1 3 5 
    --         2 4
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying build a method which returns the shortest path from one node to
Im trying to build call to action button on my site using jQuery. I
I am trying to build a function in C/C++ to sort an array and
I'm trying to build a puzzle game in javascript, using raphael to take care
I am trying build a DataTable one row at a time using the following
I have a std::vector< tr1::shared_ptr<mapObject> > that I'm trying build from data contained in
Trying to build the following simple example #include <boost/python.hpp> using namespace boost::python; tuple head_and_tail(object
I'm trying build a conditions array to be using in a prepared statement: Cars.find(:all,
Currently trying to build a script utilizing cmdlets from the MS released Team Foundation
Trying to build a CMS for a blog using rails 3. In my routes.rb...

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.