Skip to content

Instantly share code, notes, and snippets.

(ns pascal)
(defn step [row]
(let [parents (partition 2 1 row)
new-row-middle (map (fn [[x y]]
(+ x y))
parents)]
(concat [1] new-row-middle [1])))
(defonce pascals-triangle
def ordered_infix(*nums_and_ops)
initial_val = nums_and_ops[0]
op_num_pairs = nums_and_ops.drop(1).each_slice(2).to_a
op_num_pairs.reduce(initial_val) do |accumulator, op_num_pair|
op, num = op_num_pair
accumulator.send(op, num)
end
end
ordered_infix 10, '/', 2, '-', 1, '*', 2
@divs1210
divs1210 / watch_db.cljs
Created December 14, 2018 11:26
Lightweight logging of db state changes for reagent/re-frame
(defn watch-db []
(add-watch re-frame.db/app-db :watcher
(fn [_ _ old-state new-state]
(let [[removed added] (clojure.data/diff old-state new-state)]
(when (or removed added)
(println "=====\nRemoved:")
(cljs.pprint/pprint removed)
(println "Added:")
(cljs.pprint/pprint added))))))
@divs1210
divs1210 / data.edn
Created November 24, 2018 15:33
sample edn data
{:result 1}
@divs1210
divs1210 / go-for-js-geeks.json
Created November 22, 2018 16:45
Go for JS Geeks
{"result": 5}
@divs1210
divs1210 / lazy.rb
Last active November 13, 2018 11:04
Lazy Seqs in Ruby
# Abstract Class
# ==============
class ISeq
def first
end
def rest
end
def [](idx)
@divs1210
divs1210 / fork-join-recur.clj
Created October 11, 2018 16:50
Clojure fork-join example
(ns fork-join-recur
(:import [java.util.concurrent ForkJoinPool
ForkJoinWorkerThread ForkJoinTask RecursiveTask]))
;; API
;; ===
(def ^:dynamic ^ForkJoinPool *pool* (ForkJoinPool/commonPool))
(defmacro fork [& body]
`(let [^ForkJoinTask task# (proxy [RecursiveTask] []
@divs1210
divs1210 / extrapolate.clj
Last active August 3, 2018 06:04
extrapolate: like iterate, but different
(defn extrapolate
"Returns a lazy seq produced by extrapolating `coll` with given rules.
On each iteration,
`gen-next` is called with the entire seq generated till this iteration
`shrink` is called with the extrapolated seq (`identity` by default)"
([gen-next coll]
(extrapolate gen-next identity coll))
([gen-next shrink coll]
(concat coll
(let [curr (volatile! (seq coll))]
@divs1210
divs1210 / ratios.clj
Last active July 29, 2018 04:33
Clojure ratios
;; clojure solution for https://medium.com/@bellmar/is-cobol-holding-you-hostage-with-math-5498c0eb428b
;; HN: https://news.ycombinator.com/item?id=17636029
(defn rec [y z]
(- 108
(/ (- 815
(/ 1500 z))
y)))
(defn rec-list [N]
@divs1210
divs1210 / history.clj
Last active February 13, 2018 11:41
An Excercise in Time Travel
(ns history)
;; AN EXCERCISE IN TIME TRAVEL
;; ===========================
(defn hatom
"Atom that remembers previous states."
[x]
(atom {:value x
:history ()}))