On Jun 14, 12:30 pm, Mark Engelberg <mark.engelb...@gmail.com> wrote:
> On Mon, Jun 13, 2011 at 7:37 PM, Matthew Phillips <mattp...@gmail.com> wrote:
> > List<Item> items = initialItems ();
>
> > for (Op op : operations)
> > {
> >  if (op.requiresDelete ())
> >    items.remove (op.indexToDelete ());
>
> >  if (op.requiresAdd ())
> >    items.add (op.indexToAdd (), op.newItem ());
> > }
>
> One way to transform this is loop-recur.
> (loop [items initialitems, ops (seq operations)]
>   (if-not ops items
>     (let [op (first ops)]
>         ;; Now you can write the logic pretty much the way you did in java
>         (cond
>              (.requiresDelete op)  (recur (.remove items (.op
> indexToDelete)) (next ops))
>              (.requiresAdd op) (recur (.add items (.op indexToAdd)
> (.op newItem)) (next ops)))))

Yes. I agree that can work, and that's what I've done in some other
situations, but it has the downside of lots of "recur" points
sprinkled around the loop body, which I think makes it almost as hard
to reason about as having lots of "return" statements in a Java
method. For small problems like this it's just as good/bad as my
solution, but what I'm really looking for is a good general idiom to
use whenever this sort of thing comes up.

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