I use the philosophy, I've got this nice hammer with a nail remover on the
back.  It's a natural fit that sometimes you want to pull nails out in
addition to hammering nails in.  For those two jobs, I don't need to be
walking around with a special nail-remover-only tool.  and if has fewer
characters.

I actually got confused and started thinking of when as a loop because I
missed the recursive call.  Now THAT could be nice.  It would be like "keep
looping without going into the body.  go into the body 'when' the following
is true".  That's not quite 'until' or 'while not'.  It would be like if's
special form but with a loop.  Now I have some incentive to understand
macros, I think!  I think the closest thing is the loop-recur mechanism
combined with the if clause that has to manually feed something back to the
loop.

(whenever <some-pred applied to some list>
       (blah blah blah)  ; predicate finally satisfied on the nth element.
that element is now in the first spot of the list I have a handle to.  I
don't even need a do loop for side effects
       (blah blah blah)  ; Whatever I want to do
       (...)
       (...) the return of the last statement is returned)

--> returns nil if it never goes in the list

Sorry for sort of changing the subject (although we were comparing two ways
to loop for the same purpose), but would this be a cool construct?


On Sat, Jan 24, 2009 at 10:47 AM, Meikel Brandmeyer <m...@kotka.de> wrote:

> Hi,
>
> Am 24.01.2009 um 15:33 schrieb wubbie:
>
>  The question is when to use  when or if.
>>
>
> 'When' does have only one branch, while if has two.
>
> (when test the-branch)
> (if test then else)
>
> I use 'when' for side-effects or in case there is only
> one interesting branch.
>
> (defn do-something
>  [x]
>  (when verbose?
>    (println "I'm doing something"))
>  ...)
>
> Or the take example:
>
> (defn take
>  [n coll]
>  (when (seq coll)
>    ...))
>
> There are style guides, which say:
> "Use if for two branch statements and when for
> one branch statements."
>
> I've been following this line, but using if for both
> is also possible. YMMV.
>
> Sincerely
> Meikel
>
>

--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to