elm-community / array-extra

convenience functions for working with Array

Home Page:http://package.elm-lang.org/packages/elm-community/array-extra/latest

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Adding Array.unzip

AlienKevin opened this issue · comments

Since we have zip, shall we add an unzip?

unzip : Array (a, b) -> (Array a, Array b)
unzip arrAB =
  Array.foldl
    (\(a, b) (arrA, arrB) ->
      (Array.push a arrA, Array.push b arrB)
    )
    (Array.empty, Array.empty)
    arrAB

Hi @AlienKevin, I'm not familiar with this kind of function. Is this a common Array utility e.g. in Lodash/Ramda?

Could you provide some examples? This could help with the implementation and the docstring for any potential contributors.

With the implementation you have above + the docstring with examples, this would be enough for a PR.

Lodash has the same function _.unzip:

var zipped = _.zip(['a', 'b'], [1, 2], [true, false]);
// => [['a', 1, true], ['b', 2, false]]
 
_.unzip(zipped);
// => [['a', 'b'], [1, 2], [true, false]]

Elm example:

zipped = Array.zip (Array.fromList [ 'a', 'b' ], Array.fromList [ 1, 2 ])
-- Array.fromList [('a', 1), ('b', 2)]
unzipped = Array.unzip zipped
-- (Array.fromList [ 'a', 'b' ], Array.fromList [ 1, 2 ])

I made a PR with docstring and example #20