Skip to content

Instantly share code, notes, and snippets.

@fumokmm
Created May 9, 2012 22:16
Show Gist options
  • Select an option

  • Save fumokmm/2649322 to your computer and use it in GitHub Desktop.

Select an option

Save fumokmm/2649322 to your computer and use it in GitHub Desktop.
Buzzになる数字をn個挙げるをClojureで
;
; (元ネタ
; (Buzzになる数字をn個挙げる http://d.hatena.ne.jp/irof/20120509/p1)
; ("Buzzになる数字をn個挙げる"をGroovyで…やろうと思ったんだけど http://d.hatena.ne.jp/irof/20120509/p2)
; (“Buzzになる数字をn個挙げる”をScalaで http://d.hatena.ne.jp/backpaper0/20120509/1336577196))
;
(use '(clojure.contrib (str-utils :only (str-join))))
; num to fizz-buzz-string
(defn to-fizz-buzz-str [x]
[x, (cond
(= (mod x 15) 0) "FizzBuzz"
(= (mod x 3) 0) "Fizz"
(= (mod x 5) 0) "Buzz"
:else (str x)) ] )
; FizzBuzzの遅延リスト [x, "fizz|buzz|fizzbuzz|x"]
(def fizz-buzz-list
(map to-fizz-buzz-str (iterate inc 1)))
;絞り込み
(defn to-only-buzz-list [list]
(map first (filter #(= (last %) "Buzz") list)))
; 出力 (str-join版)
(println
(str-join ", " (take 10 (to-only-buzz-list fizz-buzz-list))))
;
; (元ネタ
; (Buzzになる数字をn個挙げる http://d.hatena.ne.jp/irof/20120509/p1)
; ("Buzzになる数字をn個挙げる"をGroovyで…やろうと思ったんだけど http://d.hatena.ne.jp/irof/20120509/p2)
; (“Buzzになる数字をn個挙げる”をScalaで http://d.hatena.ne.jp/backpaper0/20120509/1336577196))
;
; num to fizz-buzz-string
(defn to-fizz-buzz-str [x]
[x, (cond
(= (mod x 15) 0) "FizzBuzz"
(= (mod x 3) 0) "Fizz"
(= (mod x 5) 0) "Buzz"
:else (str x)) ] )
; FizzBuzzの遅延リスト [x, "fizz|buzz|fizzbuzz|x"]
(def fizz-buzz-list
(map to-fizz-buzz-str (iterate inc 1)))
;絞り込み
(defn to-only-buzz-list [list]
(map first (filter #(= (last %) "Buzz") list)))
; 出力
(doseq [x (take 10 (to-only-buzz-list fizz-buzz-list))]
(println x))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment