Peter Naur's classic 1985 essay "Programming as Theory Building" argues that a program is not its source code. A program is a shared mental construct (he uses the word theory) that lives in the minds of the people who work on it. If you lose the people, you lose the program. The code is merely a written representation of the program, and it's lossy, so you can't reconstruct
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
from typing import Dict, List | |
def pagerank(G: Dict[int, List[int]], v: Dict[int, float] = None, | |
alpha: float = 0.85, max_iter: int = 1000, | |
tol: float = 10e-12) -> Dict[int, float]: | |
# Number of nodes in the graph | |
n = len(G) | |
# Removes self loops from the graph | |
for i in G: | |
if i in G[i]: |
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
import java.util.*; | |
@SuppressWarnings("ALL") | |
class Test { | |
static class ReifiedList<T> extends AbstractList<T> { | |
private final List<T> delegate = new ArrayList<>(); | |
private final Class<?> type; | |
@SafeVarargs | |
ReifiedList(@Deprecated T... unused) { |
An "invertible syntax description" is something that can be used to both parse and generate a syntax.
For example, instead of defining separate toJson
and fromJson
functions for a given data type, an invertible syntax description could be used to provide both.
There are many Haskell libraries that provide this functionality or something close to it.
As far as I can tell most of them are at least inspired by Invertible syntax descriptions by Tillmann Rendel and Klaus Ostermann.
Personally I am interested in using these for HTTP routing.
I frequently want to be able to define a route such as /episodes/:id.json
, dispatch based on that route, and generate links to that route.
Doing so manually is tedious and error prone.
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
/* | |
* https://overreacted.io/algebraic-effects-for-the-rest-of-us/ | |
*/ | |
private val effectsStack = ThreadLocal<EffectsFrame>() | |
fun <T> perform(effectKey: Any): T { | |
var frame = effectsStack.get() | |
while (frame != null) { | |
@Suppress("UNCHECKED_CAST") |
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 of backward-mode automatic differentiation. | |
*/ | |
/** | |
* Differentiable variable with value and derivative of differentiation ([grad]) result | |
* with respect to this variable. | |
*/ | |
data class D(var x: Double, var d: Double = 0.0) { | |
constructor(x: Int): this(x.toDouble()) |
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
import kotlin.coroutines.* | |
import kotlin.coroutines.intrinsics.* | |
/** | |
* Implementation for Delimited Continuations `shift`/`reset` primitives via Kotlin Coroutines. | |
* See [https://en.wikipedia.org/wiki/Delimited_continuation]. | |
* | |
* The following LISP code: | |
* | |
* ``` |
See the official Differentiable Programming Manifesto instead.
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
infix fun <T,O>List<T>.cartesianProduct(others:List<O>):List<Pair<T,O>> = | |
flatMap{ t:T-> | |
others.map { o-> Pair(t,o) } | |
} | |
inline fun <T, O, R> List<T>.mapCartesianProduct(others: List<O>, transform: (Pair<T, O>) -> R): List<R> = | |
flatMap { t: T -> | |
others.map { o -> transform(Pair(t, o)) } | |
} |
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
os.system("kotlinc src/main/kotlin/Launcher.kt -include-runtime -d launcher.jar") | |
process = Popen(['java', '-jar', 'launcher.jar', tmp_file_name], stdout=PIPE) | |
(stdout, stderr) = process.communicate() |