Skip to content

Instantly share code, notes, and snippets.

(ns barnsleys-fern)
(defn- affine [a b]
(fn [x] (map #(apply + (conj (map * %1 x) %2)) a b)))
(defn- move [x]
(let [afs (mapv (partial apply affine)
[[[[ 0.0 0.0 ] [ 0.0 0.16]] [0.0 0.0 ]]
[[[ 0.85 0.04] [-0.04 0.85]] [0.0 1.6 ]]
[[[ 0.2 -0.26] [ 0.23 0.22]] [0.0 1.6 ]]
@kohyama
kohyama / subseqseq.clj
Last active October 6, 2015 00:58
Divide a sequence into continuous subsequences
(def sss
(memoize
(fn [s]
(let [n (count s)]
(cond
(zero? n) '(())
(= n 1) `((~s))
:else (mapcat
(fn [c]
(let [[a b] (split-at c s)]
@kohyama
kohyama / factorize.clj
Last active May 18, 2022 02:20
Prime Factorization in Clojure
(defn factorize [n]
((fn f [n [h & r :as ps]]
(cond (< n 2) '()
(zero? (mod n h)) (cons h (lazy-seq (f (quot n h) ps)))
:else (recur n r)))
n prime-numbers)))
@kohyama
kohyama / primes.clj
Created September 28, 2015 05:55
Prime numbers in Clojure
(def prime-numbers
((fn f [x]
(cons x
(lazy-seq
(f (first
(drop-while
(fn [n]
(some #(zero? (mod n %))
(take-while #(<= (* % %) n) prime-numbers)))
(iterate inc (inc x))))))))
@kohyama
kohyama / project.clj
Last active September 18, 2015 09:28
I can't execute a standalone jar using lighttable.nrepl.handler/lighttable-ops.
(defproject nltry "0.1"
:dependencies [
[org.clojure/clojure "1.7.0"]
[org.clojure/tools.nrepl "0.2.11"]
[lein-light-nrepl "0.1.3"]
]
:aot :all
:main nltry.core
:repl-options {:nrepl-middleware [lighttable.nrepl.handler/lighttable-ops]}
)
@kohyama
kohyama / fibbuzz.clj
Last active August 29, 2015 13:58
FibBuzz in Clojure
(defn fibs [a b] (cons a (lazy-seq (fibs b (+' a b)))))
(defn fizzbuzz [x]
(condp #(zero? (mod %2 %1)) x
15 "FizzBuzz"
5 "Buzz"
3 "Fizz"
x))
(def fibbuzz-seq (map fizzbuzz (fibs 1 1)))
@kohyama
kohyama / sorts.clj
Last active August 29, 2015 13:58
Sort algorithms in Clojure
(defn insertion-sort [ks]
(reduce (fn [a x]
(let [[h t] (split-with #(< % x) a)]
(concat h [x] t)))
[] ks))
(defn merge-sort [ks]
(let [c (count ks)]
(if (= c 1) ks
((fn m [[[ah & ar :as a] [bh & br :as b]]]
@kohyama
kohyama / file1.txt
Created March 7, 2014 08:59
Ntmux を用いたマルチリンガル開発 ref: http://qiita.com/kohyama/items/d2399fd1d58cd8aec72a
% ntmux 5000 irb
irb(main):001:0>
@kohyama
kohyama / foo.clj
Last active December 24, 2015 11:09
How to generalize these three functions.
(ns foo
(:require [clojure.test :refer (with-test is are run-tests)]))
(with-test
(defn flatten-map [m kf vf]
(into {}
((fn g [kv n]
(if (map? n)
(apply concat
(keep (fn [[k v]] (g (conj kv k) v)) n))
@kohyama
kohyama / README.md
Last active December 22, 2015 10:19
An example client code to binary-communicate with a TCP server / TCP サーバとバイナリ通信するクライアントコード例

TCP-BIN

An example client code to binary-communicate with a TCP server.

TCP サーバとバイナリ通信するクライアントコードの例です.

Usage / 使い方

Give informations of a TCP server to the first argument as a map. :host is a string representing an IP address or a hostname which can be forward-looked up and :port is an integer represents a port.