Skip to content

Instantly share code, notes, and snippets.

View cbarrett's full-sized avatar
🏴
Revolting

Colin Barrett cbarrett

🏴
Revolting
View GitHub Profile
### Keybase proof
I hereby claim:
* I am cbarrett on github.
* I am chbarrett (https://keybase.io/chbarrett) on keybase.
* I have a public key ASDhSCmtfibfvZ0Lo54bUO6YcFc7iqAiFDj0Um0oFWhc9wo
To claim this, I am signing this object:
@cbarrett
cbarrett / Reducer.swift
Last active August 22, 2017 00:19
Some fun with Reducers
// type State = ...
// infix operator <>: AdditionPrecedence
// enum Either<A, B> { ...
struct Reducer<Action> {
let reduce: (Action, State) -> State
init(_ f: @escaping (Action, State) -> State) {
reduce = f
}
}
@cbarrett
cbarrett / AtomicPtr.swift
Last active March 9, 2017 04:13
Simple atomic pointer
// Copyright (c) 2017 Colin Barrett, all rights reserved.
// License freely granted under the terms of CC-BY-NC <https://creativecommons.org/licenses/by-nc/4.0/>.
import Darwin
public class AtomicPointer<Pointee>: Equatable {
fileprivate var ptr: UnsafeMutablePointer<Pointee?>
fileprivate var flag: atomic_flag = atomic_flag()
public init(_ pointee: inout Pointee?) {
@cbarrett
cbarrett / dhall.swift
Last active January 17, 2017 04:56
Notes from my stream on 2017/01/16
// Dhall, a terminating configuration language
// Syntax: https://github.com/Gabriel439/Haskell-Dhall-Library/blob/master/src/Dhall/Core.hs
// This gist's history:
// 1. Contents at stream end
// 2. Clean up free vs. bound variables
enum Expr {
case constant(Int)
case variable(Bound)
@cbarrett
cbarrett / Amb.hs
Last active August 29, 2015 13:58
SICP's amb is the list monad, by example
-- You can find the original in section 4.3.2 of SICP: http://mitpress.mit.edu/sicp/full-text/book/book-Z-H-28.html#%_sec_4.3.2
import Data.List
distinct :: Eq a => [a] -> Bool
distinct xs = length (nub xs) == length xs
require :: Bool -> [[a]] -> [[a]]
require b e = if b then e else return []
/* Let's say you want to list all of the options to your program as an enum */
enum Option {
OptionFoo,
OptionBar,
CountOfOption
}
/* You want to write a procedure to generate a help message for your program. Here's one way: */
addDependentTree :: FilePath -> Bool -> Q ()
addDependentTree p inclFile = do
pwd <- runIO getCurrentDirectory
let p' = if not inclFile then dropFileName p else p
let paths = scanl (</>) pwd (splitDirectories p')
mapM_ addDependentFile paths -- We add every subdirectory along `p` in the hopes of catching any new files added therein
@cbarrett
cbarrett / delim-cont-reactive.rkt
Last active December 12, 2015 05:28
Using delimited continuations to implement RAC's signal type
#lang scheme
(require racket/control)
(require racket/match)
(define (create-signal didSubscribe)
(lambda (next error complete)
(match (shift k (didSubscribe k))
[(list 'next v) (next v)]
[(list 'error e) (error e)]
[(list 'complete) (complete)])))
@cbarrett
cbarrett / Lich.hs
Created October 10, 2012 18:22 — forked from patrickt/Lich.hs
Simple Lich parsing and encoding in Haskell
import Control.Applicative
import Control.Monad
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import Data.Map (Map)
import qualified Data.Map as M
import Text.Parsec hiding ((<|>), many)
import Text.PrettyPrint.HughesPJ ((<>), brackets, text, braces, hsep)
import qualified Text.PrettyPrint.HughesPJ as P
@cbarrett
cbarrett / Lich.hs
Created October 10, 2012 16:59
Simple Lich parsing and encoding in Haskell
import Control.Applicative
import Control.Monad
import qualified Data.ByteString as B
import qualified Data.ByteString.Char8 as C
import qualified Data.Map as M
import Text.Parsec hiding ((<|>), many)
import qualified Text.PrettyPrint.HughesPJ as P
type Parser = Parsec B.ByteString ()