Skip to content

Instantly share code, notes, and snippets.

// Example code, see below for run/yield implementation. Note how we
// can even do yields across the stack, so this implements something
// more like coroutines than generators.
function foo() {
var x = Yield();
return x + 1;
}
var process = Run(function() {
// Example code, see try/catch/throw implementation below
function bar(x) {
if(x < 0) {
Throw(new Error("error!"));
}
return x * 2;
}
function foo(x) {
@johnynek
johnynek / stref.scala
Last active November 10, 2015 19:47
mutable variables via a state thread in scala. This is just meant as an illustration of how ST in haskell works, and more generally, how you can implement mutation with immutable APIs.
object STRef {
/**
* Here is a container where we have a single "state thread" = ST.
* When we run this thread of type ST[T] we get a result of type T.
*/
sealed abstract class ST[+T] {
def map[R](fn: T => R): ST[R] = flatMap(t => Const(fn(t)))
def flatMap[R](fn: T => ST[R]): ST[R] = FlatMapped(this, fn)
}
@chambart
chambart / packed_fields_gadt.ml
Last active April 3, 2023 02:00
Packed fields in integers
type zero = unit
type 'a succ = unit -> 'a
type one = zero succ
type 'a plus_1 = 'a succ
type 'a plus_2 = 'a plus_1 plus_1
type 'a plus_4 = 'a plus_2 plus_2
type 'a plus_8 = 'a plus_4 plus_4
@TheSeamau5
TheSeamau5 / svgdragdrop.elm
Last active January 29, 2020 05:26
Drag and drop example with svg in Elm
import Svg (Svg, circle, svg, g, line, text)
import Svg.Attributes (cx, cy, r, fill, stroke, strokeWidth, x, y, x1, x2, y1, y2, fontSize, style)
import Html
import Html.Attributes as Html
import Signal (Signal, map, foldp)
import DragAndDrop (mouseEvents, MouseEvent(..))
import List
--------------
@rmoff
rmoff / foo.md
Last active January 5, 2024 17:17
cx_Oracle install on MacOS
  1. Download Instant Client:
  • instantclient-basic-macos.x64-11.2.0.4.0.zip
  • instantclient-sdk-macos.x64-11.2.0.4.0.zip
  • instantclient-sqlplus-macos.x64-11.2.0.4.0.zip
  1. Unzip and move to /opt

  2. Create symlink

@jozefg
jozefg / infer.sig
Last active February 9, 2022 11:59
A demonstration of type inference in SML
signature TYPEINFER =
sig
type tvar = int
datatype monotype = TBool
| TArr of monotype * monotype
| TVar of tvar
datatype polytype = PolyType of int list * monotype
datatype exp = True
| False
| Var of int
@jbgi
jbgi / Term.java
Last active September 25, 2023 00:45
Generalized Algebraic Data Types (GADT) in Java
import static java.lang.System.*;
import java.util.function.BiFunction;
import java.util.function.Function;
// Implementation of a pseudo-GADT in Java, translating the examples from
// http://www.cs.ox.ac.uk/ralf.hinze/publications/With.pdf
// The technique presented below is, in fact, just an encoding of a normal Algebraic Data Type
// using a variation of the visitor pattern + the application of the Yoneda lemma to make it
// isomorphic to the targeted 'GADT'.
@jozefg
jozefg / queue.idr
Last active August 29, 2015 14:14
Queue in Idris
module queue
import Data.Vect
-- Here's the port of the Liquid Haskell blog post on amortized
-- queues. The tricksy bit is that the "amortized" bit depends on
-- laziness. This means that while in the REPL (where Idris is lazy)
-- this is reasonably efficient. It compiles absolutely horribly
-- though because each time push or pop something we rotate the whole
-- damned thing.
@divarvel
divarvel / continuation.js
Last active May 11, 2018 08:57
Continuation monad in JS. just run $ node continuation.js
console.log("\033[39mRunning tests…");
function assertEquals(actual, expected, description) {
if(typeof(actual) === "undefined") {
console.error("\033[31m" + description + " not implemented\033[39m");
} else {
if(actual !== expected) {
console.error("\033[31m" + description + " failed, expected " + expected + ", got " + actual + "\033[39m");
} else {
console.log(description + " \033[32m ok\033[39m");
}