Skip to content

Instantly share code, notes, and snippets.

View bkase's full-sized avatar

Brandon Kase bkase

View GitHub Profile
@bkase
bkase / reified-monoid.swift
Created January 5, 2018 06:07
Reified Monoids (ported from Swift Sandbox)
// Thanks, Chris Eidhof for the idea
struct Monoid<T> {
let empty: T
let append: (T, T) -> T
func fold(arr: Array<T>) -> T {
return arr.reduce(self.empty, self.append)
}
}
@bkase
bkase / callback-based-effects.swift
Created January 5, 2018 06:06
Callback-based Effects (ported from Swift Sandbox)
/*
* A callback-base implementation of effectful computation that defers interpretation
* until after computation is expressed (so you can inject different interpreters
* onto the same computation).
*
* The downside is the results aren't expressed as values. They are callback calls.
* You probably don't actually want to use this, but it seems interesting to think about.
*
* Inspired by "Programming with Algebraic Effects and Handlers" by
* Andrej Bauer and Matija Pretnar 2012
@bkase
bkase / hashconsing-sklansky.swift
Created January 5, 2018 06:02
Hashconsing and Sklansky (ported from Swift Sandbox)
// An exploration of http://okmij.org/ftp/tagless-final/sharing/sharing.pdf
// Specifically focusing on Hashconsing and Sklansky
typealias NodeId = Int
enum Node: Equatable, Hashable {
case nconst(Int)
case nvar(String)
case nadd(NodeId, NodeId)
var hashValue: Int {
@bkase
bkase / algorithm-w.swift
Created January 5, 2018 06:00
Type Inference Algorithm W (ported from Swift Sandbox)
// Algorithm W
// From Principal Types for functional programs
// http://web.cs.wpi.edu/~cs4536/c12/milner-damas_principal_types.pdf
// by bkase
// STD library
infix operator <>: AdditionPrecedence
extension Dictionary {
static func <>(lhs: Dictionary, rhs: Dictionary) -> Dictionary {
var d: [Key: Value] = [:]
@bkase
bkase / algorithm-m.swift
Created January 5, 2018 05:59
Type Inference Algorithm M (ported from Swift Sandbox)
// Write some awesome Swift code, or import libraries like "Foundation",
// "Dispatch", or "Glibc"
enum Result<E, T> {
case fail(E)
case success(T)
var succeeded: Bool {
switch self {
case .fail(_): return false
@bkase
bkase / algebraic-graphs.swift
Created January 5, 2018 05:58
Playing with Algebraic Graphs (port from Swift Sandbox)
// Paper located: https://github.com/snowleopard/alga-paper
infix operator <>: AdditionPrecedence
protocol Semigroup {
static func <>(lhs: Self, rhs: Self) -> Self
}
protocol Monoid: Semigroup {
static var empty: Self { get }
}
protocol CommutativeMonoid: Monoid { }
protocol BoundedSemilattice: CommutativeMonoid { }
@bkase
bkase / override-method-return.swift
Created January 5, 2018 05:56
Fun with extension-method-return-type overriding (port from Swift Sandbox)
protocol Base { }
extension Base {
var a: String? { return nil }
var b: Int? { return nil }
}
protocol A: Base {
var a: String { get }
}
@bkase
bkase / example.re
Last active December 1, 2017 23:04
Reason React Native: Workaround to get jsThis from Reason-React
module Example {
type jsProps = {. x: int, y: string };
type state = ReasonReact.stateless;
type props = ReasonReact.noRetainedProps;
type action = ReasonReact.actionless;
let component = ReasonReact.statelessComponent("Example");
let make = (jsThis: Withjs.thisType) => (~x, ~y) => (_children) => {
@bkase
bkase / Animation.swift
Created October 6, 2017 08:34
Rough Sketch Functional Animations
// SUPER ROUGH ideas about distributive property on Animations
// This is the "current relative time as input" rather than the "0 to 1 as input" (unit) version of the animation
// this has flaws, but simple to show (or not) the distributive property of sequence and parallel
infix operator <>: AdditionPrecedence
protocol Semigroup {
static func <>(lhs: Self, rhs: Self) -> Self
}
indirect enum FreeSemigroup<A>: Semigroup {
@bkase
bkase / OlegSharing.swift
Created June 14, 2017 04:38
An exploration of Sharing (Oleg) focusing on Hashconsing and Sklansky
// An exploration of http://okmij.org/ftp/tagless-final/sharing/sharing.pdf
// Specifically focusing on Hashconsing and Sklansky
// See http://swift.sandbox.bluemix.net/#/repl/5940bd55698d8d064dec2b22 for this snippet in a playground
typealias NodeId = Int
enum Node: Equatable, Hashable {
case nconst(Int)
case nvar(String)
case nadd(NodeId, NodeId)