1
0
mirror of https://github.com/janet-lang/janet synced 2024-12-01 04:19:55 +00:00

Address #293 - wildcard to match macro.

The _ symbol will match any value without creating a binding.
This commit is contained in:
Calvin Rose 2020-03-07 09:38:37 -06:00
parent e0c9910d85
commit c864828735
2 changed files with 9 additions and 3 deletions

View File

@ -2,6 +2,8 @@
All notable changes to this project will be documented in this file. All notable changes to this project will be documented in this file.
## Unreleased ## Unreleased
- Allow `_` in the `match` macro to match anything without creating a binding
or doing unification.
- Add `:range-to` and `:down-to` verbs in the `loop` macro. - Add `:range-to` and `:down-to` verbs in the `loop` macro.
- Fix `and` and `or` macros returning nil instead of false in some cases. - Fix `and` and `or` macros returning nil instead of false in some cases.
- Allow matching successfully against nil values in the `match` macro. - Allow matching successfully against nil values in the `match` macro.

View File

@ -1325,6 +1325,9 @@
[pattern expr onmatch seen] [pattern expr onmatch seen]
(cond (cond
(= '_ pattern)
(onmatch)
(symbol? pattern) (symbol? pattern)
(if (in seen pattern) (if (in seen pattern)
~(if (= ,pattern ,expr) ,(onmatch) ,sentinel) ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel)
@ -1374,14 +1377,15 @@
(defmacro match (defmacro match
"Pattern matching. Match an expression x against "Pattern matching. Match an expression x against
any number of cases. Easy case is a pattern to match against, followed any number of cases. Each case is a pattern to match against, followed
by an expression to evaluate to if that case is matched. A pattern that is by an expression to evaluate to if that case is matched. A pattern that is
a symbol will match anything, binding x's value to that symbol. An array a symbol will match anything, binding x's value to that symbol. An array
will match only if all of it's elements match the corresponding elements in will match only if all of it's elements match the corresponding elements in
x. A table or struct will match if all values match with the corresponding x. A table or struct will match if all values match with the corresponding
values in x. A tuple pattern will match if it's first element matches, and the following values in x. A tuple pattern will match if it's first element matches, and the following
elements are treated as predicates and are true. Any other value pattern will only elements are treated as predicates and are true. The last special case is
match if it is equal to x." the '_ symbol, which is a wildcard that will match any value without creating a binding.
Any other value pattern will only match if it is equal to x."
[x & cases] [x & cases]
(with-idemp $x x (with-idemp $x x
(def len (length cases)) (def len (length cases))