https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112011-08-01T19:09:17ZRuby Issue Tracking SystemRuby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=199222011-08-01T19:09:17Zznz (Kazuhiro NISHIYAMA)
<ul></ul><p>逆とはどういう動作のことを言っていますか?</p>
<p>Array#zip が複数の配列から1つの配列を返す(かブロックを受け取ると別の動作)で、<br>
Array#transpose が1つの配列から1つの配列を返す(ブロックは受け取らない)ということから<br>
逆という印象は受けなかったので、逆というだけの説明だとよくわかりませんでした。</p> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=199242011-08-01T20:59:06Zmetanest (Makoto Kishimoto)
<ul></ul><p>きしもとです</p>
<p>[1, 2, 3, 4] と ["a", "b", "c", "d"] から、<br>
[[1, "a"], [2, "b"], [3, "c"], [4, "d"]] を作るのが zip で、</p>
<p>irb> [1, 2, 3, 4].zip ["a", "b", "c", "d"]<br>
=> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]]</p>
<p>[[1, "a"], [2, "b"], [3, "c"], [4, "d"]] を transpose すると、<br>
[[1, 2, 3, 4], ["a", "b", "c", "d"]] が得られるわけですが、</p>
<p>irb> [[1, "a"], [2, "b"], [3, "c"], [4, "d"]].transpose<br>
=> [[1, 2, 3, 4], ["a", "b", "c", "d"]]</p>
<p>なんとなく、たまたま、という気がします。</p>
<p>transpose は自己双対で、zip と双対というわけでもないので、<br>
エイリアスには反対、かなぁ。</p>
<a name="1-2-3-4-5-6-7-8-9transpose-に対応するものって"></a>
<h1 >[[1, 2, 3], [4, 5, 6], [7, 8, 9]].transpose に対応するものって<a href="#1-2-3-4-5-6-7-8-9transpose-に対応するものって" class="wiki-anchor">¶</a></h1>
<a name="zip-で簡単にできましたっけ"></a>
<h1 >zip で簡単にできましたっけ?<a href="#zip-で簡単にできましたっけ" class="wiki-anchor">¶</a></h1>
<p>というわけで、ドキュメント等にわかりやすく書いておくことについては<br>
賛成します。</p> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=199302011-08-01T22:53:06Znaruse (Yui NARUSE)naruse@airemix.jp
<ul></ul><p>(2011/08/01 20:54), KISHIMOTO, Makoto wrote:</p>
<blockquote>
<p>transpose は自己双対で、zip と双対というわけでもないので、<br>
エイリアスには反対、かなぁ。</p>
<a name="1-2-3-4-5-6-7-8-9transpose-に対応するものって"></a>
<h1 >[[1, 2, 3], [4, 5, 6], [7, 8, 9]].transpose に対応するものって<a href="#1-2-3-4-5-6-7-8-9transpose-に対応するものって" class="wiki-anchor">¶</a></h1>
<a name="zip-で簡単にできましたっけ"></a>
<h1 >zip で簡単にできましたっけ?<a href="#zip-で簡単にできましたっけ" class="wiki-anchor">¶</a></h1>
</blockquote>
<p>irb(main):001:0> a = [[1, 2, 3], [4, 5, 6], [7, 8, 9]].transpose<br>
=> [[1, 4, 7], [2, 5, 8], [3, 6, 9]]<br>
irb(main):002:0> a.zip<br>
=> [[[1, 4, 7]], [[2, 5, 8]], [[3, 6, 9]]]<br>
irb(main):003:0> a[0].zip(*a[1,2])<br>
=> [[1, 2, 3], [4, 5, 6], [7, 8, 9]]</p>
<p>ですね。<br>
簡単っちゃ簡単だけど、ちょっと軸はずれてますね</p>
<p>--<br>
NARUSE, Yui <a href="mailto:naruse@airemix.jp" class="email">naruse@airemix.jp</a></p> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=199312011-08-01T23:34:27Zmrkn (Kenta Murata)muraken@gmail.com
<ul></ul><p>「zip が transpose の逆になっているか」<br>
を確認せず投稿したことがバレてしまいましたね。</p>
<p>私が欲しかったのは unzip のような zip の逆っぽいイメージの名前が付いている、<br>
zip する前の配列のセットが返ってくるメソッドです。</p>
<p>きしもとさんの</p>
<blockquote>
<p>transpose は自己双対で、zip と双対というわけでもないので、<br>
エイリアスには反対、かなぁ。</p>
</blockquote>
<p>という意見については、確かにその通りだと思いましたので、<br>
エイリアスではない方法で導入できるなら嬉しいです。</p> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=247512012-03-18T18:46:01Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li></ul> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=316672012-10-27T06:17:48Zko1 (Koichi Sasada)
<ul><li><strong>Assignee</strong> changed from <i>matz (Yukihiro Matsumoto)</i> to <i>mrkn (Kenta Murata)</i></li><li><strong>Target version</strong> changed from <i>2.0.0</i> to <i>2.6</i></li></ul><p>ええと,まつもとさんアサインで進むのだろうか?<br>
mrkn さんがまつもとさんを説得して,next minor から 2.0 に戻して下さい.</p> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=687032017-12-25T18:15:00Znaruse (Yui NARUSE)naruse@airemix.jp
<ul><li><strong>Target version</strong> deleted (<del><i>2.6</i></del>)</li></ul> Ruby master - Feature #5133: Array#unzip as an alias of Array#transposehttps://redmine.ruby-lang.org/issues/5133?journal_id=1074312024-03-22T07:59:25Zduerst (Martin Dürst)duerst@it.aoyama.ac.jp
<ul></ul><p>In issue <a class="issue tracker-5 status-1 priority-4 priority-default" title="Misc: DevMeeting-2024-04-17 (Open)" href="https://redmine.ruby-lang.org/issues/20336">#20336</a>, <a class="user active user-mention" href="https://redmine.ruby-lang.org/users/49162">@matheusrich (Matheus Richard)</a> wrote:</p>
<pre><code>* [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"]]
</code></pre>
<p>It's already alluded in some of the comments above: <code>transpose</code> is it's own inverse, so if we define <code>unzip</code> as an alias for <code>transpose</code>, <code>unzip</code> will be its own inverse.</p>
<p>The reason Haskell has <code>zip</code>/<code>unzip</code> as inverses is that <code>zip</code> in Haskell converts from two lists to a list of pairs, and <code>unzip</code> converts from a list of pairs to a pair of lists. In Haskell, <code>zip</code> and <code>unzip</code> are inverses (up to (un)currying).</p>
<p>In Haskell, the arguments of <code>zip</code> are limited to two lists. In Ruby, it's one array as receiver and one or more arrays as arguments. For <code>transpose</code>, it's an array of arrays. Only the later is fully generic and thus can be made exactly invertible.</p>