Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
2009/4/23 Christophe Grand : > > Laurent PETIT a écrit : >> 2009/4/23 Christophe Grand : >> >>> Laurent PETIT a écrit : >>> I guess you're right. But a little warning in the javadoc could help newcomers not shoot themselves in the foot. And the problem is, calling directly (second)

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Christophe Grand
Laurent PETIT a écrit : > 2009/4/23 Christophe Grand : > >> Laurent PETIT a écrit : >> >>> I guess you're right. But a little warning in the javadoc could help >>> newcomers not shoot themselves in the foot. >>> And the problem is, calling directly (second) without calling (first) >>> woul

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
2009/4/23 Christophe Grand : > > Laurent PETIT a écrit : >> I guess you're right. But a little warning in the javadoc could help >> newcomers not shoot themselves in the foot. >> And the problem is, calling directly (second) without calling (first) >> would work most of the time. I wanted to make

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Christophe Grand
Laurent PETIT a écrit : > I guess you're right. But a little warning in the javadoc could help > newcomers not shoot themselves in the foot. > And the problem is, calling directly (second) without calling (first) > would work most of the time. I wanted to make it fail every time => > same behaviou

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
2009/4/23 Christophe Grand : > > Laurent PETIT a écrit : >> 2009/4/23 Christophe Grand : >> >>> *Warning this message contains mutable state and may hurt functional >>> sensibilities.* >>> >>> Ugly hack: >>> >>> (defn my-split-with [pred coll] >>>  (let [s (atom coll) >>>        p #(when-let [r (p

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Christophe Grand
Laurent PETIT a écrit : > 2009/4/23 Christophe Grand : > >> *Warning this message contains mutable state and may hurt functional >> sensibilities.* >> >> Ugly hack: >> >> (defn my-split-with [pred coll] >> (let [s (atom coll) >>p #(when-let [r (pred %)] (swap! s rest) r)] >>[(take-

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
2009/4/23 Christophe Grand : > > *Warning this message contains mutable state and may hurt functional > sensibilities.* > > Ugly hack: > > (defn my-split-with [pred coll] >  (let [s (atom coll) >        p #(when-let [r (pred %)] (swap! s rest) r)] >    [(take-while p coll) (drop-while pred (lazy-s

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Michael Wood
On Thu, Apr 23, 2009 at 2:11 PM, Emeka wrote: > Laurent, > > Sampi question was; > > > Let's say I have a sequence of integers: > >  (def a (3 9 1 5 102 -322 ...)) > > > > Is there a function for inserting an object—let's say :foo—after > > elements that fulfill a certain predicate? > > Furthermo

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Emeka
Laurent, Sampi question was; Let's say I have a sequence of integers: (def a (3 9 1 5 102 -322 ...)) Is there a function for inserting an object—let's say :foo—after elements that fulfill a certain predicate? Furthermore, I mean inserting :foo after any block of elements that fulfill it: (mys

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
Ah, and also it's not lazy, e.g. : (take 10 (reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) [] (iterate inc 0 Since you invited comments, please note that the quick way of writing a literal list '(3 4 5 6 8) is not general, because it does not evaluate the elements of the list : Try (let

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
1:2 user=> (reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) [] '(3 4 5 8 4 2)) (3 4 5 :foo 8 4 2) Not good, the expected result would have been (3 4 5 :foo 8 4 2 :foo) Regards, -- Laurent 2009/4/23 Emeka : > > (reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) [] '(3 4 5 8 4 2)) > > Note th

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Emeka
(reduce #(concat %1 (if (> %2 6) [ :foo %2] [%2])) [] '(3 4 5 8 4 2)) Note that I am not a programmer and do not know much about writing code, however the above snippet in my view can achieve the result you desired. I invite comments on the above. Regards, Emeka On Thu, Apr 23, 2009 at 10:52 AM,

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Christophe Grand
*Warning this message contains mutable state and may hurt functional sensibilities.* Ugly hack: (defn my-split-with [pred coll] (let [s (atom coll) p #(when-let [r (pred %)] (swap! s rest) r)] [(take-while p coll) (drop-while pred (lazy-seq @s))])) Now it works ;-) Laurent PETIT

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
This is a general problem with function (split-with) (and derivatives such as partition-by ...), This should certainly deserve a mention in their respective docstrings, I think. Because the docstring speak about lazyness, but not the kind of lazyness that can avoid Out of Memory in corner cases.

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Christophe Grand
Laurent PETIT a écrit : > Hi Meikel, > > It seems to me that your version is the only safe one so far, that > would succesfully indefinitely return values with this test: > > (dorun (mystery-function true? :foo (repeat true))) > > Mine, a new version of mine I'll never bother to publish, and > Chr

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Michael Wood
On Thu, Apr 23, 2009 at 9:44 AM, Laurent PETIT wrote: > > Hi Meikel, > > It seems to me that your version is the only safe one so far, that > would succesfully indefinitely return values with this test: > > (dorun (mystery-function true? :foo (repeat true))) > > Mine, a new version of mine I'll n

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Michael Wood
On Thu, Apr 23, 2009 at 1:11 AM, Christophe Grand wrote: > > Hi all! > > (defn mystery-function [pred coll] >  (lazy-seq >    (when (seq coll) >      (let [[run etc] (split-with pred coll)] >        (if (seq run) >          (concat run (cons :foo (mystery-function pred etc))) >          (cons (fi

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
Hi, Here is my final word (I promise :-): (defn mystery-function [f s o] (let [s (seq s) preds (map f s)] (mapcat #(if (and %2 (not %3)) [%1 o] [%1]) s preds (concat (rest preds) [false] I has only one drawback AFAIK (and depending on your use case, it

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
You version does not work for certain kind of data, (as did mine), see my answer to Meikel for more detail, -- Laurent 2009/4/23 Christophe Grand : > > Hi all! > > (defn mystery-function [pred coll] >  (lazy-seq >    (when (seq coll) >      (let [[run etc] (split-with pred coll)] >        (if (

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-23 Thread Laurent PETIT
Hi Meikel, It seems to me that your version is the only safe one so far, that would succesfully indefinitely return values with this test: (dorun (mystery-function true? :foo (repeat true))) Mine, a new version of mine I'll never bother to publish, and Christophe's all retain head. To explain o

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Christophe Grand
Hi all! (defn mystery-function [pred coll] (lazy-seq (when (seq coll) (let [[run etc] (split-with pred coll)] (if (seq run) (concat run (cons :foo (mystery-function pred etc))) (cons (first coll) (mystery-function pred (rest coll Christophe samp

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Meikel Brandmeyer
Hi Micheal, Am 22.04.2009 um 21:18 schrieb Michael Wood: Your version gives the same answer as mine, but I believe what he wants is something that skips over all the elements that pass the test and only inserts one instance of o after them. That's why in his example there is not a :foo after 1

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Michael Wood
On Wed, Apr 22, 2009 at 8:03 PM, Meikel Brandmeyer wrote: > Hi, > > Am 22.04.2009 um 16:57 schrieb samppi: > >> Let's say I have a sequence of integers: >> (def a (3 9 1 5 102 -322 ...)) >> >> Is there a function for inserting an object—let's say :foo—after >> elements that fulfill a certain pred

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Meikel Brandmeyer
Hi, Am 22.04.2009 um 16:57 schrieb samppi: Let's say I have a sequence of integers: (def a (3 9 1 5 102 -322 ...)) Is there a function for inserting an object—let's say :foo—after elements that fulfill a certain predicate? Furthermore, I mean inserting :foo after any block of elements that ful

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Laurent PETIT
2009/4/22 Michael Wood : > > On Wed, Apr 22, 2009 at 4:57 PM, samppi wrote: >> >> Let's say I have a sequence of integers: >>  (def a (3 9 1 5 102 -322 ...)) >> >> Is there a function for inserting an object—let's say :foo—after >> elements that fulfill a certain predicate? >> Furthermore, I mean

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Adam Blinkinsop
On Wed, Apr 22, 2009 at 9:58 AM, Michael Wood wrote: > > On Wed, Apr 22, 2009 at 4:57 PM, samppi wrote: > > > > Let's say I have a sequence of integers: > > (def a (3 9 1 5 102 -322 ...)) > > > > Is there a function for inserting an object—let's say :foo—after > > elements that fulfill a certai

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Michael Wood
On Wed, Apr 22, 2009 at 4:57 PM, samppi wrote: > > Let's say I have a sequence of integers: >  (def a (3 9 1 5 102 -322 ...)) > > Is there a function for inserting an object—let's say :foo—after > elements that fulfill a certain predicate? > Furthermore, I mean inserting :foo after any block of e

Re: Simple sequence question: inserting something after blocks of fulfilling elements

2009-04-22 Thread Laurent PETIT
Hi, what about that : (require 'clojure.contrib.seq-utils) (defn mystery-function "f : predicate function ; s : sequence to work on ; o : object to insert after each group of items fullfilling predicate f" [f s o] (let [partitioned (clojure.contrib.seq-utils/partition-by f s)] (mapc