From c864828735bef98e5b9a18015167133b75ee5662 Mon Sep 17 00:00:00 2001 From: Calvin Rose Date: Sat, 7 Mar 2020 09:38:37 -0600 Subject: [PATCH] Address #293 - wildcard to match macro. The _ symbol will match any value without creating a binding. --- CHANGELOG.md | 2 ++ src/boot/boot.janet | 10 +++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0dc68a30..72aa429f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ All notable changes to this project will be documented in this file. ## 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. - Fix `and` and `or` macros returning nil instead of false in some cases. - Allow matching successfully against nil values in the `match` macro. diff --git a/src/boot/boot.janet b/src/boot/boot.janet index 317d08ec..e7bd9491 100644 --- a/src/boot/boot.janet +++ b/src/boot/boot.janet @@ -1325,6 +1325,9 @@ [pattern expr onmatch seen] (cond + (= '_ pattern) + (onmatch) + (symbol? pattern) (if (in seen pattern) ~(if (= ,pattern ,expr) ,(onmatch) ,sentinel) @@ -1374,14 +1377,15 @@ (defmacro match "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 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 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 - elements are treated as predicates and are true. Any other value pattern will only - match if it is equal to x." + elements are treated as predicates and are true. The last special case is + 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] (with-idemp $x x (def len (length cases))