Skip to content

Instantly share code, notes, and snippets.

@kohyama
kohyama / inverse-fizz-buzz.clj
Last active October 19, 2017 09:05
逆FizzBuzz問題 (Inverse FizzBuzz)
(require '[clojure.test :refer :all])
;; fizz-buzz
;; 連続整数列をリストとして与えると各要素に対して
;; 3 の倍数は "fizz" を
;; 5 の倍数は "buzz" を
;; 3 と 5 の両方の倍数の場合は "fizzbuzz" を出力し,
;; それ以外は何も出力しない, という法則で出力される文字列の順序集合を
;; リストとして返す.
(defn fizz-buzz [coll]
@kohyama
kohyama / hanoi-states.clj
Last active October 19, 2017 09:05
Hanoi States
(require '[clojure.test :refer :all])
(defn hanoi-states [n]
(letfn [
(hanoi [s t d n]
(if (zero? n) '()
(concat (hanoi s d t (dec n))
(list
(condp = [s d]
[0 2] (fn [[[h & a] b c]] [(if a a '()) b (cons h c)])
@kohyama
kohyama / fold-right.clj
Created June 8, 2012 06:41
fold-right and reduce-right in Clojure
(use 'clojure.test)
(defn fold-right [f z coll]
(loop [[c & cs] coll rvsd '()]
(if (nil? c)
(loop [acc z [r & rs] rvsd]
(if r (recur (f r acc) rs) acc))
(recur cs (cons c rvsd)))))
(defn reduce-right [f coll]
@kohyama
kohyama / example-to-use-r-reduce.clj
Created June 8, 2012 17:13
R-reduce is an abstraction of folding operations from right to left
(import 'java.util.Date java.text.SimpleDateFormat)
(defn r-reduce [f coll retn pred]
(loop [[c & cs :as curr] coll rvsd '()]
(if (or (nil? c) (pred curr))
(loop [acc (retn curr) [r & rs] rvsd]
(if r (recur (f r acc) rs) acc))
(recur cs (cons c rvsd)))))
(def sdf (SimpleDateFormat. "yyyy-MM-dd"))
@kohyama
kohyama / unfold.clj
Created June 9, 2012 15:40
unfold in Clojure
(use 'clojure.test)
(defn unfold
"A variant of 'iterate' which accepts a stopping condition,
having the same syntax as 'unfold' of scheme.
Supposed
(x1 x2 x3 ... xk-1 xk ... ) == (s (g s) (g (g s)) ... )
and '(p xi)' returns true first at i == k,
((f x1) (f x2) (f x3) ... (f xk-1))
is returned."
@kohyama
kohyama / 4clojure-s53.clj
Last active October 19, 2017 09:04
A solution for #53 of 4clojure
(require '[clojure.test :refer :all])
(def s53
; If you are in 4clojure paste from here
(fn [coll]
(->> coll
; 1st. Make tails of coll.
(#(take-while identity (iterate next %)))
; 2nd. Take only consecutive elements from the head for each list.
(map (fn [[c & cs]]
@kohyama
kohyama / plex01.clj
Created October 23, 2012 05:37
Exercise 01 FizzBuzz
(defn fizz-buzz [n]
(map #(cond (zero? (mod % 15)) "FizzBuzz"
(zero? (mod % 5)) "Buzz"
(zero? (mod % 3)) "Fizz"
:else %)
(range 1 (inc n))))
(fizz-buzz 100)
; -> (1 2 "Fizz" 4 "Buzz" "Fizz" 7 8 "Fizz" "Buzz" 11 "Fizz" 13 14 "FizzBuzz"
; 16 17 "Fizz" 19 "Buzz" "Fizz" 22 23 "Fizz" "Buzz" 26 "Fizz" 28 29 "FizzBuzz"
@kohyama
kohyama / str-drop-while-same.clj
Last active October 19, 2017 08:55
文字列を先頭から見て同じところまで除去 in Clojure
(require '[clojure.test :refer (with-test are run-tests)])
(with-test
(defn str-drop-while-same [& args]
(if (some nil? args) args
(->> (map #(concat % (repeat nil)) args)
(apply map vector)
(drop-while #(apply = %))
(take-while #(some identity %))
(apply map str))))
@kohyama
kohyama / awtp.clj
Last active October 19, 2017 08:55
お釣りの要らない全ての支払い方
(require '[clojure.test :refer (with-test are run-tests)])
(with-test
(defn awtp [cs a]
((fn f [[h & r] a]
(cond
(zero? a) #{{}}
(nil? h) #{}
:else (set
(mapcat (fn [c]
@kohyama
kohyama / clojure-exercises.md
Last active October 19, 2017 08:54
Clojure Exercises / Clojure 演習