librelist archives

« back to archive

sequence of all the subsequences

sequence of all the subsequences

From:
i-blis
Date:
2013-05-28 @ 09:24
This is all awesome. I am really impressed by the group's warm reactivity 
and dedication to make learning Clojure a joint effort. A short, in 
passing question of mine on IRC fostered a series of takes on the problem.
Awesome. 

The problem was about building a sequence of all the subsequences of a 
given sequence: (:a :b :c) -> (:a (:a :b) (:a :b :c)). The function should
be able to handle infinite and finite sequences, which means lazily 
handling the input and avoiding cycling in case of a finite output.

My take on that fulfilled the requirements but I was worried about the 
fact that the solution used an explicit integer accumulator to follow 
through subsequence building with `take`.

Then came that amazing bunch of takes on the problem. I wrapped them up 
here with comments: https://www.refheap.com/15090.

What I was  looking for came unsurprisingly from Sean Corfield. It was 
clear that this should boil down to a `map` but I wasn't able to get to 
it. Interestingly, my attempts failed because of a faulty implementation 
of a custom `cycle` function, which I didn't know about. The lesson here 
is that the standard Clojure API is awesome. And the probably wrong 
community too.

Thanks again, i-blis.
 

Re: [foray] sequence of all the subsequences

From:
Sean Corfield
Date:
2013-05-28 @ 16:35
On May 28, 2013, at 2:24 AM, i-blis wrote:
> Then came that amazing bunch of takes on the problem. I wrapped them up 
here with comments: https://www.refheap.com/15090.

Cool to see them all side-by-side. Two minor points:

* There's no 'n' in seancorfield.
* Using (repeat c) would be better than (cycle (list c)) - as I realized later...

> The lesson here is that the standard Clojure API is awesome. And the 
probably wrong community too.

Indeed! Even after three years of using Clojure (two years in production),
I'm constantly discovering new and simpler ways of doing things because of
some wonderful new combination of built-in functions!

Sean A Corfield -- (904) 302-SEAN
An Architect's View -- http://corfield.org/

"Perfection is the enemy of the good."
-- Gustave Flaubert, French realist novelist (1821-1880)



Re: [foray] sequence of all the subsequences

From:
Eric Pritchett
Date:
2013-05-31 @ 19:53
I'm a bit late to this party but I thought I'd contribute an interesting
solution and get some feedback. The way I approach this problem doesn't
seem to be the way anyone else did and I'm not sure if mine handles
infinites.

