Andrei,

You could just bind another local variable in the loop form:
(loop [ps pairs
       ret {}
       ffps (ffirst ps)]
  (cond (empty? ps) ret
        (some-test ffps) (recur (rest ps) (add-to-result ret ffps) (ffirst 
(rest ps)))
        :true (recur (rest ps) (do-sth-else ret ffps) (ffirst (rest ps)))) )

I think I like Andrew's solution better though.

But you probably want to use 'reduce' instead of 'loop' anyway:
(reduce (fn [ret [key val]] (if (some-test key) (add-to-result ret key) 
(do-sth-else ret key))) {} pairs)

Notice how you can destructure each pair in a convenient way.

BTW, do you really mean to call 'add-to-result' and 'do-sth-else' with a map 
and a key but no value?

Have all good days,
David Sletten

On Oct 28, 2010, at 8:49 PM, andrei wrote:

> Hi,
> 
> I have a code similar to this:
> 
> (def pairs (list [1 :a] [2 :b] [3 :c]))
> ...
> (loop [ps pairs, ret {}]
>    (cond (empty? ps) ret
>             (some-test (first (first ps))) (recur (rest ps) (add-to-
> result ret (first (first ps))))
>             :true (recur (rest ps) (do-smth-else ret (first (first
> ps))))))
> 
> You can see that string (first (first ps)), which actually gets first
> element of a first vector in pairs, occur many times. I can't move it
> before cond, since ps may be empty and evaluating of (first (first
> ps)) will produce an error (ok, it won't, but in general case it may).
> Is there another way to simplify code to not repeat one form several
> times?
> 
> -- 
> 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 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