2010/11/27 rob levy <r.p.l...@gmail.com>

> partition-by does exactly what you need.


Nope.

partition-by will split each time the value changes. Not each time a
particular value is seen.


>
>
> On Sat, Nov 27, 2010 at 3:04 PM, Benny Tsai <benny.t...@gmail.com> wrote:
>
>> Could do it this way.  Use index-filter (borrowed from Programming
>> Clojure) to get the indices where the predicate is true in the
>> sequence.  partition-when-true then just calls subvec with pairs of
>> indices to get the subsequences.
>>
>> coll-end is the index just past the end of the collection.  This is
>> used as a pad element so that the last true index is included in the
>> pairs, and also accounts for the case where there is only a single
>> true index.
>>
>> (use '[clojure.contrib.seq :only (indexed)])
>>
>> (defn index-filter [pred coll]
>>  (when pred
>>    (for [[idx elt] (indexed coll) :when (pred elt)] idx)))
>>
>> (defn partition-when [pred coll]
>>  (let [coll-end (count coll)
>>        indices (index-filter pred coll)
>>        index-pairs (partition 2 1 [coll-end] indices)
>>        coll-vec (vec coll)]
>>    (for [[start end] index-pairs] (subvec coll-vec start end))))
>>
>> user=> (partition-when true? '(true false false true false true true))
>> ([true false false] [true false] [true] [true])
>>
>> On Nov 27, 10:02 am, Asim Jalis <asimja...@gmail.com> wrote:
>> > I want to partition a sequence based on the values of a predicate so
>> > that every true starts a new sequence in the partition. Here is an
>> > example of how partition-with could be used:
>> >
>> > (partition-when true? '(true false false true false true true))
>> >   -> '((true false false) (true false) (true) (true))
>> >
>> > I have written this lazy-seq and recursion. However, I was wondering
>> > if there is a way to do this with core Clojure sequence primitives.
>> > Any suggestions?
>> >
>> > Asim
>> > --
>> > San Francisco, CA
>>
>> --
>> 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<clojure%2bunsubscr...@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 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<clojure%2bunsubscr...@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 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

Reply via email to