Skip to content

Instantly share code, notes, and snippets.

@divs1210
divs1210 / diff_engine.py
Last active August 29, 2015 14:07
Babbage's Difference Engine in Python
def diff(seq):
""" [1,4,9] => [3,5] """
return map(lambda x,y: x-y, seq[1:], seq[:-1])
def triangle(seq):
""" [1,4,9] => [[1,4,9],[3,5],[2]] """
diffs=[seq]
for i in range(1, len(seq)):
diffs.append(diff(diffs[i-1]))
return diffs
@divs1210
divs1210 / diff_engine.clj
Last active August 29, 2015 14:07
Babbage's Difference Engine in Clojure
(defn diffs
"(diffs [1 4 9]) => (3 5)"
[alist]
(map - (rest alist) alist))
(defn infer
"(infer [1 4 9]) => (1 4 9 16 25 ...)"
[pattern]
(->> (take (count pattern) (iterate diffs pattern))
(map last) reverse
@divs1210
divs1210 / factorial.clj
Last active August 29, 2015 14:15
Simple made Easy- A Taste of Clojure for the Cjurious
(ns factorial)
;;----------------
;;A Simple Problem
;;----------------
;The factorial function can be written as:
; factorial(n) = 1 * 2 * 3 * ... * n
;
;Let's take the case of factorial(5):
; 1 * 2 * 3 * 4 * 5
;
(ns impala)
;; ===============
;; IMPALA
;; -subleq oisc-
;; ---------------
;; github/divs1210
;; ===============
;; Primitives
;; ==========
(ns aoc.day6
(:require [clojure.core.reducers :as r]
[clojure.set :as cset]
[clojure.string :as s]))
(defn part-1-input []
(.trim (slurp "resources/day6-part1-input")))
(defn str->point
"(str->point \"0,0\") => [0 0]"
@divs1210
divs1210 / protocols.clj
Last active April 6, 2016 10:51
This works fine for some time, but then I start getting an exception saying: "No implementation of method: :search of protocol: #'some.thing/ISearchEngine found for class: some.thing.SearchEngine"
(defprotocol ISearchEngine
"Search the web"
(search [engine term] "Search the web for a single (one-word) term."))
(defrecord SearchEngine [url]
ISearchEngine
(search [this term]
(-> (str (:url this) term)
http/get deref :body)))
@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 / hmap.clj
Last active February 5, 2018 08:56
Immutable hashmap implemented as a Clojure macro
(ns hmap)
(defmacro hmap [& kvs]
"Returns an immutable hashmap.
Keys must be compile-time constants."
(if (even? (count kvs))
(let [tups (partition 2 kvs)
keys (mapv first tups)
kvs+ (concat kvs [::keys keys])]
`(fn [k#]