Skip to content

Instantly share code, notes, and snippets.

# A lightweight approach to parsing text with combinators.
# put together in an afternoon for a project by acb, dedicated to the public domain
# Inspired by Haskell Parsec and the PointFree swift-parsing library
import re
# elementary parsing functions.
# These take an input string and return `(parsed_value, remainder)` if
# successful, or `None` if not.
# The functions below generate a parsing function when called.
@andrewcb
andrewcb / statemonad.swift
Created July 3, 2019 14:53
statemonad.swift
// a simple Swift translation of the Haskell State monad
// taken from http://brandon.si/code/the-state-monad-a-tutorial-for-the-confused/
struct State<S,A> {
let run: ((S)->(A,S))
}
let fromSToAAndS = { (v: Int) -> (String, Int) in
if v%5 == 0 { return ("foo", v+1) }
//
// AudioBufferExtensions.swift
// Plink
//
// Created by acb on 20/03/2019.
// Copyright © 2019 Kineticfactory. All rights reserved.
//
// Extensions for AudioBuffer and related classes
import CoreAudio
// Generating UIImages functionally in Swift
// By Andrew Bulhak http://dev.null.org/acb/ github:andrewcb
// Distributed under the Apache Licence
//
// For more information, see http://tech.null.org/item/201603210210_functionally_uiimage
/* --------------------------------------------------------------
A UIImage extension providing the means of functionally generating images; the general form is:
let myImage = UIImage (width: w, height: h) { (x, y) in return pixelValue }
@andrewcb
andrewcb / prefix-trie.swift
Created March 6, 2016 23:53
A prefix-matching trie in Swift
// prefix-trie.swift A generic prefix-matching trie in Swift
// By Andrew Bulhak http://dev.null.org/acb/
/** The prefix trie has two types: the type of input element, C, and the type of final result for a successful prefix match, T. */
enum Trie<C:Hashable,T> {
typealias IndexType = C
typealias ResultType = T
case Success(value:T)
case Node([C:Trie<C,T>])
@andrewcb
andrewcb / promises-v1.1.swift
Last active January 19, 2016 01:16
Futures/Promises proof of concept in Swift for Linux (first draft)
/* Basic (Scala-esque) Futures/Promises in Swift on Linux (first draft)
* Author: Andrew Bulhak (https://github.com/andrewcb/)
* Distributed under Creative Commons CC-BY-SA. Some rights reserved.
*
* Note: this code will not be performant until there's a proper
* libdispatch available.
*/
import Foundation
import NSLinux