The code snippet you provided is written in Clojure, a dialect of Lisp. The
function filter-collecting takes a predicate function, a collector
function, and multiple lists as arguments. It filters the lists based on
the predicate function and collects the values from the corresponding
positions
> (defn filter-collecting [predicate collector & lists]
> (for [v (apply map vector lists) :when (apply predicate v)] (apply
> collector v)))
This is very nice. It's short, sweet, and actually describes the
functionality.
-m
--~--~-~--~~~---~--~~
You received
On Aug 14, 3:43 pm, Fogus wrote:
> Wanna play golf?
ok...
Not efficient or elegant, but certainly weird...
(defn filter-collecting [p c & seqs]
(let [fun #(if (apply p %1)
(conj! %2 (apply c %1))
%2)]
(loop [hs (map first seqs)
ts (map rest seqs)
Let's play !
First:
Slight variation on Chouser's second version (presuming it should be faster
for the token comparison, which is evil - presuming performance instead of
testing it ! ) :
(let [x (Object.)]
(defn filter-collecting [p c & l]
(remove #(identical? % x)
(apply map #
(defn filter-collecting [predicate collector & lists]
(for [v (apply map vector lists) :when (apply predicate v)] (apply
collector v)))
On Fri, Aug 14, 2009 at 10:22 PM, Jarkko Oranen wrote:
>
>
>
> On Aug 14, 10:51 pm, Sean Devlin wrote:
> > I'd start with you usage docs
> >
> > ;; usage:
>
On Aug 14, 10:51 pm, Sean Devlin wrote:
> I'd start with you usage docs
>
> ;; usage:
> ;; (filter-collecting
> ;; (fn [x y] (< x y))
> ;; (fn [x y] (+ x y))
> ;; [1 7 3 9]
> ;; [5 5 5 5])
> ;; ==> (6 8)
;; usage:
;; (filter-collecting < +
;; [1 7 3 9]
;; [5 5 5 5])
;; ==> (6 8)
On Fri, Aug 14, 2009 at 3:43 PM, Fogus wrote:
>
> Wanna play golf?
Yes I do!
(defn filter-collecting [p c & l]
(remove nil? (apply map #(when % (apply c %&)) (apply map p l) l)))
But that gives incorrect results if c ever returns nil, so
I guess it should be:
(defn filter-collecting [p c & l
est lists) out)))
>
> ;; usage:
> ;; (filter-collecting
> ;; (fn [x y] (< x y))
> ;; (fn [x y] (+ x y))
> ;; '(1 7 3 9)
> ;; '(5 5 5 5))
> ;; ==> (6 8)
>
> More detail at:http://blog.fogus.me/2009/08/14/clojure-golf-episode-1/
>
>
pply collector heads) out))
(recur (map rest lists) out)))
;; usage:
;; (filter-collecting
;; (fn [x y] (< x y))
;; (fn [x y] (+ x y))
;; '(1 7 3 9)
;; '(5 5 5 5))
;; ==> (6 8)
More detail at: http://blog.fogus.me/2009/08/14/clojure