(import (rnrs))
(define (derive f dx)
(lambda (x)
(/ (- (f (+ x dx))
(f x))
dx)))
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns transducers | |
"Learn transducers by implementing them." | |
(:refer-clojure :exclude [map filter transduce sequence])) | |
(comment | |
"Let's forget, for the sake of this experiment, about laziness in Clojure. | |
We can implement eager versions of Clojure's `map` and `filter` functions as follows:") | |
(defn map | |
[f coll] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Var(object): | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return self.name | |
class Lam(object): | |
def __init__(self, bind, expr): | |
self.bind = bind |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns futamura | |
(:require [clojure.string :as str])) | |
;; ========= util ===================== | |
(defn get-source-code [var] | |
(let [{:keys [file line]} (meta var) | |
file (slurp file) | |
lines (->> file | |
str/split-lines | |
(drop (dec line))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; Implementation | |
;; ============== | |
(defn- return | |
[base stack] | |
(reduce (fn [acc f] | |
(f acc)) | |
base | |
stack)) | |
(defn run-fsm |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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)))))) |