Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / 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 / 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 */
@kohyama
kohyama / dce.clj
Created July 30, 2012 05:43
Add paths of sources and classes of your program, compile and execute it. Do them all at runtime.
(ns dce.core
(:import java.net.URL clojure.lang.DynamicClassLoader)
(:gen-class))
(defn -main [abs-path target-ns & args]
(let [ccl (.getContextClassLoader (Thread/currentThread))
dcl (if (instance? DynamicClassLoader ccl) ccl
(let [l (DynamicClassLoader. ccl)]
(.setContextClassLoader (Thread/currentThread) l)
l))]
@kohyama
kohyama / plex02.md
Created October 22, 2012 06:45
Exercise 02 Fibonacci Sequence - JavaScript
@kohyama
kohyama / plex03.clj
Last active October 12, 2015 01:38
Exercise 03 Sudoku Level 0
(require '[clojure.set :refer (difference)])
(defn sudoku-level0 [coll]
(let [ics (vec (map #(if % #{%} (set (range 1 10))) coll))
cnb (fn [cs i]
(set
(keep #(if (= 1 (count %)) (first %))
(concat
(map first (partition-all 1 9 (drop (mod i 9) cs)))
(take 9 (drop (* 9 (quot i 9)) cs))