Tools for parsing arguments
Is there anything in core or contrib for parsing arguments to a function? Since Clojure fns don't support optional args or even keyword args, is there a standard set of helpers functions to do this? I looked around but I didn't see anything obvious. David --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: Tools for parsing arguments
On Feb 6, 10:06 am, David Nolen wrote: > Is there anything in core or contrib for parsing arguments to a > function? Since Clojure fns don't support optional args or even keyword > args, is there a standard set of helpers functions to do this? I looked > around but I didn't see anything obvious. No, but there is a pattern for keyword args: (defn my-function [& args] (let [options (apply hash-map args)] ...)) Then you can call (my-function :keyword value :keyword2 value). -Stuart Sierra --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: Tools for parsing arguments
That is remarkably simple and elegant. On Fri, Feb 6, 2009 at 10:20 AM, Stuart Sierra wrote: > > On Feb 6, 10:06 am, David Nolen wrote: > > Is there anything in core or contrib for parsing arguments to a > > function? Since Clojure fns don't support optional args or even keyword > > args, is there a standard set of helpers functions to do this? I looked > > around but I didn't see anything obvious. > > No, but there is a pattern for keyword args: > > (defn my-function [& args] > (let [options (apply hash-map args)] >...)) > > Then you can call (my-function :keyword value :keyword2 value). > > -Stuart Sierra > > > --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: Tools for parsing arguments
you can also use map destructuring. (defn x [{:keys [a b c]}] [a b c]) user=> (x {:a 1 :b 2 :c 3}) [1 2 3] On Fri, Feb 6, 2009 at 7:52 AM, Jeffrey Straszheim wrote: > That is remarkably simple and elegant. > > On Fri, Feb 6, 2009 at 10:20 AM, Stuart Sierra > wrote: >> >> On Feb 6, 10:06 am, David Nolen wrote: >> > Is there anything in core or contrib for parsing arguments to a >> > function? Since Clojure fns don't support optional args or even keyword >> > args, is there a standard set of helpers functions to do this? I looked >> > around but I didn't see anything obvious. >> >> No, but there is a pattern for keyword args: >> >> (defn my-function [& args] >> (let [options (apply hash-map args)] >>...)) >> >> Then you can call (my-function :keyword value :keyword2 value). >> >> -Stuart Sierra >> > > > > > -- And what is good, Phaedrus, And what is not good— Need we ask anyone to tell us these things? --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: Tools for parsing arguments
Stuart Sierra a écrit : > No, but there is a pattern for keyword args: > > (defn my-function [& args] > (let [options (apply hash-map args)] > ...)) > > Then you can call (my-function :keyword value :keyword2 value) And you can use destructuring to add support for defaults value: (defn my-function [& args] (let [{:keys [option1 option2] :or {option1 "default1" option2 "default2"}} (apply hash-map args)] ...)) Christophe -- Professional: http://cgrand.net/ (fr) On Clojure: http://clj-me.blogspot.com/ (en) --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: Tools for parsing arguments
Thanks all, Clojure list is the best. On Fri, Feb 6, 2009 at 11:27 AM, Christophe Grand wrote: > > Stuart Sierra a écrit : > > No, but there is a pattern for keyword args: > > > > (defn my-function [& args] > > (let [options (apply hash-map args)] > > ...)) > > > > Then you can call (my-function :keyword value :keyword2 value) > And you can use destructuring to add support for defaults value: > > (defn my-function [& args] > (let [{:keys [option1 option2] :or {option1 "default1" option2 > "default2"}} (apply hash-map args)] >...)) > > > Christophe > > -- > Professional: http://cgrand.net/ (fr) > On Clojure: http://clj-me.blogspot.com/ (en) > > > > > > --~--~-~--~~~---~--~~ 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 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 -~--~~~~--~~--~--~---
Re: Tools for parsing arguments
Hi, There is also a defnk macro by Rich, which allows easy definition of keyword args for functions. http://paste.lisp.org/display/69347 Sincerely Meikel smime.p7s Description: S/MIME cryptographic signature