Skip to content

Instantly share code, notes, and snippets.

Avatar

Dan Appel Danappelxx

View GitHub Profile
View Keybase.md

Keybase proof

I hereby claim:

  • I am danappelxx on github.
  • I am dannn (https://keybase.io/dannn) on keybase.
  • I have a public key ASCT9QNqsziUWVm9U_uFCxpgp69kMqdq6B9D2U0AGVm8vwo

To claim this, I am signing this object:

View ThreadedFileResponse.swift
public struct ThreadedFileResponder: Responder {
let path: String
let headers : Headers
public init(path: String, headers: Headers = [:]) {
self.path = path
self.headers = headers
}
public func respond(to request: Request) throws -> Response {
if request.method != .get {
View arrayperf-swift2.2.swift
// 2.2 (xcode 7.3), built with optimizations
final class ArrayPerfTests: XCTestCase {
let source = 0..<300_000
// otherwise, compiler would be able to optimize these loops by inlining.
var map: (Int) -> String = { i in "\(i)" }
// 118 ms (15% STDEV)
func testRepeatedValue() {
measureBlock {
var array = Array(count: self.source.count, repeatedValue: "")
@Danappelxx
Danappelxx / simple-sql.swift
Created Jul 2, 2016
An exercise to execute basic SQL queries (skipped parsing)
View simple-sql.swift
indirect enum Query {
case equal(field: String, value: String)
case notEqual(field: String, value: String)
case not(query: Query)
case or(first: Query, second: Query)
case and(first: Query, second: Query)
}
func &&(lhs: Query, rhs: Query) -> Query {
View .travis.yml
notifications:
slack: zewo:VjyVCCQvTOw9yrbzQysZezD1
os:
- linux
- osx
language: generic
sudo: required
dist: trusty
osx_image: xcode8
install:
View Extensible-Enums.md

Extensible Enums

Introduction

This proposal introduces a new keyword that can be applied to enums which allows new cases to be introduced in extensions.

View heap.swift
enum Heap<Key: Comparable> {
case empty(key: Key)
indirect case halfFull(key: Key, left: Heap<Key>)
indirect case full(key: Key, left: Heap<Key>, right: Heap<Key>)
}
extension Heap {
var isFull: Bool {
if case .full(key: _, left: _, right: _) = self {
return true
View TypeErasedEventDispatcher.swift
protocol Event {}
protocol ErasedListener {
func dispatchIfMatches(event: Event)
}
struct Listener<T: Event>: ErasedListener {
let dispatch: T -> Void
func dispatchIfMatches(event: Event) {
(event as? T).flatMap(dispatch)
View UnsafeMutableBufferPointer.swift
let length = 10
let ptr = UnsafeMutablePointer<UInt8>(allocatingCapacity: length)
defer { ptr.deallocateCapacity(length) }
for i in 0..<length {
ptr.advanced(by: i).pointee = UInt8(i)
}
let buffer = UnsafeMutableBufferPointer(start: ptr, count: length)
@Danappelxx
Danappelxx / hungarian.swift
Last active May 13, 2016
A simple expression interpreter which parses tokens, arranges them into Hungarian Notation, and finally evaluates them.
View hungarian.swift
// For evaluation purposes
indirect enum Token: CustomStringConvertible {
case Value(Int)
case Add(Token, Token)
case Subtract(Token, Token)
case Multiply(Token, Token)
case Divide(Token, Token)
var description: String {
switch self {