More idiomatic way to find the first non-nil function result?

1,629 views
Skip to first unread message

Steven Degutis

unread,
May 1, 2013, 12:13:33 PM5/1/13
to clo...@googlegroups.com
I've found myself writing a validator function.

It has a list of internal validator functions, and should return the first one that evaluates to non-nil, without evaluating the rest.

Here's the code I've come up with:

(defn validate-something [data-1 data-2 data-3]
  (some #(%)
        [#(validate-data-1 data-1)
         #(validate-data-2 data-2)
         #(validate-data-3 data-3)]))

But this code just feels ugly and I feel like it could be more idiomatic.

For one thing, it shares something in common with cond, since it should return the first one that returns non-nil. But each cond clause takes two params, the test and the result, whereas this is just one and the same. So I don't know how I would use cond with it.

Alternatively, that property could be implemented by the lazy-evaluation of map. My first impulse was to do (find-first (map apply [f1 f2 f3])) but first of all I'd have to write find-first (or use something in contrib), and second of all apply seems to expect args and gave me an error. But maybe that last part was just me being overtired.

Does anyone here have a more idiomatic or elegant solution than the (some) one above?

-Steven

Alex Robbins

unread,
May 1, 2013, 12:22:51 PM5/1/13
to clo...@googlegroups.com
This is similar to, but not exactly the same, as some-fn. Maybe the source of that could guide you?



--
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clo...@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+u...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups "Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure+u...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Cedric Greevey

unread,
May 1, 2013, 1:46:36 PM5/1/13
to clo...@googlegroups.com
What about

(or
  (validate-data-1 data-1)
  (validate-data-2 data-2)
  (validate-data-3 data-3))

?

Or

(some identity
  (map #(%1 %2)
       [validate-data-1 validate-data-2 validate-data-3]
       [data-1 data-2 data-3]))

(though that might realize elements past the first non-nil one if vectors produce chunked seqs).



--

Steven Degutis

unread,
May 1, 2013, 2:30:35 PM5/1/13
to clo...@googlegroups.com
Oh yeah, "or"! Perfect!

Gary Trakhman

unread,
May 20, 2013, 2:19:05 PM5/20/13
to clo...@googlegroups.com
boolean algebra... demorgan's laws.. etc..http://en.wikipedia.org/wiki/De_Morgan's_laws


On Mon, May 20, 2013 at 1:38 PM, Jeb <jebb...@gmail.com> wrote:
Is there a term/phrase that describes this style of programming (as opposed to nested ifs, for example)?


On Wednesday, May 1, 2013 12:46:36 PM UTC-5, Cedric Greevey wrote:
What about

(or
  (validate-data-1 data-1)
  (validate-data-2 data-2)
  (validate-data-3 data-3))

--
Reply all
Reply to author
Forward
0 new messages