I really liked this one too, though my first draft of it was more like
(defn pipe [test f]
  (fn [value]
    (...)))

I've since expanded and generalized it into three related functions
you can see at 
https://github.com/flatland/useful/blob/develop/src/useful/fn.clj#L16

fix is basically your pipe, except it takes a series of test/f pairs,
and to-fix is my original version, returning a function of x instead
of computing on x immediately (this is handy as you can map (to-fix
string? read-string) over a collection, for example).

Given accepts only one clause and is "shaped" right for use in ->
thread chains, like:

(-> x
    inc
    (given even? / 2))

I would love to see something like one of these make it into Clojure
proper, but given that I usually complain about clojure.core being too
big, I would be perfectly happy to see it in some other namespace. In
the meantime, take useful for a spin, and see if the enhanced versions
of pipe are of any use to you.

If nothing else, I recommend you change the argument order to your
pipe function: (defn pipe [value test f]) is clearly the "correct"
signature, since it means that (like fix) you can use it in update-in/
alter/swap!/-> chains.

On Oct 14, 3:47 pm, Daniel Bell <dchristianb...@gmail.com> wrote:
> One higher-order function I've found useful as I've goofed around with
> clojure has been one that I made up myself, but it's proved so useful
> and so simple that I have to believe it's in core somewhere:
>
> (defn pipe [test value f]
>   (if (test value)
>         (f value)
>         value))
>
> Is this a core function I'm just missing?

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