Skip to content

Instantly share code, notes, and snippets.

View Sophia-Gold's full-sized avatar

Sophia Gold Sophia-Gold

View GitHub Profile
(ns cljs-storybook.components
(:require
[om.next :as om :refer-macros [defui]]
[om.dom :as dom]
cljsjs.react))
(defn assign-ref [this ref]
(fn [el]
(aset this ref el)))
(name :foo/bar)
(namespace :foo/bar)
(namespace (keyword (name :foo/bar/baz)))
(keyword "foo" "bar/baz")
(keyword "foo" (str (keyword "bar" "baz")))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(use 'criterium.core)
@Sophia-Gold
Sophia-Gold / trapping-rain-water.clj
Last active August 14, 2017 03:51
from Guy Steele's talk, "Four Solutions to a Trivial Problem"
(defn histo-water [l]
(->> l
(map-indexed #(vector
%2
(reduce max 0 (subvec l 0 %1))
(reduce max 0 (subvec l %1))))
(map #(- (min (second %) (nth % 2)) (first %)))
(filter pos?)
(reduce +)))
(defn swap [v i1 i2]
(assoc v i2 (v i1) i1 (v i2)))
(defn max-index [v]
(->> v
(map-indexed vector)
(apply max-key second)
(first)))
(defn Y [f]
((fn [x] (x x))
(fn [x]
(f (fn [& args]
(apply (x x) args))))))
(def fac
(fn [f]
(fn [n]
(if (zero? n) 1 (* n (f (dec n)))))))
(defn halve [i]
(bit-shift-right i 1))
(defn double [i]
(bit-shift-left i 1))
(defn mul [x y]
(let [x (take-while #(not= % 0) (iterate halve x))]
(->> y
(iterate double)
@Sophia-Gold
Sophia-Gold / AdditionChains.clj
Created August 18, 2017 03:53
using binary method (not always shortest)
(defn double [i]
(bit-shift-left i 1))
(defn to-binary-seq [^long x]
(map #(- (int %) (int \0))
(Long/toBinaryString x)))
(defn addition-chain [x]
(reduce #(if (zero? %2)
(conj %1 (double (peek %1)))
(defn map-longest [f default & colls]
(lazy-seq
(when (some seq colls)
(cons
(apply f (map #(if (seq %) (first %) default) colls))
(apply map-longest f default (map rest colls))))))
(defn str-to-longs [^String s]
(map #(Character/codePointAt s (long %)) (range (count s))))