Skip to content

Instantly share code, notes, and snippets.

@divs1210
divs1210 / SLIP.java
Last active July 3, 2017 16:31
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(),
@divs1210
divs1210 / functional-core-async-bench.clj
Last active November 16, 2017 09:57
Benchmark for functional-core-async
(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 / coroutines-js-walkthrough.md
Last active December 1, 2017 09:08
coroutines.js walkthrough

Setup

Channels & go

var c = chan();

go(function() {
  // async block
@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 ()}))
@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 / 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 / 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 / calculus.clj
Last active August 1, 2020 11:01
Calculus in Clojure
(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 / multilambda.py
Last active September 14, 2022 18:14
Dead Simple Multiline Lambdas for Python
def do(*body):
return body[-1]
# Example (Python 2)
# =======
map(lambda x: do(
print('x =', x),
x * x),
range(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)