(defn list-seq [coll]
  (->> coll
       (reductions #(conj %2 %1) [])
       (drop 1)
       ((juxt first rest))
       (apply concat)))


On Tue, May 28, 2013 at 11:35 AM, Sean Corfield <sean@corfield.org> wrote:

> On May 28, 2013, at 2:24 AM, i-blis wrote:
> > Then came that amazing bunch of takes on the problem. I wrapped them up
> here with comments: https://www.refheap.com/15090.
>
> Cool to see them all side-by-side. Two minor points:
>
> * There's no 'n' in seancorfield.
> * Using (repeat c) would be better than (cycle (list c)) - as I realized
> later...
>
> > The lesson here is that the standard Clojure API is awesome. And the
> probably wrong community too.
>
> Indeed! Even after three years of using Clojure (two years in production),
> I'm constantly discovering new and simpler ways of doing things because of
> some wonderful new combination of built-in functions!
>
> Sean A Corfield -- (904) 302-SEAN
> An Architect's View -- http://corfield.org/
>
> "Perfection is the enemy of the good."
> -- Gustave Flaubert, French realist novelist (1821-1880)
>
>
>
>
>

Re: [foray] sequence of all the subsequences

From:
Eric Pritchett
Date:
2013-05-31 @ 19:56
Whoops, that should be #(conj %1 %2). Forgot to change it back from when I
was testing cons.


On Fri, May 31, 2013 at 2:53 PM, Eric Pritchett <
eric.a.pritchett.che@gmail.com> wrote:

> I'm a bit late to this party but I thought I'd contribute an interesting
> solution and get some feedback. The way I approach this problem doesn't
> seem to be the way anyone else did and I'm not sure if mine handles
> infinites.
>
> (defn list-seq [coll]
>   (->> coll
>        (reductions #(conj %2 %1) [])
>        (drop 1)
>        ((juxt first rest))
>        (apply concat)))
>
>
> On Tue, May 28, 2013 at 11:35 AM, Sean Corfield <sean@corfield.org> wrote:
>
>> On May 28, 2013, at 2:24 AM, i-blis wrote:
>> > Then came that amazing bunch of takes on the problem. I wrapped them up
>> here with comments: https://www.refheap.com/15090.
>>
>> Cool to see them all side-by-side. Two minor points:
>>
>> * There's no 'n' in seancorfield.
>> * Using (repeat c) would be better than (cycle (list c)) - as I realized
>> later...
>>
>> > The lesson here is that the standard Clojure API is awesome. And the
>> probably wrong community too.
>>
>> Indeed! Even after three years of using Clojure (two years in
>> production), I'm constantly discovering new and simpler ways of doing
>> things because of some wonderful new combination of built-in functions!
>>
>> Sean A Corfield -- (904) 302-SEAN
>> An Architect's View -- http://corfield.org/
>>
>> "Perfection is the enemy of the good."
>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>
>>
>>
>>
>>
>

Re: [foray] sequence of all the subsequences

From:
Solomon White
Date:
2013-05-31 @ 20:10
Neat!  It appears to handle infinites:

```clojure
(take 100 (list-seq (range)))
# => ... big list ...
```

The only "issue" I see is that the first item is "bare", not a list:

```clojure
(take 3 (list-seq (range)))
# => (0 [0 1] [0 1 2])
```

Thanks!


On Fri, May 31, 2013 at 1:56 PM, Eric Pritchett <
eric.a.pritchett.che@gmail.com> wrote:

> Whoops, that should be #(conj %1 %2). Forgot to change it back from when I
> was testing cons.
>
>
> On Fri, May 31, 2013 at 2:53 PM, Eric Pritchett <
> eric.a.pritchett.che@gmail.com> wrote:
>
>> I'm a bit late to this party but I thought I'd contribute an interesting
>> solution and get some feedback. The way I approach this problem doesn't
>> seem to be the way anyone else did and I'm not sure if mine handles
>> infinites.
>>
>> (defn list-seq [coll]
>>   (->> coll
>>        (reductions #(conj %2 %1) [])
>>        (drop 1)
>>        ((juxt first rest))
>>        (apply concat)))
>>
>>
>> On Tue, May 28, 2013 at 11:35 AM, Sean Corfield <sean@corfield.org>wrote:
>>
>>> On May 28, 2013, at 2:24 AM, i-blis wrote:
>>> > Then came that amazing bunch of takes on the problem. I wrapped them
>>> up here with comments: https://www.refheap.com/15090.
>>>
>>> Cool to see them all side-by-side. Two minor points:
>>>
>>> * There's no 'n' in seancorfield.
>>> * Using (repeat c) would be better than (cycle (list c)) - as I realized
>>> later...
>>>
>>> > The lesson here is that the standard Clojure API is awesome. And the
>>> probably wrong community too.
>>>
>>> Indeed! Even after three years of using Clojure (two years in
>>> production), I'm constantly discovering new and simpler ways of doing
>>> things because of some wonderful new combination of built-in functions!
>>>
>>> Sean A Corfield -- (904) 302-SEAN
>>> An Architect's View -- http://corfield.org/
>>>
>>> "Perfection is the enemy of the good."
>>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>>
>>>
>>>
>>>
>>>
>>
>

Re: [foray] sequence of all the subsequences

From:
Eric Pritchett
Date:
2013-05-31 @ 20:24
The first email states the problem is (:a :b :c) -> (:a (:a :b) (:a :b
:c)), so that's what I was trying to match. If that isn't the case,  then
you would just drop the part:

((juxt first rest))
 (apply concat)

and it would work as you are expecting. Also, the part "#(conj %1 %2)" can
just be "conj". I realized that after posting.


On Fri, May 31, 2013 at 3:10 PM, Solomon White <rubysolo@gmail.com> wrote:

> Neat!  It appears to handle infinites:
>
> ```clojure
> (take 100 (list-seq (range)))
> # => ... big list ...
> ```
>
> The only "issue" I see is that the first item is "bare", not a list:
>
> ```clojure
> (take 3 (list-seq (range)))
> # => (0 [0 1] [0 1 2])
> ```
>
> Thanks!
>
>
> On Fri, May 31, 2013 at 1:56 PM, Eric Pritchett <
> eric.a.pritchett.che@gmail.com> wrote:
>
>> Whoops, that should be #(conj %1 %2). Forgot to change it back from when
>> I was testing cons.
>>
>>
>> On Fri, May 31, 2013 at 2:53 PM, Eric Pritchett <
>> eric.a.pritchett.che@gmail.com> wrote:
>>
>>> I'm a bit late to this party but I thought I'd contribute an interesting
>>> solution and get some feedback. The way I approach this problem doesn't
>>> seem to be the way anyone else did and I'm not sure if mine handles
>>> infinites.
>>>
>>> (defn list-seq [coll]
>>>   (->> coll
>>>        (reductions #(conj %2 %1) [])
>>>        (drop 1)
>>>        ((juxt first rest))
>>>        (apply concat)))
>>>
>>>
>>> On Tue, May 28, 2013 at 11:35 AM, Sean Corfield <sean@corfield.org>wrote:
>>>
>>>> On May 28, 2013, at 2:24 AM, i-blis wrote:
>>>> > Then came that amazing bunch of takes on the problem. I wrapped them
>>>> up here with comments: https://www.refheap.com/15090.
>>>>
>>>> Cool to see them all side-by-side. Two minor points:
>>>>
>>>> * There's no 'n' in seancorfield.
>>>> * Using (repeat c) would be better than (cycle (list c)) - as I
>>>> realized later...
>>>>
>>>> > The lesson here is that the standard Clojure API is awesome. And the
>>>> probably wrong community too.
>>>>
>>>> Indeed! Even after three years of using Clojure (two years in
>>>> production), I'm constantly discovering new and simpler ways of doing
>>>> things because of some wonderful new combination of built-in functions!
>>>>
>>>> Sean A Corfield -- (904) 302-SEAN
>>>> An Architect's View -- http://corfield.org/
>>>>
>>>> "Perfection is the enemy of the good."
>>>> -- Gustave Flaubert, French realist novelist (1821-1880)
>>>>
>>>>
>>>>
>>>>
>>>>
>>>
>>
>