Skip to content
Discussion options

You must be logged in to vote

The main difficulty is you may want to match on a tuple too.
Thankfully, (= [1] '(1)), which means we can check against tuple/type instead of requiring quote semantics.
Does this work for you?

(defn- switch*
  [& xs]
  (match xs
    [k v & rest]
    (if (and (tuple? k) (= :parens (tuple/type k)))
       [;(interleave k (seq [:repeat (length k)] v))
        ;(switch* ;rest)]
       [k v ;(switch* ;rest)])

    [v] [v]
    (error "unreachable")))
(defmacro switch
  [x & xs]
  (let [cases (switch* ;xs)]
    ~(case ,x
       ,;cases)))

It's an initial attempt and is largely untested, but hey, if it works eh?
The only testing I've done is:

(each e ["hi" "hallo" "ok" "nope"]
  (switch e

Replies: 2 comments 13 replies

Comment options

You must be logged in to vote
9 replies
@tionis
Comment options

@nstgc
Comment options

@tionis
Comment options

@tionis
Comment options

@nstgc
Comment options

Comment options

You must be logged in to vote
4 replies
@CosmicToast
Comment options

@nstgc
Comment options

@CosmicToast
Comment options

@nstgc
Comment options

Answer selected by nstgc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants