JanetDocsI'm feeling luckyGithub sign in

match



    macro
    boot.janet on line 1583, column 1

    (match x & cases)
    
    Pattern matching. Match an expression x against 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. 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.


1 exampleSign in to add an example
Loading...
(def a-tuple-1 [:number 10])
(def a-tuple-2 [:string "Hello there!"])
(defn show [what]
  (match what
    [:number n] (printf "It's a number! %d" n)
    [:string s] (printf "Here's a string: %s" s)
    _ (printf "I dunno what this is: %q" what)))

(show a-tuple-1)
(show a-tuple-2)
(show [:array @[]])
roobie