Re: How can I remove the nils in the result?

2014-12-24 Thread Adam Clements
Colin, minor code review point that makes no real difference for this
example but is a good habit to get into: try not to use last on vectors,
use peek instead, otherwise it calls seq and steps through the entire
sequence on each call. peek on the other hand does what you'd expect. For
long lists this saves you a fair bit of work!

On Wed, 24 Dec 2014 1:54 am Colin Jones  wrote:

> Thanks, I had fun with this!
>
> This isn't more concise, but I went in a little a different direction,
> trying to pull the various concerns apart. In particular I had fun
> separating:
> - the idea of intervals for which any predicate passed from the specific
> case of 0/1 equality comparison
> - the string representation of intervals from their computation
> - the idea of one-indexing from the rest of the problem
>
> https://gist.github.com/trptcolin/a573561ac9262092f254
>
> - Colin
>
> p.s. There was no need for me to use `juxt` in `format-interval` but
> honestly, if you're not going to use `juxt` at every conceivable
> opportunity, why bother?
>
>
> On Monday, December 22, 2014 10:09:06 PM UTC-6, Pauli wrote:
>>
>> Hi, all:
>>
>> I'm tring to solve such a problem: Given a string consisting of "1" and
>> "0", find all the locations of "1", and print them in the format of
>> intervals.
>>
>> For example: "00101110101110" => 3, 5-7, 9, 11-13
>>
>> Here is my solution:
>>
>> (defn bar [x]
>>   (letfn [(foo [mystr]
>> (->>
>>   (map-indexed vector mystr)
>>   (filter #(= (second %) \1))
>>   (map (comp inc first))
>>   (partition-all 2 1)
>>   (filter #(= 2 (count %)]
>> (let [y (map #(if (> (- (second %) (first %)) 1) (print (first %) ", " 
>> (second %) "-")) (foo x))]
>>   (print (ffirst y) "-" y (last (last (foo x)))
>>
>>
>> With the code above, I got many nils in the result:
>>
>> (bar "00101110101110") => 3 , 5 -nil - (nil nil 7 , 9 -nil 9 , 11 -nil
>> nil nil nil) 13
>>
>> How can I remove them?
>>
>> And, is there any way to make my code more concise?
>>
>>
>>
>>  --
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I remove the nils in the result?

2014-12-23 Thread Colin Jones
Thanks, I had fun with this!

This isn't more concise, but I went in a little a different direction, 
trying to pull the various concerns apart. In particular I had fun 
separating:
- the idea of intervals for which any predicate passed from the specific 
case of 0/1 equality comparison
- the string representation of intervals from their computation
- the idea of one-indexing from the rest of the problem

https://gist.github.com/trptcolin/a573561ac9262092f254

- Colin

p.s. There was no need for me to use `juxt` in `format-interval` but 
honestly, if you're not going to use `juxt` at every conceivable 
opportunity, why bother?


On Monday, December 22, 2014 10:09:06 PM UTC-6, Pauli wrote:
>
> Hi, all:
>
> I'm tring to solve such a problem: Given a string consisting of "1" and 
> "0", find all the locations of "1", and print them in the format of 
> intervals.
>
> For example: "00101110101110" => 3, 5-7, 9, 11-13
>
> Here is my solution:
>
> (defn bar [x]
>   (letfn [(foo [mystr]
> (->>
>   (map-indexed vector mystr)
>   (filter #(= (second %) \1))
>   (map (comp inc first))
>   (partition-all 2 1)
>   (filter #(= 2 (count %)]
> (let [y (map #(if (> (- (second %) (first %)) 1) (print (first %) ", " 
> (second %) "-")) (foo x))]
>   (print (ffirst y) "-" y (last (last (foo x)))
>
>
> With the code above, I got many nils in the result:
>
> (bar "00101110101110") => 3 , 5 -nil - (nil nil 7 , 9 -nil 9 , 11 -nil 
> nil nil nil) 13
>
> How can I remove them?
>
> And, is there any way to make my code more concise?
>
>
>
>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I remove the nils in the result?

2014-12-22 Thread Tim
whoops...

   (defn in-step [xs]
 (let [[x1 x2] xs]
   (= (- x2 x1) 1)))

On Monday, December 22, 2014 11:38:49 PM UTC-8, Tim wrote:
>
> Another option (though Bens does look nicer!) 
>
> (loop [xs (->> "00101110101110"
> (map vector (iterate inc 1))
> (filter #(= (last %) \1))
> (map first))
>   it nil]
>   (let [steps (partition 2 1 xs)
> [i o] (split-with in-step steps)]
> (cond (every? empty? steps)
> (str it (first xs))
>   (empty? i)
> (recur (rest xs)(str it (first xs) ","))
>:else (recur (last (split-at (count i) xs)) (str it (ffirst i) "-")
>
>
>>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I remove the nils in the result?

2014-12-22 Thread Tim
Another option (though Bens does look nicer!) 

(loop [xs (->> "00101110101110"
(map vector (iterate inc 1))
(filter #(= (last %) \1))
(map first))
  it nil]
  (let [steps (partition 2 1 xs)
[i o] (split-with in-step steps)]
(cond (every? empty? steps)
(str it (first xs))
  (empty? i)
(recur (rest xs)(str it (first xs) ","))
   :else (recur (last (split-at (count i) xs)) (str it (ffirst i) "-")


>

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I remove the nils in the result?

2014-12-22 Thread Ben Wolfson
(it's also zero-indexed, unlike yours, but that isn't a big change)

On Mon, Dec 22, 2014 at 9:17 PM, Ben Wolfson  wrote:

> This is a little more concise:
>
> (->> "00101110101110"
>(map-indexed vector)
>(partition-by second)
>(filter #(= (second (first %)) \1))
>(map (comp (fn [[f & tail]]
>   (if rest (str f "-" (last tail)) (str f)))
>   (partial map first
>
> On Mon, Dec 22, 2014 at 8:09 PM, Pauli  wrote:
>
>> Hi, all:
>>
>> I'm tring to solve such a problem: Given a string consisting of "1" and
>> "0", find all the locations of "1", and print them in the format of
>> intervals.
>>
>> For example: "00101110101110" => 3, 5-7, 9, 11-13
>>
>> Here is my solution:
>>
>> (defn bar [x]
>>   (letfn [(foo [mystr]
>> (->>
>>   (map-indexed vector mystr)
>>   (filter #(= (second %) \1))
>>   (map (comp inc first))
>>   (partition-all 2 1)
>>   (filter #(= 2 (count %)]
>> (let [y (map #(if (> (- (second %) (first %)) 1) (print (first %) ", " 
>> (second %) "-")) (foo x))]
>>   (print (ffirst y) "-" y (last (last (foo x)))
>>
>>
>> With the code above, I got many nils in the result:
>>
>> (bar "00101110101110") => 3 , 5 -nil - (nil nil 7 , 9 -nil 9 , 11 -nil
>> nil nil nil) 13
>>
>> How can I remove them?
>>
>> And, is there any way to make my code more concise?
>>
>>
>>
>>  --
>> 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 unsubscribe from this group and stop receiving emails from it, send an
>> email to clojure+unsubscr...@googlegroups.com.
>> For more options, visit https://groups.google.com/d/optout.
>>
>
>
>
> --
> Ben Wolfson
> "Human kind has used its intelligence to vary the flavour of drinks, which
> may be sweet, aromatic, fermented or spirit-based. ... Family and social
> life also offer numerous other occasions to consume drinks for pleasure."
> [Larousse, "Drink" entry]
>
>


-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


Re: How can I remove the nils in the result?

2014-12-22 Thread Ben Wolfson
This is a little more concise:

(->> "00101110101110"
   (map-indexed vector)
   (partition-by second)
   (filter #(= (second (first %)) \1))
   (map (comp (fn [[f & tail]]
  (if rest (str f "-" (last tail)) (str f)))
  (partial map first

On Mon, Dec 22, 2014 at 8:09 PM, Pauli  wrote:

> Hi, all:
>
> I'm tring to solve such a problem: Given a string consisting of "1" and
> "0", find all the locations of "1", and print them in the format of
> intervals.
>
> For example: "00101110101110" => 3, 5-7, 9, 11-13
>
> Here is my solution:
>
> (defn bar [x]
>   (letfn [(foo [mystr]
> (->>
>   (map-indexed vector mystr)
>   (filter #(= (second %) \1))
>   (map (comp inc first))
>   (partition-all 2 1)
>   (filter #(= 2 (count %)]
> (let [y (map #(if (> (- (second %) (first %)) 1) (print (first %) ", " 
> (second %) "-")) (foo x))]
>   (print (ffirst y) "-" y (last (last (foo x)))
>
>
> With the code above, I got many nils in the result:
>
> (bar "00101110101110") => 3 , 5 -nil - (nil nil 7 , 9 -nil 9 , 11 -nil
> nil nil nil) 13
>
> How can I remove them?
>
> And, is there any way to make my code more concise?
>
>
>
>  --
> 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 unsubscribe from this group and stop receiving emails from it, send an
> email to clojure+unsubscr...@googlegroups.com.
> For more options, visit https://groups.google.com/d/optout.
>



-- 
Ben Wolfson
"Human kind has used its intelligence to vary the flavour of drinks, which
may be sweet, aromatic, fermented or spirit-based. ... Family and social
life also offer numerous other occasions to consume drinks for pleasure."
[Larousse, "Drink" entry]

-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


How can I remove the nils in the result?

2014-12-22 Thread Pauli
Hi, all:

I'm tring to solve such a problem: Given a string consisting of "1" and 
"0", find all the locations of "1", and print them in the format of 
intervals.

For example: "00101110101110" => 3, 5-7, 9, 11-13

Here is my solution:

(defn bar [x]
  (letfn [(foo [mystr]
(->>
  (map-indexed vector mystr)
  (filter #(= (second %) \1))
  (map (comp inc first))
  (partition-all 2 1)
  (filter #(= 2 (count %)]
(let [y (map #(if (> (- (second %) (first %)) 1) (print (first %) ", " 
(second %) "-")) (foo x))]
  (print (ffirst y) "-" y (last (last (foo x)))


With the code above, I got many nils in the result:

(bar "00101110101110") => 3 , 5 -nil - (nil nil 7 , 9 -nil 9 , 11 -nil nil 
nil nil) 13

How can I remove them?

And, is there any way to make my code more concise?



-- 
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 unsubscribe from this group and stop receiving emails from it, send an email 
to clojure+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.