prelude-ls icon indicating copy to clipboard operation
prelude-ls copied to clipboard

Nice to have function - transpose

Open chrisvfritz opened this issue 9 years ago • 2 comments

The use case for this is rarer, but it's in Haskell and I do sometimes need it. When I do, it's just on the server side, so this is what I've been using:

transpose = (array) ->
  Object.keys(array[0])
    |> map (column) ->
       array |> map (row) -> row[column]

Since Object.keys isn't supported in <IE9 (I think that's the one), we'd probably need a more lengthy implementation.

chrisvfritz avatar Jul 14 '15 19:07 chrisvfritz

There is no need for Object.keys to loop over array indices at all.

transpose = (array) ->
    for _, column in array[0]
        for row in array
            row[column]

Maybe zip-all is all you need anyway?

igl avatar Jul 14 '15 22:07 igl

zip-all could actually do the job here, with zip-all ...array. I still think it'd be great to have transpose as an alias for zip-all ...array though. Not only would it be more natural for folks coming from other functional languages, but let's take the example of piping. Sure, you could do this:

array |> -> zip-all ...it

But it'd be so much cleaner and obvious if I could just write array |> transpose.

chrisvfritz avatar Jul 18 '15 13:07 chrisvfritz