https://redmine.ruby-lang.org/https://redmine.ruby-lang.org/favicon.ico?17113305112020-05-04T09:03:08ZRuby Issue Tracking SystemRuby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=853762020-05-04T09:03:08Zshyouhei (Shyouhei Urabe)shyouhei@ruby-lang.org
<ul></ul><p>I am a noob in this area who don't understand what is good about it. Can you show us a bit more realistic example? The proposal kind of seems illustrative to me.</p> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=853772020-05-04T13:38:17Zshevegen (Robert A. Heiler)shevegen@gmail.com
<ul></ul><p>Is this a bit like .scan(), but applied to the pattern matching style? (Not<br>
sure if I understood this completely but it reminds me a bit of using .scan()<br>
and MatchData on the result possibly.)</p> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=858282020-05-27T00:07:18Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Suppose we have the following code:</p>
<pre><code>json = {name: "Alice", children: [{name: "Bob", age: 6}, {name: "Chad", age: 4}]}
case json
in {name: "Alice", children: [{name: "Bob", age: age}]}
p age
end
</code></pre>
<p>It doesn't match because array pattern does not search for the element. It only matches for an array with single element named "Bob". So to find an element in an array with multiple elements, you have to use find pattern like:</p>
<pre><code>json = {name: "Alice", children: [{name: "Bob", age: 6}, {name: "Chad", age: 4}]}
case json
in {name: "Alice", children: [*, {name: "Bob", age: age}, *]}
p age
end
</code></pre>
<p>Matz</p> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=858292020-05-27T00:09:24Zmatz (Yukihiro Matsumoto)matz@ruby.or.jp
<ul></ul><p>Accepted. But we have to clarify the term "backtracking" here. It does backtrack but not with a guard, right?</p>
<p>Matz.</p> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=858402020-05-27T19:07:01ZDan0042 (Daniel DeLorme)
<ul></ul><p>From the implementation linked above:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
<span class="k">in</span> <span class="p">[</span><span class="o">*</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="p">]</span>
<span class="nb">p</span> <span class="n">a</span> <span class="c1">#=> 0; non-greedy match</span>
<span class="k">end</span>
<span class="k">case</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">]</span>
<span class="k">in</span> <span class="p">[</span><span class="o">*</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="o">*</span><span class="p">]</span>
<span class="nb">p</span> <span class="n">b</span> <span class="c1">#=> 1; backtracking</span>
<span class="k">end</span>
</code></pre>
<p>So yes, it supports "backtracking" in a certain fashion otherwise the second case wouldn't match.</p>
<p>Note that it only supports splat expressions at the beginning and end of the array:</p>
<pre><code class="ruby syntaxhl" data-language="ruby"><span class="k">case</span> <span class="p">[</span><span class="mi">0</span><span class="p">,</span> <span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">,</span> <span class="mi">4</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="mi">6</span><span class="p">]</span>
<span class="k">in</span> <span class="p">[</span><span class="o">*</span><span class="p">,</span> <span class="n">a</span><span class="p">,</span> <span class="o">*</span><span class="p">,</span> <span class="n">b</span><span class="p">,</span> <span class="mi">5</span><span class="p">,</span> <span class="o">*</span><span class="p">]</span>
<span class="nb">p</span> <span class="n">a</span><span class="p">,</span> <span class="n">b</span>
<span class="k">end</span>
<span class="c1"># syntax error, unexpected ',', expecting ']'</span>
<span class="c1"># in [*, a, *, b, 5, *]</span>
</code></pre> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=859122020-05-31T09:59:01Zktsj (Kazuki Tsujimoto)kazuki@callcc.net
<ul><li><strong>Status</strong> changed from <i>Open</i> to <i>Assigned</i></li><li><strong>Assignee</strong> set to <i>ktsj (Kazuki Tsujimoto)</i></li></ul><blockquote>
<p>Accepted.</p>
</blockquote>
<p>Thank you. I'll commit it.</p>
<blockquote>
<p>But we have to clarify the term "backtracking" here. It does backtrack but not with a guard, right?</p>
</blockquote>
<p>Yes. This is exactly what I intend.</p> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=859152020-05-31T14:20:16Zsawa (Tsuyoshi Sawada)
<ul><li><strong>Description</strong> updated (<a title="View differences" href="/journals/85915/diff?detail_id=57209">diff</a>)</li></ul> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=861552020-06-14T00:28:14Zktsj (Kazuki Tsujimoto)kazuki@callcc.net
<ul><li><strong>Status</strong> changed from <i>Assigned</i> to <i>Closed</i></li></ul><p>Applied in changeset <a class="changeset" title="Introduce find pattern [Feature #16828]" href="https://redmine.ruby-lang.org/projects/ruby-master/repository/git/revisions/ddded1157a90d21cb54b9f07de35ab9b4cc472e1">git|ddded1157a90d21cb54b9f07de35ab9b4cc472e1</a>.</p>
<hr>
<p>Introduce find pattern [Feature <a class="issue tracker-2 status-5 priority-4 priority-default closed" title="Feature: Introduce find patterns (Closed)" href="https://redmine.ruby-lang.org/issues/16828">#16828</a>]</p> Ruby master - Feature #16828: Introduce find patternshttps://redmine.ruby-lang.org/issues/16828?journal_id=1066902024-02-12T12:54:46ZEregon (Benoit Daloze)
<ul></ul><p>An easy way to understand the "backtracking" of find pattern is it works like <code>each_slice(n)</code> and doesn't actually backtrack.<br>
So its performance is linear to the number of elements of the array being matched.<br>
<a href="https://github.com/ruby/ruby/commit/ddded1157a90d21cb54b9f07de35ab9b4cc472e1#diff-a5ba41b51e3655f9f244362a616282b5119d3e15dd6c52ee999bbdfcc5b86a77" class="external">https://github.com/ruby/ruby/commit/ddded1157a90d21cb54b9f07de35ab9b4cc472e1#diff-a5ba41b51e3655f9f244362a616282b5119d3e15dd6c52ee999bbdfcc5b86a77</a> has nice Ruby pseudo-code describing how it works.</p>