Re: All subsets of a vector

2011-11-11 Thread neveu
On Nov 9, 2:47 pm, Shoeb Bhinderwala shoeb.bhinderw...@gmail.com
wrote:
 ([a] [a b] [a b c] [a b c d])

It should be pointed out that this result -- while it may be what you
actually want -- is not all subsets of [a b c d]. For that
you might want to use combinatorics/subsets:

user= (C/subsets [a b c d])
(() (a) (b) (c) (d) (a b) (a c) (a d) (b c)
(b d) (c d) (a b c) (a b d) (a c d) (b c
d) (a b c d))

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


All subsets of a vector

2011-11-09 Thread Shoeb Bhinderwala
Is there a more elegant/idomatic way to achieve the following result:

user= a1
[a b c d]

user= (map-indexed (fn [n x] (vec (take (inc n) x))) (take (count a1)
(repeat a1)))
([a] [a b] [a b c] [a b c d])

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en


Re: All subsets of a vector

2011-11-09 Thread Linus Ericsson
(map #(vec (take (inc %) a1)) (range (count a1)))

does it the lovely map.

/Linus

2011/11/9 Shoeb Bhinderwala shoeb.bhinderw...@gmail.com

 Is there a more elegant/idomatic way to achieve the following result:

 user= a1
 [a b c d]

 user= (map-indexed (fn [n x] (vec (take (inc n) x))) (take (count a1)
 (repeat a1)))
 ([a] [a b] [a b c] [a b c d])

 --
 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 unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: All subsets of a vector

2011-11-09 Thread Alex Baranosky
Does Clojure have the equivalent of Haskell's 'scan' function? (I am on my
phone...) Seems like a solution with that would be nice. (scan is like
reduce except it keeps all intermediate results)
On Nov 9, 2011 5:57 PM, Linus Ericsson oscarlinuserics...@gmail.com
wrote:

 (map #(vec (take (inc %) a1)) (range (count a1)))

 does it the lovely map.

 /Linus

 2011/11/9 Shoeb Bhinderwala shoeb.bhinderw...@gmail.com

 Is there a more elegant/idomatic way to achieve the following result:

 user= a1
 [a b c d]

 user= (map-indexed (fn [n x] (vec (take (inc n) x))) (take (count a1)
 (repeat a1)))
 ([a] [a b] [a b c] [a b c d])

 --
 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 unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


  --
 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 unsubscribe from this group, send email to
 clojure+unsubscr...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: All subsets of a vector

2011-11-09 Thread Benny Tsai
Another way to do it, using 'reductions':

(rest (reductions conj [] a1))

On Wednesday, November 9, 2011 5:47:08 PM UTC-5, Shoeb Bhinderwala wrote:

 Is there a more elegant/idomatic way to achieve the following result: 

 user= a1 
 [a b c d] 

 user= (map-indexed (fn [n x] (vec (take (inc n) x))) (take (count a1) 
 (repeat a1))) 
 ([a] [a b] [a b c] [a b c d]) 


-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: All subsets of a vector

2011-11-09 Thread Benny Tsai
That's exactly what 'reductions' does :)

On Wednesday, November 9, 2011 6:09:08 PM UTC-5, Alex Baranosky wrote:

 Does Clojure have the equivalent of Haskell's 'scan' function? (I am on my 
 phone...) Seems like a solution with that would be nice. (scan is like 
 reduce except it keeps all intermediate results)
 On Nov 9, 2011 5:57 PM, Linus Ericsson oscarlinu...@gmail.com wrote:

 (map #(vec (take (inc %) a1)) (range (count a1)))

 does it the lovely map.

 /Linus

 2011/11/9 Shoeb Bhinderwala shoeb.bh...@gmail.com

 Is there a more elegant/idomatic way to achieve the following result:

 user= a1
 [a b c d]

 user= (map-indexed (fn [n x] (vec (take (inc n) x))) (take (count a1)
 (repeat a1)))
 ([a] [a b] [a b c] [a b c d])

 --
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.com
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+u...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en


  -- 
 You received this message because you are subscribed to the Google
 Groups Clojure group.
 To post to this group, send email to clo...@googlegroups.com
 Note that posts from new members are moderated - please be patient with 
 your first post.
 To unsubscribe from this group, send email to
 clojure+u...@googlegroups.com
 For more options, visit this group at
 http://groups.google.com/group/clojure?hl=en



-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: All subsets of a vector

2011-11-09 Thread Alex Baranosky
Dang,
I just logged in in-flight just so I could post my solution with reductions
:)

(defn doit [coll]
  (rest (reductions conj [] coll)))

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Re: All subsets of a vector

2011-11-09 Thread Bob Shock
Or my current favorite take-while iterate combo:

C:\clojure-1.2.0java -jar clojure.jar
Clojure 1.2.0
user= (take-while seq (iterate rest [1 2 3 4]))
([1 2 3 4] (2 3 4) (3 4) (4))

user= (take-while seq (iterate butlast [1 2 3 4]))
([1 2 3 4] (1 2 3) (1 2) (1))
user=

On Nov 9, 4:52 pm, Alex Baranosky alexander.barano...@gmail.com
wrote:
 Dang,
 I just logged in in-flight just so I could post my solution with reductions
 :)

 (defn doit [coll]
   (rest (reductions conj [] coll)))

-- 
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 unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en