Skip to content

Instantly share code, notes, and snippets.

View airspeedswift's full-sized avatar

Ben Cohen airspeedswift

View GitHub Profile
@airspeedswift
airspeedswift / OneSidedRanges.swift
Last active April 21, 2023 17:14
One-sided Range operators
postfix operator ..< { }
prefix operator ..< { }
struct RangeStart<I: ForwardIndexType> { let start: I }
struct RangeEnd<I: ForwardIndexType> { let end: I }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I>
{ return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I>
@airspeedswift
airspeedswift / rbt.swift
Last active August 29, 2015 14:23
Persistent Red/Black Tree in Swift using 2.0 Pattern Matching
final class Box<T> {
let unbox: T
init(_ x: T) { self.unbox = x }
}
enum Color { case R, B }
enum Tree { //<T: Comparable> { // something not working when I make this generic
case Empty
@airspeedswift
airspeedswift / subscript.swift
Created June 3, 2015 19:25
subscript mutation example
extension Int {
mutating func increment() {
self = self.successor()
}
}
struct S {
var x: Int = 0
subscript()->Int {
import Darwin
func mid<T: RandomAccessIndexType>(r: Range<T>)->T {
return r.startIndex.advancedBy(
r.startIndex.distanceTo(r.endIndex) / 2
)
}
func merge<T: Comparable>(inout source: [T], lhs: Range<Int>, rhs: Range<Int>) {
var tmp: [T] = []
@airspeedswift
airspeedswift / CSV.swift
Created May 11, 2015 16:19
CSV-parsing code in Swift
// after http://www.cocoawithlove.com/2009/11/writing-parser-using-nsscanner-csv.html
import Foundation
extension NSScanner {
func scanUpToCharactersFromSet(set: NSCharacterSet) -> String? {
var str: NSString?
return self.scanUpToCharactersFromSet(set, intoString: &str)
? str as? String
: nil
@airspeedswift
airspeedswift / OrderedSet.swift
Created May 11, 2015 15:50
Swift Ordered Set based on Red/Black tree
enum Color {
case Red, Black
}
class Tree<T: Comparable> {
let value: T
let left: Tree<T>?
let right: Tree<T>?
let color: Color
@airspeedswift
airspeedswift / spelling.swift
Last active July 17, 2019 07:22
Norvig Spellchecker in Swift
/// Translation of [Peter Norvig's spell checker](http://norvig.com/spell-correct.html) into Swift.
/// Sample input corpus [here](http://norvig.com/big.txt)
import Foundation.NSString // purely for IO, most things done with Swift.String
// pythony slicing
postfix operator ..< { }
prefix operator ..< { }
postfix func ..<<I: ForwardIndexType>(lhs: I) -> RangeStart<I> { return RangeStart(start: lhs) }
prefix func ..<<I: ForwardIndexType>(rhs: I) -> RangeEnd<I> { return RangeEnd(end: rhs) }
@airspeedswift
airspeedswift / mirror.swift
Last active October 31, 2015 18:53
Extracting properties using reflect()
struct Properties {
// someday soon these can be private
var _name: String, _nameModificationDate: Int
var _shape: String, _shapeModificationDate: Int
init(name: String, nameModificationDate: Int,
shape: String, shapeModificationDate: Int) {
self._name = name
self._nameModificationDate = nameModificationDate
self._shape = shape
@airspeedswift
airspeedswift / shuffle.swift
Created April 4, 2015 22:06
Fisher-Yates Shuffle in Swift 1.2
import Darwin
func shuffle<C: MutableCollectionType where C.Index.Distance == Int>(var list: C) -> C {
var n = count(list)
if n == 0 { return list }
let oneBeforeEnd = advance(list.startIndex, n.predecessor())
for i in list.startIndex..<oneBeforeEnd {
let ran = Int(arc4random_uniform(UInt32(n--)))
let j = advance(i,ran)
swap(&list[i], &list[j])
@airspeedswift
airspeedswift / prime.c
Last active May 9, 2016 15:40
Swift vs C Prime Number Benchmark
// code originally adapted from https://github.com/DuncanMC/SwiftPerformanceBenchmark
#include <stdlib.h>
#include <stdio.h>
#include <CoreFoundation/CFDate.h>
void updateTotal(int newTotal, CFAbsoluteTime startTime) {
CFAbsoluteTime now = CFAbsoluteTimeGetCurrent();
CFAbsoluteTime totalTime = now - startTime;