(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 <wolf...@gmail.com> 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 <pauli.mat...@gmail.com> 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.

Reply via email to