On 2/21/15 4:57 AM, Cecil Westerhof wrote:
I want to work with default values. For this I use:
     [& nr]

This gives the value null to nr. So far so good.

But I want to propagate the value, because I want to fill the default at a
higher level.

​I have:
     (defn test-lucky-numbers-all
       "Test all lucky number performance"
       [& nr]
     (printf "all: %s\n\n" nr)
       (time
        (do (test-lucky-numbers-first   nr)
and:
     (defn test-lucky-numbers-first
       "Performance test of lucky numbers (first)"
       [& nr]
     (printf "first: %s\n\n" nr)
>
When I do:
     (test-lucky-numbers-all)
The first prints:
     all: null
but the second prints:
     first: (nil)

And when I do:
     (test-lucky-numbers-all 1)​
​The first prints:
     all: (1)
and the second prints:
     first: ((1))

What is happening here and what can I do about it?


You can "unwrap" the varargs by destructuring them like so:

  (defn foo [a b & [c d]]
    (list a b (+ c d)))

  (foo 1 2 3 4) ;; => (1 2 7)

This is possible because the varargs are provided as a seq.

--
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
--- You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to