On 12 Feb., 01:34, Timothy Pratley <timothyprat...@gmail.com> wrote:
> On 12 February 2010 03:19, Martin Hauner <martin.hau...@gmx.net> wrote:
>
> > I'm mostly interested if there is anything in my code one would/should
> > never do that way.
>
> Looks excellent, you've aced it!

Thanks :-)

> I'd like to discuss switching...You wrote:
>
> (cond
>   (empty? rolls)     0
>   (strike? rolls)    (+ (score-strike rolls) (score-after-strike rolls))
>   (spare?  rolls)    (+ (score-spare rolls)  (score-after-frame rolls))
>   (more?   rolls)    (+ (score-frame rolls)  (score-after-frame rolls)))
>
> Which is correct and very readable.
>
> If I wanted a DRYer expression:
> (condp #(%1 %2) rolls
>   empty?     0
>   strike?    (+ (score-strike rolls) (score-after-strike rolls))
>   spare?    (+ (score-spare rolls)  (score-after-frame rolls))
>   more?    (+ (score-frame rolls)  (score-after-frame rolls)))
>
> But the #(%1 %2) is ugly and not immediately obvious how it works.
> Would it be any nicer if we defined a function-name?
> or made a switchp which hides that part?

I have to understand condp step by step.

We have (condp pred expr) with  (test-expr result-expr) and condp does
call
(pred test-expr expr). With pred = #(%1 %2), expr = rolls, test-expr =
empty? etc..
So it finally calls (empty? rolls).

I see, but I have to admit that I prefer the simple cond. At least for
now ;-)

> Or use a trick:
> (condp apply [rolls]
>   ....
>   ....)

Indeed, works too.

Trying to understand that one now..
pred = apply, exp = [rolls], condp calls (apply empty? [rolls]) and
apply calls
(empty? rolls).

I like that better than the anonymous function.

Someone suggested to use a (binding) to avoid the repeating rolls
parameter. He wasn't sure if
it would be ok (learning too).
Since I have to change the code to calculate (score) in parallel
anyway it may be ok to make
rolls thread locale!?

> I've found myself faced with this particular DRY vs clear trade-off
> and never been sure what to do!
>
> Regards,
> Tim.

Thanks
  Martin

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