That's incredible: that my big loop could be collapsed into such
little code. Thanks a lot; I'm still wrapping my brain around these
functions, but they're so awesome.

On Dec 6, 1:20 pm, ataggart <[email protected]> wrote:
> On Dec 6, 11:28 am, samppi <[email protected]> wrote:
>
>
>
>
>
> > 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))))
>
> How about this?
>
> (let [v (take-while condition-true? an-initial-vector)
>       v (map modify-stack v)
>       m (reduce modify-heads an-initial-map v)]
>   (final-result v m))

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to