Jimmy Hoffa
Jimmy Hoffa

Reputation: 5967

Macro want to use symbol instead of string in clojure

So trying to make something like the haskell lambda syntax, and with a macro this is what I've got:

(defmacro / [& all]
  (let [args (take-while #(not (= %1 "=>")) all)
        argCount (count args)
        expr (last (split-at (+ argCount 1) all))]
    `(fn ~(vec args) (~@expr))))

(reduce (/ x y "=>" + x y) [1 2 3])

This works well enough, but the last thing I'd like to do is make it so I don't need the "=>" but can just use =>

Any tips how I might make => a valid symbol that I can just parse in the context as I'm referring?

Upvotes: 1

Views: 180

Answers (1)

Jouni K. Seppänen
Jouni K. Seppänen

Reputation: 44118

Compare the name of the symbol against the string:

(defmacro / [& all]
  (let [args (take-while #(not (= (name %1) "=>")) all)
        argCount (count args)
        expr (last (split-at (+ argCount 1) all))]
    `(fn ~(vec args) (~@expr))))

Upvotes: 1

Related Questions