Hi Asim,
Thank you for the code and the explanation of the context.
I noticed that partition-after could be implemented by taking my
previous code and incrementing all the 'true' indices by one. My
first stab:
(defn partition-after [pred coll]
(if (empty? coll)
'()
(let [indices (add-
On Sun, Nov 28, 2010 at 5:23 PM, Asim Jalis wrote:
> On Sun, Nov 28, 2010 at 1:17 PM, Ken Wesson wrote:
>> The implementation I posted earlier appears to fit the contract of
>> your partition-before (and, additionally, works properly if the input
>> starts with (nil ...) :)).
>
> Good catch :) I
On Sun, Nov 28, 2010 at 1:17 PM, Ken Wesson wrote:
> The implementation I posted earlier appears to fit the contract of
> your partition-before (and, additionally, works properly if the input
> starts with (nil ...) :)).
Good catch :) I should have used (empty? coll) instead of (nil?
first-item)
The implementation I posted earlier appears to fit the contract of
your partition-before (and, additionally, works properly if the input
starts with (nil ...) :)).
There is another possible variation: partition-dropping. Here the
elements for which pred returns true don't get included in the outpu
Here is partition-before and partition-after -- partition-after is simpler.
(defn partition-before-internal [pred coll]
(let [first-item (first coll)]
(if (nil? first-item) ()
(letfn [(get-partial-partition-with-item []
(let [partial-partition (partition-before-internal pr
On Sat, Nov 27, 2010 at 5:53 PM, Benny Tsai wrote:
> If you don't mind, I would love to see your version with lazy-seq and
> recursion. Seems like that's the idiomatic way of solving problems
> like this, judging by the source for the partition functions.
Hi Benny,
Certainly. My solution is app
Huh, didn't know that about 'distinct'. Thank you for the tip.
On Nov 27, 9:18 pm, Ken Wesson wrote:
> On Sat, Nov 27, 2010 at 11:03 PM, Benny Tsai wrote:
> > That's what I get for coding without taking my afternoon nap :)
>
> > Try 3. Tweaked 'subsequence' and 'split-when' so that it is no lo
On Sat, Nov 27, 2010 at 11:03 PM, Benny Tsai wrote:
> That's what I get for coding without taking my afternoon nap :)
>
> Try 3. Tweaked 'subsequence' and 'split-when' so that it is no longer
> necessary to calculate the index for the end of the collection. Also
> added a check to 'split-when' t
That's what I get for coding without taking my afternoon nap :)
Try 3. Tweaked 'subsequence' and 'split-when' so that it is no longer
necessary to calculate the index for the end of the collection. Also
added a check to 'split-when' to return an empty list when called on
an empty collection; oth
On Sat, Nov 27, 2010 at 8:35 PM, Benny Tsai wrote:
> Here's a fixed version that...
>
> 1. Is lazy everywhere.
> 2. No longer loses elements :)
...
> (defn split-when [pred coll]
> (let [coll-end (count coll)
Er, did you just say "is lazy everywhere"? :)
--
You received this message because
If you don't mind, I would love to see your version with lazy-seq and
recursion. Seems like that's the idiomatic way of solving problems
like this, judging by the source for the partition functions.
On Nov 27, 10:02 am, Asim Jalis wrote:
> I want to partition a sequence based on the values of a
I really dig the succinctness of your folding solutions.
On Nov 27, 5:52 pm, rob levy wrote:
> fwiw my folding solution above yields the expected result.
>
>
>
>
>
>
>
> On Sat, Nov 27, 2010 at 7:07 PM, Benny Tsai wrote:
> > > Subvec, however, isn't lazy.
>
> > That's true. I just realized ther
Here's a fixed version that...
1. Is lazy everywhere.
2. No longer loses elements :)
(use '[clojure.contrib.seq :only (indexed)])
(defn index-filter [pred coll]
(when pred
(for [[idx elt] (indexed coll) :when (pred elt)] idx)))
(defn subsequence [coll start end]
(take (- end start) (dro
fwiw my folding solution above yields the expected result.
On Sat, Nov 27, 2010 at 7:07 PM, Benny Tsai wrote:
> > Subvec, however, isn't lazy.
>
> That's true. I just realized there's another problem with my approach
> as well: it does not preserve elements before the first true index.
>
> user
> Subvec, however, isn't lazy.
That's true. I just realized there's another problem with my approach
as well: it does not preserve elements before the first true index.
user=> (partition-when true? [false true])
([true])
When the result should probably be:
user=> (partition-when true? [false t
This is clearer to read though:
(defn partition-when [f l]
(reduce #(if (f %2)
(conj %1 (vector %2))
(conj (vec (butlast %1))
(conj (vec (last %1)) %2)))
[] (vec l)))
On Sat, Nov 27, 2010 at 4:47 PM, rob levy wrote:
> This is more correct
This is more correct, because conj does not preserve the order correctly. I
wonder if there is a more idiomatic way to cons things onto the end of the
list.
(defn partition-when [f l]
(reduce #(if (f %2)
(concat %1 (vector (vector %2)))
(concat (butlast %1)
Something like this?
(defn partition-when [f l]
(reduce #(if (f %2)
(conj %1 (vector %2))
(conj (butlast %1)
(conj (last %1) %2)))
[] l))
On Sat, Nov 27, 2010 at 4:15 PM, Ken Wesson wrote:
> On Sat, Nov 27, 2010 at 4:00 PM, rob levy wrot
On Sat, Nov 27, 2010 at 4:00 PM, rob levy wrote:
> partition-by does exactly what you need.
Not quite.
user=> (take 10 (partition-by #(= 0 (rem % 3)) (iterate inc 1)))
((1 2)
(3)
(4 5)
(6)
(7 8)
(9)
(10 11)
(12)
(13 14)
(15))
At first it seems you can fix this as follows:
user=> (defn
2010/11/27 rob levy
> 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 wrote:
>
>> Could do it this way. Use index-filter (borrowed from Programmin
Subvec, however, isn't lazy. This is:
(defn split-when [pred coll]
(let [ipred (complement pred)
bits (iterate
(fn [[out coll]]
(let [[a b] (split-with ipred (rest coll))]
[(cons (first coll) a) b]))
[nil coll])]
(map
partition-by does exactly what you need.
On Sat, Nov 27, 2010 at 3:04 PM, Benny Tsai 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
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
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))
24 matches
Mail list logo