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 <trptco...@gmail.com> 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.

Reply via email to