
Issue #5133 has been updated by duerst (Martin Dürst). In issue #20336, @matheusrich wrote: ``` * [Feature #5133] Array#unzip as an alias of Array#transpose * Seems a more friendly name for this method (easier if you don't have a strong math background) * It is nice that we can do an operation an reverse it with two similar-named methods: ```rb [1, 2, 3, 4].zip(["a", "b", "c", "d"], ["I", "II", "III", "IV"]) # => [[[1, "a"], "I"], [[2, "b"], "II"], [[3, "c"], "III"], [[4, "d"], "IV"]] [[[1, "a"], "I"], [[2, "b"], "II"], [[3, "c"], "III"], [[4, "d"], "IV"]].unzip # => [[1, 2, 3, 4], ["a", "b", "c", "d"], ["I", "II", "III", "IV"]] ``` It's already alluded in some of the comments above: `transpose` is it's own inverse, so if we define `unzip` as an alias for `transpose`, `unzip` will be its own inverse. The reason Haskell has `zip`/`unzip` as inverses is that `zip` in Haskell converts from two lists to a list of pairs, and `unzip` converts from a list of pairs to a pair of lists. In Haskell, `zip` and `unzip` are inverses (up to (un)currying). In Haskell, the arguments of `zip` are limited to two lists. In Ruby, it's one array as receiver and one or more arrays as arguments. For `transpose`, it's an array of arrays. Only the later is fully generic and thus can be made exactly invertible. ---------------------------------------- Feature #5133: Array#unzip as an alias of Array#transpose https://bugs.ruby-lang.org/issues/5133#change-107431 * Author: mrkn (Kenta Murata) * Status: Assigned * Assignee: mrkn (Kenta Murata) ---------------------------------------- Array#zip の逆は Array#transpose なんですけど、 この対応関係が非常に分かり難いなと思いました。 Haskell には zip の逆をやる関数として unzip が用意されています。 unzip という名前は、「zip の逆をやりたい」と思ったときに (transpose よりは) 思い付きやすい名前だと思います。 ということで Array#unzip を Array#transpose のエイリアスとして 導入してはどうでしょう? 以下パッチです: diff --git a/array.c b/array.c index 8caad66..dc411b7 100644 --- a/array.c +++ b/array.c @@ -4720,6 +4720,7 @@ Init_Array(void) rb_define_method(rb_cArray, "reject!", rb_ary_reject_bang, 0); rb_define_method(rb_cArray, "zip", rb_ary_zip, -1); rb_define_method(rb_cArray, "transpose", rb_ary_transpose, 0); + rb_define_alias(rb_cArray, "unzip", "transpose"); rb_define_method(rb_cArray, "replace", rb_ary_replace, 1); rb_define_method(rb_cArray, "clear", rb_ary_clear, 0); rb_define_method(rb_cArray, "fill", rb_ary_fill, -1); -- https://bugs.ruby-lang.org/