Skip to content

Instantly share code, notes, and snippets.

@onemouth
onemouth / gist:c22aa764a61709f641f1
Last active August 29, 2015 14:06
Exchange Money
(defn exchange [table coin]
(let [table-size (count table)]
(reduce #(assoc %1 %2 (+ (nth %1 %2) (nth %1 (- %2 coin))))
table
(range coin table-size))))
(defn total-exchange [money coins]
(let [table (->> (take money (repeat 0N))
(#(conj % 1N))
vec)]
(def atom?
(fn [a]
(not (seq? a))))
(def null?
(fn [a]
(or
(nil? a)
(= () a))))

Keybase proof

I hereby claim:

  • I am onemouth on github.
  • I am onemouth (https://keybase.io/onemouth) on keybase.
  • I have a public key whose fingerprint is 5D66 A6A0 FE35 6D9F 8B23 B353 FE3B 5F5A BD4B 4820

To claim this, I am signing this object:

@onemouth
onemouth / 1
Last active August 29, 2015 14:19
POH 5
(defn take-odd [input]
(apply str
(->> (partition 2 2 nil (apply list input))
(mapcat #(take 1 %)))))
(let [line (read-line)]
(println (take-odd line)))
; swap Caps Lock and Left Ctrl.
Capslock::Ctrl
LCtrl::Capslock
`::Esc
Esc::`

Latency numbers every programmer should know

L1 cache reference ......................... 0.5 ns
Branch mispredict ............................ 5 ns
L2 cache reference ........................... 7 ns
Mutex lock/unlock ........................... 25 ns
Main memory reference ...................... 100 ns             
Compress 1K bytes with Zippy ............. 3,000 ns  =   3 µs
Send 2K bytes over 1 Gbps network ....... 20,000 ns  =  20 µs
SSD random read ........................ 150,000 ns  = 150 µs

Read 1 MB sequentially from memory ..... 250,000 ns = 250 µs

from itertools import ifilter
from itertools import islice
def sieve_worker(numbers, table):
head = next(numbers)
if head not in table:
yield head
table[head+head] = [head]
else:
(require '[clojure.string :as str])
(defn parse-str-to-cells [str-cells]
(->> (str/split str-cells #"\s+")
(mapv #(Integer. %))))
(defn is-end-by-num-of-cell [n]
#(= (dec n) %))
(defn step [wheel cells is-end?]
(require '[clojure.string :as str])
(defrecord Coffee [water ground])
(defn return-monad-coffee [water ground]
(fn []
(Coffee. water ground)))
(defn bind-monad-coffee [m-coffee f]
(f (m-coffee)))
@onemouth
onemouth / gist:5593339
Created May 16, 2013 17:08
11 -> 10(-1)
trans2NegForm :: Integer -> [Int]
trans2NegForm x = loop x "init" []
where
trans 0 "init" = (0, "init")
trans 1 "init" = (-1, "trans")
trans 1 "trans" = (0, "trans")
trans 0 "trans" = (1, "init")
loop 0 "trans" ans = 1:ans
loop 0 "init" ans = ans
loop i status ans = loop (i `shiftR` 1) newStatus (newBit:ans)