(import (rnrs))
(define (derive f dx)
(lambda (x)
(/ (- (f (+ x dx))
(f x))
dx)))
This file contains 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 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<div> | |
<center> | |
<h3>Lisp.js</h3> |
This file contains 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
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
</head> | |
<body> | |
<div> | |
<center> | |
<h3>Lisp.js</h3> |
This file contains 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
javascript:(function(){ | |
function parseDollarVal(_txt) { | |
let txt = _txt.split("$")[1] || "0"; | |
let lastChar = txt[txt.length-1]; | |
let multiplier = | |
lastChar == 'M' ? 1000000 | |
: lastChar == 'K' ? 1000 | |
: 1; |
This file contains 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
(defrecord Retry [bindings]) | |
(defmacro with-retry | |
"It's really inconvenient not being able to recur from within (catch) | |
expressions. This macro wraps its body in a (loop [bindings] (try ...)). | |
Provides a (retry & new bindings) form which is usable within (catch) blocks: | |
when this form is returned by the body, the body will be retried with the new | |
bindings." | |
[initial-bindings & body] | |
(assert (vector? initial-bindings)) |
This file contains 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 BFS [tree] | |
(loop [roots [tree] | |
ret []] | |
(if (empty? roots) | |
ret | |
(let [children (->> roots | |
(mapcat :children) | |
(remove nil?)) | |
this-level (map :val roots)] | |
(recur children |
This file contains 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 eager-cat | |
"Eager concat. | |
Doesn't blow up the stack. | |
Returns a vector." | |
([] | |
[]) | |
([xs] | |
(vec xs)) | |
([xs ys] | |
(into (vec xs) ys)) |
This file contains 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
(defmacro trampolined | |
"Wraps recursive calls." | |
[& body] | |
`(fn [] ~@body)) | |
(defn trampolined-k-fn | |
"Takes a k-fn and returns a stackless fn. | |
k-fn should be a CPS function with k as the first arg. | |
In k-fn, all recursive calls should be wrapped using `trampolined`." | |
[k-fn] |