Skip to content

Instantly share code, notes, and snippets.

@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 Sep 20, 2019
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))]
@divs1210
divs1210 / ratios.clj
Last active Jul 29, 2018
Clojure ratios
View ratios.clj
;; 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 Feb 13, 2018
An Excercise in Time Travel
View history.clj
(ns history)
;; AN EXCERCISE IN TIME TRAVEL
;; ===========================
(defn hatom
"Atom that remembers previous states."
[x]
(atom {:value x
:history ()}))
@divs1210
divs1210 / coroutines-js-walkthrough.md
Last active Dec 1, 2017
coroutines.js walkthrough
View coroutines-js-walkthrough.md

Setup

Channels & go

var c = chan();

go(function() {
  // async block
@divs1210
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 []
(time
(let [n 100000
@divs1210
divs1210 / SLIP.java
Last active Jul 3, 2017
Dynamic Functional Control Flow Structures for Java
View SLIP.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.