This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.test :refer (with-test is run-tests)]) | |
(with-test | |
(defn tails [coll] | |
(take-while seq (iterate rest coll))) | |
(is (= (tails '(:a :b :c)) | |
'((:a :b :c) (:b :c) (:c))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.test :refer (with-test are run-tests)]) | |
(with-test | |
(defn map-between [f s] | |
(map (fn [[a b]] (f a b)) (partition 2 1 s))) | |
(are [f s _ r] (= (map-between f s) r) | |
#(- %2 %1) '() -> '() | |
#(- %2 %1) '(0) -> '() | |
#(- %2 %1) '(0 1) -> '(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
; 2012.03.23 境界の処理がイマイチだったので全面書き換え | |
; clojure.math.numeric-tower (https://github.com/clojure/math.numeric-tower) | |
; が必要. | |
; clojure 1.3 向け | |
(use '[clojure.math.numeric-tower :only (floor)]) | |
; 長さ w のベクタの, 長さ h のベクタとして与えられる | |
; 解像度 w * h の値マップ src を双線形補完を用いて | |
; 解像度 W * H に拡大縮小し, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(require '[clojure.test :refer :all]) | |
;; fizz-buzz | |
;; 連続整数列をリストとして与えると各要素に対して | |
;; 3 の倍数は "fizz" を | |
;; 5 の倍数は "buzz" を | |
;; 3 と 5 の両方の倍数の場合は "fizzbuzz" を出力し, | |
;; それ以外は何も出力しない, という法則で出力される文字列の順序集合を | |
;; リストとして返す. | |
(defn fizz-buzz [coll] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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")) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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." |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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]] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sys/types.h> /* read */ | |
#include <sys/socket.h> /* accept, bind, getsockname, listen, socket */ | |
#include <sys/uio.h> /* read */ | |
#include <sys/wait.h> /* waitpid */ | |
#include <arpa/inet.h> /* htons */ | |
#include <errno.h> /* errno */ | |
#include <fcntl.h> /* open */ | |
#include <signal.h> /* kill, sigaction */ | |
#include <stdio.h> /* BUFSIZ */ | |
#include <stdlib.h> /* daemon, exit */ |
OlderNewer