I've read that loop/recur is less preferable to using higher-level
function calls like reduce and for and map, especially when chunked
seqs are implemented. But is can the loop below be rewritten to use
those functions instead? It's a loop that iterates over one vector and
changes both the vector and a map until a certain condition becomes
true. I can't figure out how to use Clojure's high-level functions
instead of loop/recur:

(Let an-initial-vector be a vector; an-initial-map be a map; and
modify-stack, modify-heads, and final-result be functions.)

(loop [current-stack an-initial-vector
       current-heads an-initial-map
       current-index 0]
  (let [current-element (current-stack current-index)]
    (if (or (condition-true? current-element)
            (>= index (count current-stack)))
      (recur
        (update-in current-stack [current-index]
          modify-stack)
        (modify-heads current-heads current-element)
        (inc current-index))
      (final-result current-stack current-heads))))

If it helps, it's possible that current-stack and current-heads to be
merged into a single map that contains a :stack and a :heads key:

(loop [current-data {:stack an-initial-vector
                     :heads an-initial-map}
       current-index 0]
  (let [current-element ((:stack current-data)
                         current-index)]
    (if (or (condition-true? current-element)
            (>= index (count current-stack)))
      (recur
        (-> current-data
          (update-in [:stack current-index] modify-stack)
          (update-in [:heads] modify-heads current-element))
        (inc current-index))
      (final-result current-data))))

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