I really like the 'partition' technique. That said, as a non-expert, I find
the recursive approach marginally easier to read:

(defn has22 [coll]
  (when-let [s (seq coll)]
    (or (= 2 (first s) (second s)) (recur (rest s)))))

In my microbenchmarks, the above technique runs about 5-10x faster for all
sequences.  Close enough that I suspect it is largely a matter of personal
preference, but still slightly faster on my machine.

As a footnote, using destructuring instead ran only about half as fast as
the above code:

(defn has22-destructing [coll]
  (when-let [[x & xs] (seq coll)]
    (or (= 2 x (first xs)) (recur xs))))

Not entirely sure why that would be.

Best regards,

-DeWitt


On Sun, Jul 29, 2012 at 8:22 PM, Yoshinori Kohyama <yykohy...@gmail.com>wrote:

> Hi John,
>
> 'partition' will be useful for you, as Moritz pointed out.
>
> (partition 2 1 [1 2 3 4]) -> ((1 2) (2 3) (3 4))
> (partition 2 1 [1 2 2 4]) -> ((1 2) (2 2) (2 4))
> (partition 2 1 [1 2 2 2]) -> ((1 2) (2 2) (2 2))
>
> (some #(= % [2 2]) (partition 2 1 [1 2 3 4])) -> nil
> (some #(= % [2 2]) (partition 2 1 [1 2 2 4])) -> true
> (some #(= % [2 2]) (partition 2 1 [1 2 2 2])) -> true
>
> (filter #(= % [2 2]) (partition 2 1 [1 2 3 4])) -> ()
> (filter #(= % [2 2]) (partition 2 1 [1 2 2 4])) -> ((2 2))
> (filter #(= % [2 2]) (partition 2 1 [1 2 2 2])) -> ((2 2) (2 2))
>
> I'm sorry I can't recognize whether you need a pair of 2s or two pairs of
> 2s.
>
> If you need one or more pairs of 2s, do
> (defn has22 [coll] (boolean (some #(= % [2 2]) (partition 2 1 coll))))
> (has22 [1 2 3 4]) -> false
> (has22 [1 2 2 4]) -> true
> (has22 [1 2 2 2]) -> true
>
> If you need two or more pairs of 2s, do
> (defn has222 [coll] (< 1 (count (filter #(= % [2 2]) (partition 2 1
> coll)))))
> (has222 [1 2 3 4]) -> false
> (has222 [1 2 2 4]) -> false
> (has222 [1 2 2 2]) -> true
>
> Regards,
> Yoshinori Kohyama
>
> --
> 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
  • [no subject] John Holland
    • Re: Mark Rathwell
      • Re: Moritz Ulrich
    • Re: Yoshinori Kohyama
      • Re: DeWitt Clinton
        • Re: nicolas.o...@gmail.com
          • Re: Yoshinori Kohyama
            • Re: nicolas.o...@gmail.com
              • Re: Yoshinori Kohyama
    • Re: Ben Smith-Mannschott

Reply via email to