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
> defmodule M do
> def f do
> x = 0
> {fn -> x = 1 end, fn -> x end}
> end
> end
iex:4: warning: variable x is unused
{:module, M,
<<70, 79, 82, 49, 0, 0, 5, 24, 66, 69, 65, 77, 69, 120, 68, 99, 0, 0, 0, 124, 131, 104, 2, 100, 0, 14, 101, 108, 105, 120, 105, 114, 95, 100, 111, 99, 115, 95, 118, 49, 108, 0, 0, 0, 4, 104, 2, ...>>,
{:f, 0}}
@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)])
(require '[clojure.java.io :as io]
'[clojure.string :as s])
(defn bench [filename f]
(with-open [r (io/reader filename)]
(->> (line-seq r)
(map f)
(group-by first))))
(defn f1 [line]
(defn include? [[x & xs :as xxs] [y & ys :as yys]]
(or (empty? xxs)
(and (not (empty? yys))
(if (= x y)
(recur xs ys)
(recur xxs ys)))))