Skip to content

Instantly share code, notes, and snippets.

@kohyama
kohyama / tails.clj
Last active October 19, 2017 09:05
get a seq of tail seqs for a seq, not including '()
(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)))))
@kohyama
kohyama / map-between.clj
Last active October 19, 2017 09:05
apply a function to an element and the next element of it in a sequence
(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)
@kohyama
kohyama / bi-linear.clj
Created March 12, 2012 05:35
scaling 2D gray map by bi-linear interpolation
; 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 に拡大縮小し,
@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 / tcp-server.c
Created July 11, 2012 08:35
Minimum code for tcp server
#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 */