ruby-dev (Japanese) list archive (unofficial mirror)
 help / color / mirror / Atom feed
* [ruby-dev:52082]  [Ruby master Feature#5133] Array#unzip as an alias of Array#transpose
       [not found] <redmine.issue-5133.20110801183003.482@ruby-lang.org>
@ 2024-03-22  7:59 ` duerst via ruby-dev
  2024-04-17  4:55 ` [ruby-dev:52084] " matz (Yukihiro Matsumoto) via ruby-dev
  1 sibling, 0 replies; 2+ messages in thread
From: duerst via ruby-dev @ 2024-03-22  7:59 UTC (permalink / raw)
  To: ruby-dev; +Cc: duerst

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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

* [ruby-dev:52084]  [Ruby master Feature#5133] Array#unzip as an alias of Array#transpose
       [not found] <redmine.issue-5133.20110801183003.482@ruby-lang.org>
  2024-03-22  7:59 ` [ruby-dev:52082] [Ruby master Feature#5133] Array#unzip as an alias of Array#transpose duerst via ruby-dev
@ 2024-04-17  4:55 ` matz (Yukihiro Matsumoto) via ruby-dev
  1 sibling, 0 replies; 2+ messages in thread
From: matz (Yukihiro Matsumoto) via ruby-dev @ 2024-04-17  4:55 UTC (permalink / raw)
  To: ruby-dev; +Cc: matz (Yukihiro Matsumoto)

Issue #5133 has been updated by matz (Yukihiro Matsumoto).

Status changed from Assigned to Rejected

`unzip` is not really intuitive with Ruby's OO design. Unlike Haskell, Ruby does not have static type issues.

Matz.


----------------------------------------
Feature #5133: Array#unzip as an alias of Array#transpose
https://bugs.ruby-lang.org/issues/5133#change-107939

* Author: mrkn (Kenta Murata)
* Status: Rejected
* 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/

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-04-17  4:56 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
     [not found] <redmine.issue-5133.20110801183003.482@ruby-lang.org>
2024-03-22  7:59 ` [ruby-dev:52082] [Ruby master Feature#5133] Array#unzip as an alias of Array#transpose duerst via ruby-dev
2024-04-17  4:55 ` [ruby-dev:52084] " matz (Yukihiro Matsumoto) via ruby-dev

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).