On Fri, Nov 30, 2012 at 10:15 AM, Alex Baranosky
<alexander.barano...@gmail.com> wrote:
> I've got a utility function I've been using called `conditionally-transform`
> which is a non-macro version of `test->`.

Likewise, except with use a HOF called "conditionalize":

(defn conditionalize
  "When called with a function f, returns a function that returns its
arguments if they
   arguments do not satisfy pred, or the result of the application of
f to its arguments otherwise."
  [pred f]
  (fn [& args] (if  (apply pred args) (apply f args) (apply identity args))))

Jay's example would then be (-> x ((conditionalize number? str))
(conditionalize string? count))), which tbh I like more because (a) it
doesn't introduce a new macro that can only be used in one syntactic
position (and there are other cases where you'd want to be able to
pass these transformed functions around) and (b) it groups the
transformations syntactically with their tests.

-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks,
which may be sweet, aromatic, fermented or spirit-based. ... Family
and social life also offer numerous other occasions to consume drinks
for pleasure." [Larousse, "Drink" entry]

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