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") |
View Shadow.js
| 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; | |
| } | |
| }, |
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; | |
| } |
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 |
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) | |
View AllEqual.hs
| import Prelude hiding ((==)) | |
| import qualified Prelude | |
| import Data.List | |
| import Control.Monad.State | |
| data UnificationState a = UState [[a]] | |
| deriving Show | |
| unify :: Eq a => [a] -> State (UnificationState a) () | |
| unify terms = modify $ \(UState classes) -> |
View NotCallCC.swift
| // https://en.wikipedia.org/wiki/Call-with-current-continuation#Examples | |
| enum Exception<T>: Error { | |
| case raise(T) | |
| } | |
| func catching<T>(escapable block: (((T) throws -> Never) throws -> T)) -> T { | |
| do { | |
| return try block { escaped in | |
| // implementing call/cc signature with exceptions |
View types.pl
| % Note: There are some problems with this type system... :P | |
| :- discontiguous(type/2). | |
| :- discontiguous(convert/2). | |
| :- style_check(-singleton). | |
| /* implict conversions */ | |
| subtype(X, T2) :- type(X, T1), distinct(convert(T1, T2)). | |
| convert(T, T). |
View Optional.py
| class Optional: # Abstract class | |
| pass | |
| class Some(Optional): | |
| def __init__(self, value): | |
| self.wrapped = value | |
| class Nil(Optional): | |
| def __init__(self): | |
| pass |
View Fallible.swift
| import Darwin | |
| struct Failure: Error { } | |
| func fallible<T>(_ block: ((T?) throws -> T) throws -> T) -> T? { | |
| do { | |
| return try block { x in | |
| if let x = x { | |
| return x | |
| } else { |
NewerOlder