Live data from Hacker News

Matchure: Serious Clojure Pattern Matching

spin.atomicobject.com

1–7 of 7 posts

Re: Matchure: Serious Clojure Pattern Matching

#3
Maybe I missed this from the article, but it would be really cool if someone would mash this pattern matching up in a macro with defn (maybe called "?defn" or something) so you could do stuff like the following without relying on apply:

    (defn qsort
      ([] '())
      ([Head & Tail] (let [lower (filter #(= % Head) Tail)]
                       (concat (apply qsort lower) (list Head) (apply qsort upper)))))
I have barely played around with Clojure though, so if there is a nicer way to express that code, please drop a comment and try not to tear me to pieces. My point is that I would love to have pattern matching available in function definitions similar to Haskell/Erlang.

Re: Matchure: Serious Clojure Pattern Matching

#4
post #3

Maybe I missed this from the article, but it would be really cool if someone would mash this pattern matching up in a macro with defn (maybe called "?defn" or something) so you could do stuff like the following without relying on apply: (defn qsort ([] '()) ([Head & Tail] (let [lower (filter #( = % Head) Tail)] (concat (apply qsort lower) (list Head) (apply qsort upper))))) I have barely played around with Clojure th…

You can handle your particular case with Clojure's fn argument destructuring. No need for pattern matching.

Re: Matchure: Serious Clojure Pattern Matching

#5
post #3

Maybe I missed this from the article, but it would be really cool if someone would mash this pattern matching up in a macro with defn (maybe called "?defn" or something) so you could do stuff like the following without relying on apply: (defn qsort ([] '()) ([Head & Tail] (let [lower (filter #( = % Head) Tail)] (concat (apply qsort lower) (list Head) (apply qsort upper))))) I have barely played around with Clojure th…

Hey mnemonik. I had the same thought. It's in the todo in the readme on github.

Re: Matchure: Serious Clojure Pattern Matching

#6
post #3

Maybe I missed this from the article, but it would be really cool if someone would mash this pattern matching up in a macro with defn (maybe called "?defn" or something) so you could do stuff like the following without relying on apply: (defn qsort ([] '()) ([Head & Tail] (let [lower (filter #( = % Head) Tail)] (concat (apply qsort lower) (list Head) (apply qsort upper))))) I have barely played around with Clojure th…

You can handle your particular case with Clojure's fn argument destructuring. No need for pattern matching.

Thanks for the info, didn't know you could do that! What about the classic factorial, though?

  (defn factorial
    ([0] 1)
    ([x] (* x (factorial (- x 1)))))

Re: Matchure: Serious Clojure Pattern Matching

#7
post #6

Earlier quoted context omitted.

You can handle your particular case with Clojure's fn argument destructuring. No need for pattern matching.

Thanks for the info, didn't know you could do that! What about the classic factorial, though? (defn factorial ([0] 1) ([x] (* x (factorial (- x 1)))))

That's an actual pattern match. Might as well use matchure ;)

  (use 'matchure)
  (defn-match factorial
    ([0] 1)
    ([?x] (* x (factorial (dec x)))))