From: kazuki@... Date: 2020-05-04T04:13:42+00:00 Subject: [ruby-core:98131] [Ruby master Feature#16828] Introduce find patterns Issue #16828 has been reported by ktsj (Kazuki Tsujimoto). ---------------------------------------- Feature #16828: Introduce find patterns https://bugs.ruby-lang.org/issues/16828 * Author: ktsj (Kazuki Tsujimoto) * Status: Open * Priority: Normal ---------------------------------------- I propose to add find patterns to pattern matching. # Specification ``` find_pattern: Constant(*var, pat, ..., *var) | Constant[*var, pat, ..., *var] | [*var, pat, ..., *var] ``` This patterns are similar to array patterns, but it finds first match values from the given object. ```ruby case ["a", 1, "b", "c", 2, "d", "e", "f", 3] in [*pre, String => x, String => y, *post] p pre #=> ["a", 1] p x #=> "b" p y #=> "c" p post #=> [2, "d", "e", "f", 3] end ``` Note that it doesn't support backtracking to avoid complexity. This means that the following code raises NoMatchingPatternError. ```ruby case [0, 1, 2] in [*, a, *] if a == 1 end ``` # Implementation * https://github.com/k-tsj/ruby/tree/find-pattern-prototype -- https://bugs.ruby-lang.org/ Unsubscribe: