This looks like a standard thing you might want to do with a transducer: 
accept a stream of inputs and statefully group them into a stream of 
varying-length vectors.  A typical example might be to accept a series of 
bowling throws and emit them grouped into bowling frames.  So you have a 
generic "vectorTransducer" which is defined by the rule as to when one 
frame is full and the next begins.  Does such a thing have a standard name?

Thanks!

Marshall

(defn vectorTransducer [timeForNextVector]
  (fn [rf]
    (let [pending (volatile! [])]
      (fn
        ([] (rf))
        ([result] (if (not-empty @pending)
                    (rf result @pending)
                    (rf result)))
        ([result input]
         (let [appended (conj @pending input)]
           (if (timeForNextVector appended)
             (do (vreset! pending [])
                 (rf result appended))
             (do (vreset! pending appended)
                 result))))))))

(defn bowlingTransducer[]
  (vectorTransducer
   (fn ([v]
        (or (<= 2 (count v))
            (<= 10 (reduce + v)))))))

(into [] (bowlingTransducer)
      (concat (range 11) (range 11)))

-- 
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
--- 
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+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to