On 14 June 2011 12:37, Matthew Phillips <mattp...@gmail.com> wrote:

> The only way I can think of to write it in Clojure is:
>
> (reduce
>  (fn [items op]
>    (let [items1 (if (:delete op) (drop-index (:delete op) items)
> items)]
>      (if (:insert op) (cons (:insert op) items1) items1)))
>  items ops)
>
> i.e. I'm using a cascade of conditional let's. This isn't _too_ bad in
> this case, but you can image how unreadable this could get.
>

How about something like this (borrowing from Mark's loop/recur suggestion
above):

(defn maybe-delete [items op]
  (if-let [index (:delete op)]
    (drop-index index items)
    items))

(defn maybe-insert [items op]
  (if-let [new-item (:insert op)]
    (cons new-item items)
    items))

(loop [items items ops ops]
  (if-not ops
    items
    (let [op (first op)]
      (recur (-> items
                 (maybe-delete op)
                 (maybe-insert op))
             (next ops)))))

Looks more verbose on the surface, but I'm sure you could abstract away some
common bits of maybe-delete and maybe-insert using a macro.

Regards,
Stuart

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