Skip to content

Instantly share code, notes, and snippets.

divs1210 / lazy.rb
Last active Nov 13, 2018
Lazy Seqs in Ruby
View lazy.rb
# Abstract Class
# ==============
class ISeq
def first
def rest
def [](idx)
divs1210 /
Last active Nov 3, 2018
Dead Simple Multiline Lambdas for Python
def do(*body):
return body[-1]
# Example (Python 2)
# =======
map(lambda x: do(
print('x =', x),
x * x),
divs1210 / calculus.clj
Last active Sep 20, 2019
Calculus in Clojure
View calculus.clj
(ns calculus)
(defonce ^:const dx 0.00001M)
(defn df
"Returns the derivative of f."
(fn [x]
(/ (- (f (+ x dx)) (f x))
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 / 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))]
divs1210 / ratios.clj
Last active Jul 29, 2018
Clojure ratios
View ratios.clj
;; clojure solution for
;; HN:
(defn rec [y z]
(- 108
(/ (- 815
(/ 1500 z))
(defn rec-list [N]
divs1210 / history.clj
Last active Feb 13, 2018
An Excercise in Time Travel
View history.clj
(ns history)
;; ===========================
(defn hatom
"Atom that remembers previous states."
(atom {:value x
:history ()}))
divs1210 /
Last active Dec 1, 2017
coroutines.js walkthrough


Channels & go

var c = chan();

go(function() {
  // async block
divs1210 / functional-core-async-bench.clj
Last active Nov 16, 2017
Benchmark for functional-core-async
View functional-core-async-bench.clj
(defn event [env type val]
(let [rc (chan)]
(>! (:queue @env)
{:type type :val val :rc rc :time (:now @env)})
(<! rc)))
(defn bench []
(let [n 100000
divs1210 /
Last active Jul 3, 2017
Dynamic Functional Control Flow Structures for Java
import java.util.*;
import java.util.concurrent.Callable;
import java.util.function.Function;
class SLIP {
* Keywords
public static final Object
_IF = new Object(),
You can’t perform that action at this time.