I am searching a recursive solution space and would like to return the first match I find. (I am using this to solve sudoku, but I guess this would apply to any recursively defined problem or datastructure).
So, in pseudocode, I need to call a function repeatedly with a set of possible input values, and return the first non-nil result:
for each possible input i:
r = do-something(i)
if r <> nil:
return r
return nil
My first attempt was to just translate this into clojure:
(defn find-first
[func values]
(loop [v values]
(if (empty? v)
nil
(if-let [ret (func (first v))]
ret
(recur (rest v))))))
But whenever I use (loop) it feels like I am programming C and not clojure, so I tried looking at this as applying a function to a sequence and returning the first non-nil result:
(defn find-first-2
[do-something range]
(->> range
(map do-something)
(remove nil?)
first))
This looks more like clojure, and should work the same way because of lazy sequences. However, lazy sequences actually chunk items in sets of 32, so when my ranges are shorter than 32 items this isn't really lazy at all.
I have found the "unchunk"-trick creating really lazy sequences, but this also doesn't really "feel" right...
What would be other ways of implementing this?