This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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] = [:] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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) | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write some awesome Swift code, or import libraries like "Foundation", | |
// "Dispatch", or "Glibc" | |
struct Monoid<T> { | |
let empty: T | |
let append: (T, T) -> T | |
func fold(_ arr: [T]) -> T { | |
return arr.reduce(empty, append) | |
} | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Write some awesome Swift code, or import libraries like "Foundation", | |
// "Dispatch", or "Glibc" | |
import Foundation | |
struct SampleCollection: LazyCollectionProtocol, RandomAccessCollection { | |
typealias Indices = DefaultRandomAccessIndices<SampleCollection> | |
let xs = ["a", "b", "c"] | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
protocol Expr { | |
associatedtype A | |
func eval() -> A | |
} | |
struct Const<T>: Expr { | |
typealias A = T | |
let value: A | |
func eval() -> A { | |
return value |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Bow | |
// This version of Co seems to work even thoug it's a bit gross to deal with Any | |
// Think of this as an example of how to deal with rank2 type polymorphism -- you only | |
// deal with Any in the constructor, even `runCo` has a generic interface. | |
// newtype Co w a = Co (forall r. w (a -> r) -> r) | |
public final class ForCo {} | |
public final class CoPartial<W: Comonad>: Kind<ForCo, W> {} | |
public typealias CoOf<W: Comonad, A> = Kind<CoPartial<W>, A> |