What I tend to do when I run into this situation is to split my function in
two and provide:
1. an API function that accepts the key/value pairs as named arguments -
per the library coding guidelines
2. an implementation function that accepts a map of args as its last
argument (and destructures i
+10
If the library provided (this :arg style) just to be a convenience for me,
then that purpose completely backfired the moment I had to type (apply
your-fn (apply concat my-args)).
That unnecessary dance was the inspiration for me buying applyconcat.com
(empty; site ideas welcome).
-Steven
I agree, this approach fits with our code base.
Apply induces a significant hit and should be avoided when performance is
critical. I tend to let the computer do its job and free my brain estate for
more
useful things until performance becomes critical. A kind of laziness :))
Luc P.
> My rule
I think it's more a matter of taste at this point and what choices were made
earlier.
I chose the explicit destructuring route on optional args in the fn definition
a while ago.
Nothing prevents us from dealing with anonymous option maps when appropriate,
merging, sub-selecting keys, ... (:or ..
My rule of thumb for this is if that's something that will be "static" (as
in, set once in the source and never changes) kw options are fine, if it is
likely to be manipulated, is the result of some previous computation, then
a map fits better.
apply also has a performance cost that's not alwa
I was not aware of this, we used a macro I think in a few places, mainly to
avoid adding an extra call.
Luc P.
> The keyword arguments vs. (apply (apply)) issue has come up before:
> https://groups.google.com/d/topic/clojure-dev/9ctJC-LXNps/discussion
> My take is: Use keyword arguments wherever
On 18 June 2013 14:38, Softaddicts wrote:
> I use destructuring most of the time, the main benefits I see are runtime
> validation
> of typo errors in the option names, better doc strings and the ability to
> provide defaults
> where nil does not make any sense.
>
You can use destructuring on ro
The keyword arguments vs. (apply (apply)) issue has come up before:
https://groups.google.com/d/topic/clojure-dev/9ctJC-LXNps/discussion
My take is: Use keyword arguments wherever appropriate and keep apply-kw
handy: http://dev.clojure.org/jira/browse/CINCU-3
2013/6/18 Softaddicts
> I use destr
I use destructuring most of the time, the main benefits I see are runtime
validation
of typo errors in the option names, better doc strings and the ability to
provide defaults
where nil does not make any sense.
Of course you may need to use apply to pass options in turn to another fn
but I found
+1 James.
I can't count the number of times I've found myself unrolling an options
map with a dance like this apply / apply concat.
And don't forget that the function itself then has to roll this options map
back up to operate on it, which syntactically isn't all that bad, but it
usually seem
The code for dmirylenka's release-sharks form tdsl.search> (defn release-sharks [n & {:keys [laser-beams]}] (if (boolean laser-beams) (repeat (int n) :shark)))#'tdsl.search/release-sharkstdsl.search> (release-sharks 2 :laser-beams 1)(:shark :shark) 18.06.2013, 14:32, "dmirylenka" :Accor
I somewhat disagree with the coding standards in certain cases. If you have
a large number of options, you may find yourself creating the option map
programmatically. In which case:
(release-sharks 2 options)
Is preferable to:
(apply release-sharks 2 (apply concat options))
- James
On
According, to the library coding standards, the first is better:
(release-sharks 2 :laser-beams true); good
(release-sharks 2 {:laser-beams true}) ; bad
http://dev.clojure.org/display/design/Library+Coding+Standards
On Tuesday, June 18, 2013 5:26:15 PM UTC+12, Omer Iqbal wrote:
>
> Hey folk
Hey folks,
What'c considered more idiomatic when having multiple, optional arguments?
(defn foo1 [a b & args]
(let [opts (apply hash-map args]
...))
or
(defn foo2 [a b opts]
...)
Cheers,
Omer
--
--
You received this message because you are subscribed to the Google
Groups "Clojure"
14 matches
Mail list logo