Instantly share code, notes, and snippets.

• Sort options
Last active Nov 28, 2017
Dependent sums subsume algebraic data types in presence of enums
View NatSum.idr
 %hide Nat %hide Nat.S %hide Nat.Z function : List Type -> Type -> Type function [] b = b function (a :: as) b = a -> function as b infixr 10 *->
Last active Nov 7, 2017
Optional Default Raw
View Default.swift
 extension Optional { subscript (default value: Wrapped) -> Wrapped { get { return self ?? value } set { self = newValue } } }
Created Oct 19, 2017
DynamicDict
View DynamicDict.py
 #!/usr/bin/python import string class DynamicDict: def __init__(self, function): self._function = function def __getitem__(self, key): return self._function(key)
Last active Oct 7, 2017
Idris Spiral
View Spiral.idr
 import Data.Vect Matrix : Nat -> Nat -> Type -> Type Matrix r c a = Vect r (Vect c a) unrotate : Matrix r c a -> Matrix c r a unrotate = reverse . transpose spiral : Matrix r c a -> Vect (r * c) a spiral {r=Z} [] = []
Last active Jul 18, 2017
Mean Monoid
View Mean.swift
 infix operator ☹: AdditionPrecedence struct Mean: ExpressibleByFloatLiteral, ExpressibleByIntegerLiteral { private let total: Float private let count: Int private init(total: Float, count: Int) { self.total = total self.count = count }
Last active May 7, 2017
Lambda Calculus w/o Parenthesis
View OpenClose.js
 //// Implementation OPEN = Symbol("open") CLOSE = Symbol("close") function run(program) { function applyUntilClose(continuation = x => x) { function iter(f) { if (f === OPEN) { return applyUntilClose(r => iter(r)) } else if (f == CLOSE) { throw new Error("Unexpected empty grouping")
Last active May 6, 2017
 function Shadow() { return new Proxy({}, { get: function (target, name) { if (name in target) { let stack = target[name]; return stack[stack.length - 1]; } else { return undefined; } },
Created May 6, 2017
Scope
View Scope.js
 function Scope(parent = {}) { return new Proxy({}, { get: function(target, name) { return name in target ? target[name] : parent[name]; }, has: function (target, name) { return name in target || name in parent; }
Last active Nov 5, 2017
Monadic Python (requires Python interpreter that supports deep-copying generators, like pypy)
View Change.py
 ############## This is deprecated. ### NOTICE ### Use the NEW VERSION on GitHub: ############## https://github.com/JadenGeller/Guac # Similiar to this Hasekell program: https://gist.github.com/JadenGeller/faef08a0278d41c0dcc682bca36a9ef8#file-solution-hs @monadic(ListMonad) def make_change(amount_still_owed, possible_coins): change = [] while amount_still_owed > 0 and possible_coins: give_min_coin = yield [True, False] # nondeterministic choice, aka try both
Created May 3, 2017
Using coroutines for throwing errors
View YieldThrows.py
 import math # use `yield` as equivalent to Swift's # - `try` keyword def sqrt(x): # throwing if x < 0: yield Exception() return math.sqrt(x)
You can’t perform that action at this time.