Skip to content

Instantly share code, notes, and snippets.

View athos's full-sized avatar
🤷‍♂️
I know the value and cost of nothing

Shogo Ohta athos

🤷‍♂️
I know the value and cost of nothing
View GitHub Profile
(defmacro hash-map-by-names [names]
(zipmap (map keyword names) names))
(def x 100)
(def y 200)
(let [y 2
z 3]
(hash-map-by-names [x y])) ; {:y 2, :x 100}
@athos
athos / gist:4224221
Created December 6, 2012 12:50
clojure.coreで定義されている変数・関数名の長さのリスト。名前の平均の長さは約8.3文字。
user=> (def freq (frequencies (map #(count (str %)) (keys (ns-publics 'clojure.core)))))
#'user/freq
user=> (doseq [[l n] freq] (printf "%2d %s\n" l (apply str (repeat n \*))))
1 *******
2 ****************
3 ****************************
4 **********************************************************************
5 *******************************************************
6 ********************************************************
7 ****************************************************************
@athos
athos / reader_test.clj
Created December 12, 2012 12:47
an example of *default-data-reader-fn*, a feature of Clojure 1.5
(ns reader-test)
(alter-var-root #'*default-data-reader-fn* (fn [_] cons))
#defn(fibs [n]
#letfn([#f([a b] #lazy-seq(#cons(a, #f(b, #+(a, b)))))]
#take(n, #f(0, 1))))
; #fibs(10) => (0 1 1 2 3 5 8 13 21 34)
@athos
athos / tramp.clj
Last active December 16, 2015 07:08
(declare odd?)
(defn even? [x]
(if (== 0 x)
true
#(odd? (- x 1))))
(defn odd? [x]
(if (== 1 x)
true
@athos
athos / send_more_money.clj
Created June 25, 2013 14:20
A solution in Clojure for the problem of SEND + MORE = MONEY
(ns send-more-money
(:require [clojure.math.combinatorics :refer [combinations permutations]]))
(defn solve [n cond]
(for [ds (combinations (range 10) n)
ds' (permutations ds)
:when (cond ds')]
ds'))
(defn ->n [ds]
@athos
athos / gist:5956979
Created July 9, 2013 12:29
^:dynamicなVarの参照にかかる時間
user=> (def ^:dynamic x 0)
#'user/x
user=> (dotimes [_ 5] (time (dotimes [_ 10000000] x)))
"Elapsed time: 665.329 msecs"
"Elapsed time: 755.922 msecs"
"Elapsed time: 735.566 msecs"
"Elapsed time: 780.893 msecs"
"Elapsed time: 766.333 msecs"
nil
user=> (binding [x 100] x) ; 一度値を動的に束縛すると、それ以降の参照は遅くなる
let fib n =
(let rec iter n a b =
(if ((=) n 0)
then a
else (iter ((-) n 1) b ((+) a b)))
in (iter n 0 1))
5 0 0 0
10 0 0 0
15 0 0 0
20 0 0 0
25 0 0 0
30 0 0 0
35 0 0 0
40 133 36 97
45 52 0 52
50 66 23 43
@athos
athos / gist:8081180
Last active January 1, 2016 02:49
「Clojureが遅いという話」http://mopemope.hatenablog.com/entry/2013/10/16/145955 についての(手元の環境での)検証結果。
nREPL server started on port 55753 on host 127.0.0.1
REPL-y 0.3.0
Clojure 1.5.1
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
(defn- compound-rule [r1 r2]
(fn [type]
(fn [part]
(fn [x v]
(((r2 type) part) x (((r1 type) part) x v))))))
(defn- apply-rule [r x]
(if-let [f (r (:type x))]
(assoc x :contents
(mapcat (fn [[p v]] [p (or ((f p) x v) v)])