On Jun 14, 4:40 pm, Alex Osborne <a...@meshy.org> wrote:
> Matthew Phillips <mattp...@gmail.com> writes:
> > 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.
>
> To avoid a "cascade of lets" I've occasionally defined a function called
> conj-if (or disj-if or assoc-if...). Like so:
>
>     (defn conj-if [coll test x]
>       (if test (conj coll x) coll))
>
>     (fn [items op]
>       (-> (conj-if (foo? op) "one")
>           (conj-if (bar? op) "two")))
>           (conj-if (baz? op) "three")))
>

Yes, I have thought about using -> to chain transformations together,
in fact it's a little like what I imagine the ST monad (or is it the
State monad?) does by chaining a series of apparent-variable-
assignments together.

I like this idea, but something about writing "xxx-if" functions
doesn't seem right, perhaps due to the number of xxx's there might be.
Perhaps I need a "do-if" fn that takes a target, predicate and a
transform fn, and applies the transform when pred is true. Will have a
think about that.

> On the other hand the cascading lets are really not that bad especially
> if you pull any complex predicate or value-manipulation logic out into
> separate functions:
>
>     (fn [items op]
>       (let [items (if (foo? op) (assoc items "foo" 1) items)
>             items (if (bar? op) (dissoc items "bar")  items)
>             items (if (baz? op) (empty items)         items)]
>         items))

Interesting, it hadn't occurred to me that I could just keep shadowing
"items" in the same let. Usually I think shadowing is a bad idea, but
perhaps it's better than "items1", "items2", etc.

Thanks!

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