Skip to content

Instantly share code, notes, and snippets.

@divs1210
divs1210 / trampolined-cps.clj
Created December 15, 2022 10:21
Clojure trampolined continuation passing style
(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]
@divs1210
divs1210 / eager-cat.clj
Last active December 15, 2022 09:39
Clojure eager concatenation
(defn eager-cat
"Eager concat.
Doesn't blow up the stack.
Returns a vector."
([]
[])
([xs]
(vec xs))
([xs ys]
(into (vec xs) ys))
@divs1210
divs1210 / bfs.clj
Last active November 2, 2022 08:59
Clojure BFS
(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
@divs1210
divs1210 / with-retry.clj
Last active October 17, 2022 16:05 — forked from aphyr/with-retry.clj
Recur from within catch block
(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))
@divs1210
divs1210 / tinyaprs.js
Created November 26, 2021 14:33
Tinyman APRs
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;
@divs1210
divs1210 / Mylang.md
Last active August 19, 2021 17:52
Hypothetical Programming Language of My Dreams

Mylang

Features

Functional

  • like Clojure / Elixir
  • not fanatic like Haskell

Algol-derived syntax

  • syntax like JS / Python / Ruby / Elixir
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<center>
<h3>Lisp.js</h3>
@divs1210
divs1210 / lisp.html
Last active August 12, 2021 21:55
Simple lisp interpreter in browser
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
</head>
<body>
<div>
<center>
<h3>Lisp.js</h3>
@divs1210
divs1210 / Solver.md
Last active July 16, 2021 20:30
Arithmetic solver

Code

(import (rnrs))

(define (derive f dx)
  (lambda (x)
    (/ (- (f (+ x dx))
          (f x))
       dx)))
@divs1210
divs1210 / transducers.clj
Last active June 27, 2021 14:05
Learn Clojure transducers by implementing them!
(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]