If you’re interested in a transducer version of partition-when, here’s my code
from https://github.com/miner/transmuters
;; collection version by Frank on mailing list
;; SEM added transducer version, adapted from partition-by
(defn partition-when
"Applies f to each value in coll, start
Gorgeous! Thanks, Moe! (Chris says hi back :))
> On Aug 19, 2015, at 7:51 PM, Moe Aboulkheir wrote:
>
> Laurens,
>
> I don't think I've encountered an identical function I can point you to, but
> here's an alternative implementation:
>
> (defn p
Laurens,
I don't think I've encountered an identical function I can point you to,
but here's an alternative implementation:
(defn partition-when [pred coll]
(lazy-seq
(when-let [[h & t] (seq coll)]
(let [[run remains] (split-with (complement pred) t)]
red returns true.
I found an implementation that used partition-by on the mailing list, but this
had some issues: if multiple elems next to each other were true under the pred,
it wouldn’t create a new pred for each one.
I came up with the following implementation.
(defn ^:private partition-w
what so you
don’t even need to call f. Maybe you should for side-effects?
(defn partition-when
"Applies f to each value in coll, starting a new partition each time f
returns a
true value. Returns a lazy seq of partitions. Returns a stateful
transducer when no collection is pro
Tried myself and my first transducer for fun:
(defn partition-when
[f]
(fn [rf]
(let [a (java.util.ArrayList.)
fval (volatile! false)]
(fn
([] (rf))
([result]
(let [result (if (.isEmpty a)
result
Hi Frank,
I use a similar function that combines partition-by and partition-all:
(defn partition-when
[f coll]
(map (partial apply concat) (partition-all 2 (partition-by f coll
Martin.
On Wed, Mar 4, 2015 at 10:19 PM, Ivan L wrote:
> I went though almost the exact same exercise
gt; for some tests I need a function which starts a new partition each time a
> predicate returns true, for instance:
>
> (partition-when
> (fn [s] (.startsWith s ">>"))
> [">> 1" "2" "3" ">> 4" "5"
Hi all,
for some tests I need a function which starts a new partition each time a
predicate returns true, for instance:
(partition-when
(fn [s] (.startsWith s ">>"))
[">> 1" "2" "3" ">> 4" "5" "6"])
:=&g
artition-by to see if that would help. Turns
> out, what I really need are two complementary functions: take-to-first
> and partition-when. Take-to-first is similar to take-while, but is
> *inclusive* and also inverts the boolean. For example:
>
> Clojure=> (take-to-first even? [1 1
That implementation of partitions feels really low level. If you
implement the monadic version of partition-when (which I call
partition-where in my own code), it looks as simple as this:
(defn partitions [xs]
(run-seq (m-partition-where (const [false true]) xs)))
-Per
On Wed, Mar 17, 2010 at
Hey thanks :) These are different than partition-by and take-while.
partition-by triggers a new partition when the predicate value
*changes*, whereas partition-when triggers a new partition at any
point in the sequence where the predicate is true. take-while takes
all the items up to the point in
ems from coll up to
> and including the point at which it (pred item) returns true.
> pred must be free of side-effects."
> [pred coll]
> (lazy-seq
> (when-let [s (seq coll)]
> (if-not (pred (first s))
> (cons (first s) (take-to-first pred (res
azy-seq
(when-let [s (seq coll)]
(if-not (pred (first s))
(cons (first s) (take-to-first pred (rest s)))
(list (first s))
(defn partition-when
"Applies f to each value in coll, splitting it each time f returns
true. Returns a lazy seq of lazy seqs."
[
two complementary functions: take-to-first
and partition-when. Take-to-first is similar to take-while, but is
*inclusive* and also inverts the boolean. For example:
Clojure=> (take-to-first even? [1 1 1 1])
(1 1 1 1)
Clojure=> (take-to-first even? [1 1 1 1 2 3 3 3])
(1 1 1 1 2)
Clojure=>
15 matches
Mail list logo