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
extension Range where T: RandomAccessIndexType { | |
var mid: T { | |
return startIndex.advancedBy( | |
startIndex.distanceTo(endIndex) / 2 | |
) | |
} | |
} |
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
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> |
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
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 |
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
extension Int { | |
mutating func increment() { | |
self = self.successor() | |
} | |
} | |
struct S { | |
var x: Int = 0 | |
subscript()->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
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] = [] |
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
// 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 |
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
enum Color { | |
case Red, Black | |
} | |
class Tree<T: Comparable> { | |
let value: T | |
let left: Tree<T>? | |
let right: Tree<T>? | |
let color: Color |
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
/// 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) } |
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
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 |
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 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]) |