Skip to content

Instantly share code, notes, and snippets.

Divyansh Prakash divs1210

Block or report user

Report or block divs1210

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View pascal.clj
(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
View ordered_infix.rb
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 Dec 14, 2018
Lightweight logging of db state changes for reagent/re-frame
View watch_db.cljs
(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 Nov 24, 2018
sample edn data
View data.edn
{:result 1}
@divs1210
divs1210 / lazy.rb
Last active Nov 13, 2018
Lazy Seqs in Ruby
View lazy.rb
# Abstract Class
# ==============
class ISeq
def first
end
def rest
end
def [](idx)
@divs1210
divs1210 / multilambda.py
Last active Nov 3, 2018
Dead Simple Multiline Lambdas for Python
View multilambda.py
def do(*body):
return body[-1]
# Example (Python 2)
# =======
map(lambda x: do(
print('x =', x),
x * x),
range(5))
@divs1210
divs1210 / calculus.clj
Last active Oct 26, 2018
Calculus in Clojure
View calculus.clj
(ns calculus)
(defonce ^:const dx 0.00001M)
(defn df
"Returns the derivative of f."
[f]
(fn [x]
(/ (- (f (+ x dx)) (f x))
dx)))
@divs1210
divs1210 / fork-join-recur.clj
Created Oct 11, 2018
Clojure fork-join example
View fork-join-recur.clj
(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 Aug 3, 2018
extrapolate: like iterate, but different
View extrapolate.clj
(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))]
You can’t perform that action at this time.