Re: looking for a simpler implementation of a function I'm using

2010-08-10 Thread ngocdaothanh
> What I'm looking for is a natural, conceptually clean approach. Erlang programmers do version 2 all the time and it can be call "Erlang-style". You should be confident with version 2. Version 2 is cleaner if you write the inner loop as a separate function. -- You received this message because

Re: looking for a simpler implementation of a function I'm using

2010-08-09 Thread Meikel Brandmeyer
Hi, On Aug 9, 8:06 am, gary ng wrote: > Assuming vector is implemented in some form of array(well it seems to > have the characteristic of an array where accessing any element would > be O(1)), appending one element can be O(1) if there are space > reserved for some extra items but if that actio

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread gary ng
On Sun, Aug 8, 2010 at 10:50 PM, Meikel Brandmeyer wrote: > Hi, > > On Aug 8, 6:19 pm, gary ng wrote: > >> I wrote a similar version in F# which does have the advantage of >> handling infinite input or a very long partition in the sense that I >> can still consume the first element immediately an

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread gary ng
On Sun, Aug 8, 2010 at 10:32 PM, Meikel Brandmeyer wrote: > I'm bit confused about what you mean here, but vector append (read: > conj) is O(1) (don't nail me down on whether it's amortised). There is > no array copying going on underneath. > Assuming vector is implemented in some form of array(we

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread Meikel Brandmeyer
Hi, On Aug 8, 6:19 pm, gary ng wrote: > I wrote a similar version in F# which does have the advantage of > handling infinite input or a very long partition in the sense that I > can still consume the first element immediately and can skip to the > second, third group ... In a sense, the result i

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread Meikel Brandmeyer
Hi, On Aug 8, 8:25 pm, gary ng wrote: > Interesting to know. Just curious which one is more efficient(not that > I question the vector append is slow) as it would still involve some > array copy and creation(though I assume it would be very efficient as > it is using the very low level i.e. sort

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread gary ng
On Sat, Aug 7, 2010 at 9:30 PM, David Cabana wrote: > conj is not the same as append; it will insert the new element in the > smart (most efficient) way. For instance: > user> (conj  '(1 2 3) 0) > (0 1 2 3) > user> (conj [1 2 3] 0) > [1 2 3 0] > > There is no performance hit from using conj to in

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread gary ng
On Sun, Aug 8, 2010 at 12:12 AM, Meikel Brandmeyer wrote: > Just for fun another low-level solution: > > (defn partition-when >  [pred coll] >  (let [step (fn [p s] >               (if s >                 (let [fst (first s)] >                   (if (pred fst) >                     [p s] >        

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread Daniel Janus
Here's my take using my clj-iter (http://github.com/nathell/clj-iter): (defn partition-when [pred coll] (iter (for x in coll) (for p = (when x (pred x))) (for y initially () then (if p (list x) (cons x y))) (collect (reverse y) if p) (finally-collect (reverse y)

Re: looking for a simpler implementation of a function I'm using

2010-08-08 Thread Meikel Brandmeyer
Hi, Am 08.08.2010 um 05:27 schrieb David Cabana: > Using a vector instead of a list as the accumulator makes it possible > to skip the mapping of reverse used in the earlier version of pw: > > (defn pw [f? x] > (let [phi (fn [a e] > (if (f? e) >(cons [e] a ) >

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread David Cabana
> conj sounds like 'append' to me which I have no idea about the > performance characteristics in clojure(it is a no-no in F#, Haskell ++ > is better but would grow the stack). conj is not the same as append; it will insert the new element in the smart (most efficient) way. For instance: user> (co

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 8:27 PM, David Cabana wrote: > Using a vector instead of a list as the accumulator makes it possible > to skip the mapping of reverse used in the earlier version of pw: > > (defn pw [f? x] >  (let [phi (fn [a e] >              (if (f? e) >                (cons [e] a ) >    

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread David Cabana
Using a vector instead of a list as the accumulator makes it possible to skip the mapping of reverse used in the earlier version of pw: (defn pw [f? x] (let [phi (fn [a e] (if (f? e) (cons [e] a ) (cons (conj (first a) e) (rest

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread David Cabana
On Sat, Aug 7, 2010 at 9:36 PM, gary ng wrote: > if you don't mind about performance, this seems to be natural to me > > user=> (reverse (map reverse (reduce (fn [a e] (if (even? e) (cons [e] a) > (cons > (cons e (first a)) (rest a (list) [1 2 3 7 5 4 1]))) > ((1) (2 3 7 5) (4 1)) I reworked

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 6:55 PM, Michael Gardner wrote: > On Aug 7, 2010, at 8:48 PM, Michael Gardner wrote: > >> On Aug 7, 2010, at 8:39 PM, gary ng wrote: >> >>> nice, why do I need the gensym ? >> >> The gensym was just a cheesy way of generating a unique value. > > To elaborate a bit more on my

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread Michael Gardner
On Aug 7, 2010, at 8:48 PM, Michael Gardner wrote: > On Aug 7, 2010, at 8:39 PM, gary ng wrote: > >> nice, why do I need the gensym ? > > The gensym was just a cheesy way of generating a unique value. To elaborate a bit more on my failure, I was reading the docs for partition-by thinking that

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread Michael Gardner
On Aug 7, 2010, at 8:35 PM, Michael Gardner wrote: > (partition-by #(when (even? %) (gensym)) Whoops, hit 'send' too soon. And it doesn't actually work, since it splits before and after the even values. On Aug 7, 2010, at 8:39 PM, gary ng wrote: > nice, why do I need the gensym ? The gensym w

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 6:39 PM, gary ng wrote: > On Sat, Aug 7, 2010 at 6:35 PM, Michael Gardner wrote: >> (partition-by #(when (even? %) (gensym)) > nice, why do I need the gensym ? ah, the gensym is for 'break'. but why does it behave like this user=> (partition-by #(when (even? %) (gensym))

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 6:35 PM, Michael Gardner wrote: > (partition-by #(when (even? %) (gensym)) nice, why do I need the gensym ? -- 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

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread gary ng
On Sat, Aug 7, 2010 at 6:12 PM, David Cabana wrote: > Speed is no big deal for me; the sequences I'm handling are short, say > about length100. BTW, one of these is considerably faster than the > other for longish sequences. Can you guess which? > if you don't mind about performance, this seems to

Re: looking for a simpler implementation of a function I'm using

2010-08-07 Thread Michael Gardner
(partition-by #(when (even? %) (gensym)) -- 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 unsubsc

looking for a simpler implementation of a function I'm using

2010-08-07 Thread David Cabana
Here are a couple of implementations of a function I'm calling 'partition-when'. I feel like there should be a simpler way than either of these. If you have one, I'd love to see it. (defn partition-when;;version 1 "Partition a sequence into subsequences; begin a new s