Skip to content

Instantly share code, notes, and snippets.

@CodaFi
CodaFi / Continuations.swift
Created October 5, 2014 02:54
A Swift Continuation Monad
// Playground - noun: a place where people can play
public func id<A>(x : A) -> A {
return x
}
public func error<A>(x : String) -> A {
assert(false, x)
}
@CodaFi
CodaFi / Y.swift
Last active November 29, 2021 18:19
The Y Combinator in Swift
public func unsafeCoerce<A, B>(_ x : A) -> B {
return unsafeBitCast(x, to: B.self)
}
func Y<A>(_ f : @escaping (A) -> A) -> A {
return { (x : @escaping (A) -> A) in
return f((x(unsafeCoerce(x))))
}({ (x : A) -> A in
let fn : (A) -> A = unsafeCoerce(x)
return f(fn(x))
@CodaFi
CodaFi / turing-fake.swift
Last active November 25, 2021 00:50 — forked from garrigue/turing.ml
struct State0 {}; struct State1 {}; struct FinalState {} /*States*/
struct Symbol {}; struct Blank {} /*Symbols*/
struct Left {}; struct Right {} /*Directions*/
struct EndOfTape {} /*End of Tape*/
struct Transition<StartState, StartHead, EndState, EndHead, Direction> {
static func transitionOne() -> Transition<State0, Blank, State1, Symbol, Left> { fatalError() }
static func transitionTwo() -> Transition<State1, Blank, State0, Symbol, Right> { fatalError() }
static func transitionThree<State>() -> Transition<State, Symbol, FinalState, Symbol, Left> { fatalError() }
}
//
// Yoneda.swift
// Basis
//
// Created by Robert Widmann on 12/11/14.
// Copyright (c) 2014 Robert Widmann. All rights reserved.
//
import Basis
class AsyncOperation: Operation {
private(set) var taskHandle: Task.Handle<Void, Error>?
fileprivate enum State: Equatable {
case ready
case executing
case finished
}
fileprivate var state: State = .ready {
@CodaFi
CodaFi / Nat.swift
Last active September 24, 2021 19:46
// Playground - noun: a place where people can play
protocol ℕ {
static func construct() -> Self
static var value : UInt { get }
}
struct Zero : ℕ {
static func construct() -> Zero {
return Zero()
@CodaFi
CodaFi / gist:3880173
Created October 12, 2012 16:43
TUIScrollView+CVDisplayLink
@interface TUIScrollView : TUIView
{
CGPoint _unroundedContentOffset;
CGSize _contentSize;
CGSize resizeKnobSize;
TUIEdgeInsets _contentInset;
__unsafe_unretained id _delegate;
TUIScrollKnob * _verticalScrollKnob;
@CodaFi
CodaFi / NUIColor.h
Last active June 16, 2021 22:31
Redoing NSColor for no reason whatsoever.
//
// NUIColor.h
// NUIKit
//
// Created by Robert Widmann on 9/21/13.
// Copyright (c) 2013 CodaFi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <CoreGraphics/CoreGraphics.h>
# NOTE: You need sccache first - but if you already have it, this'll void your cache.
env SCCACHE_RECACHE=1 caffeinate ./swift/utils/build-script -R --cmake-cxx-launcher (which sccache) --cmake-c-launcher (which sccache) --skip-build-benchmark
@CodaFi
CodaFi / Algebra.pl
Last active January 2, 2021 11:14
Algebraic Laws in Prolog
% Introduce reflexive equality
refl(X, X).
% Introduce symmetrical equality
sym(X, Y) :-
refl(X, Y),
refl(Y, X).
% Identity of addition by zero
add_zero(N) :-