Occasionally I have to write a custom def macro, and this would make
life easier.  I would have to use it to provide specific feedback, but
it seems like  an idea worth pursuing.

On Dec 19, 3:58 pm, Stuart Halloway <stuart.hallo...@gmail.com> wrote:
> In Clojure it is idiomatic to have optional args at the front of the  
> signature. This makes it easy to define convenient caller APIs, but it  
> leads to bulky let forms like this one (from clojure.core/defmulti)
>
>    (let [docstring   (if (string? (first options))
>                        (first options)
>                        nil)
>          options     (if (string? (first options))
>                        (next options)
>                        options)
>          m           (if (map? (first options))
>                        (first options)
>                        {})
>          options     (if (map? (first options))
>                        (next options)
>                        options)
>          dispatch-fn (first options)
>          options     (next options)
>          m           (assoc m :tag 'clojure.lang.MultiFn)
>          m           (if docstring
>                        (assoc m :doc docstring)
>                        m)
>          m           (if (meta mm-name)
>                        (conj (meta mm-name) m)
>                        m)]
>
> Is it worth capturing this common idiom in a helper function, e.g. pop-
> optional-args:
>
> (defn pop-optional-args
>    [preds args]
>    (if (seq preds)
>      (if ((first preds) (first args))
>        (cons (first args) (pop-optional-args (rest preds) (rest args)))
>        (cons nil (pop-optional-args (rest preds) args)))
>      (list args)))
>
> The above let form would then be:
>
>    (let [[docstring m dispatch-fn options] (pop-optional-args [string?  
> map? identity] options)
>          m           (assoc m :tag 'clojure.lang.MultiFn)
>          m           (if docstring
>                        (assoc m :doc docstring)
>                        m)
>          m           (if (meta mm-name)
>                        (conj (meta mm-name) m)
>                        m)]
>
> Worth doing? If so, how could it be better?
>
> Stu

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to