Hi,
This thread is interesting because we have a solution that is
suggested : using map then filter then take then count, which will
result in creating, for at least num pixels, at worst the full number
of pixels, 2 conses, and 3 times walking the array.
What's more interesting to me is that the ultimate result is not to
get a transformation of the input preserving the structure (a
"collection" of pixels), but a reduced value.
So it really seems to me that the missing abstraction, here, is being
able to do a reduce over the list of pixels, and being able, from the
reduction function, to quit the reduction early.
If we had this function, then there would be no new cons, and just one
walk of the list, while still being very functional.
Here is how the enhanced version of reduce could work :
(defn interesting? [pixels c]
(reduce (fn [counter pixel] (if (in-interval? pixel) (inc counter) counter))
0
pixels
(partial < c)))
The new optional argument to reduce would be a function applied to the
accumulated value before each invocation of the reducing function, and
if it returns true, then reduce would quit and return the currently
accumulated value.
Another way to implement the new version of reduce would be by having
the 4th parameter be an explicit "early-return value", and let the "do
we return early ?" test be part of the reduction function ?
Do you see a value in implementing such a new function (or extension
to existing reduce) ?
Of course, if one does not want to implement the general problem
solving solution, a good old walk via 'loop seems appealing to this
exact problem (I presume walking over pixels would have to be as fast
as possible) :
(defn interesting? [pixels interest-threshold]
(loop [count 0 pixels (seq pixels)]
(cond
(>= count interest-threshold) true
pixels (recur (if (in-interval? (first pixels)) (inc count)
count) (next pixels))
:else false)))
OK, ok, this one is rather verbose :-)
Regards,
--
Laurent
2009/6/13 CuppoJava <
patrick...@hotmail.com>: