Re: How to be lazy…

2012-02-24 Thread JuanManuel Gimeno Illa
This solution, I think, does not do more map first than needed, avoids computing len and pieces more than once, uses nthnext to avoid extra cost of drop and computes the example (first (second (split-at-subsequence [1 2] (range (defn split-at-subsequence [mark input] (when-let [sequenc

Re: How to be lazy…

2012-02-24 Thread Cedric Greevey
On Fri, Feb 24, 2012 at 2:43 PM, Alan Malloy wrote: > Why would you (remove nil? (map f coll))? That's what keep is for: > (keep f coll). Why would you (remove nil? (map f coll))? Because you didn't know about keep, obviously. :) -- You received this message because you are subscribed to the Go

Re: How to be lazy…

2012-02-24 Thread Alan Malloy
Why would you (remove nil? (map f coll))? That's what keep is for: (keep f coll). On Feb 24, 11:20 am, Cedric Greevey wrote: > On Fri, Feb 24, 2012 at 2:17 PM, JuanManuel Gimeno Illa > > wrote: > > What I don't understand of your solution is the (map seq (step pieces)) > > because for me it is c

Re: How to be lazy…

2012-02-24 Thread Cedric Greevey
On Fri, Feb 24, 2012 at 2:17 PM, JuanManuel Gimeno Illa wrote: > What I don't understand of your solution is the (map seq (step pieces)) > because for me it is clear that each of the sequences generated by step is a > seq, so why do you need to seq it? It's the combination of (remove nil? (map se

Re: How to be lazy…

2012-02-24 Thread JuanManuel Gimeno Illa
El viernes 24 de febrero de 2012 18:46:03 UTC+1, Cedric Greevey escribió: > > On Fri, Feb 24, 2012 at 11:30 AM, JuanManuel Gimeno Illa wrote: > > > I think it would be better to create the lazy-seq over a function that > uses > > the pieces in order to not to explode input with partition-all and

Re: How to be lazy…

2012-02-24 Thread Cedric Greevey
On Fri, Feb 24, 2012 at 11:30 AM, JuanManuel Gimeno Illa wrote: > Maybe this version is clearer: > > (defn split-at-subsequence [mark input] >     (when-let [sequence (seq input)] >         (let [len       (count mark) >               pieces   (partition-all len 1 sequence) >               [fst rs

Re: How to be lazy…

2012-02-24 Thread JuanManuel Gimeno Illa
One variation making the lazy-sequence only over the pieces: (defn split-at-subsequence [mark input] (let [len (count mark) split-at-subseq (fn split-at-subseq [pieces] (when (seq pieces) (let [[fst rst] (sp

Re: How to be lazy…

2012-02-24 Thread JuanManuel Gimeno Illa
Maybe this version is clearer: (defn split-at-subsequence [mark input] (when-let [sequence (seq input)] (let [len (count mark) pieces (partition-all len 1 sequence) [fst rst] (split-with #(not= mark %) pieces) head (map first fst)

Re: How to be lazy…

2012-02-24 Thread JuanManuel Gimeno Illa
One implementation using only one recursive function and the sequence library: (defn split-at-subsequence [mark input] (when-let [sequence (seq input)] (let [len (count mark) pieces (partition-all len 1 sequence) [fst rst] (split-with #(not= mark %)

Re: How to be lazy…

2012-02-23 Thread Wolodja Wentland
On Thu, Feb 16, 2012 at 14:06 -0600, Michael Gardner wrote: > On Feb 16, 2012, at 11:08 AM, Wolodja Wentland wrote: > > > Thanks for the explanation. What would be a good way to ensure that the > > subseqeuence are lazy too? > > I can't think of a good way to do this with higher-order functions,

Re: How to be lazy…

2012-02-16 Thread Michael Gardner
On Feb 16, 2012, at 11:08 AM, Wolodja Wentland wrote: > Thanks for the explanation. What would be a good way to ensure that the > subseqeuence are lazy too? I can't think of a good way to do this with higher-order functions, so you'll probably have to write a recursive function that "manually" i

Re: How to be lazy…

2012-02-16 Thread Wolodja Wentland
On Thu, Feb 16, 2012 at 10:51 -0600, Michael Gardner wrote: > partition-by is lazy in that it only splits its argument into as many > subseqs as requested; the subseqs themselves are not lazy. So when you > partition-by the result of (replace-subseq-with-sentinel [1 2] sentinel > (range)) and then

Re: How to be lazy…

2012-02-16 Thread Michael Gardner
partition-by is lazy in that it only splits its argument into as many subseqs as requested; the subseqs themselves are not lazy. So when you partition-by the result of (replace-subseq-with-sentinel [1 2] sentinel (range)) and then request the infinite third subseq thereof, partition-by won't ter

How to be lazy…

2012-02-16 Thread Wolodja Wentland
Hi all, I am trying to implement a function that splits any given sequence on a subsequence. I would like it to behave like this: user> (split-at-subsequence [5 6] (range 10)) ((0 1 2 3 4) (7 8 9)) user> (split-at-subsequence [5 7] (range 10)) ((0 1 2 3 4 5 6 7 8 9)) user> (fi