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
| // Swift Standard Librray - String | |
| // Keith Harrison http://useyourloaf.com | |
| // Import Foundation if you want to bridge to NSString | |
| import Foundation | |
| // ==== | |
| // Initializing a String | |
| // ==== |
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
| let values = [ "1", "foo", "3" ] | |
| // Swift 1.2 | |
| extension Array { | |
| func filterMap(@noescape transform: T -> U?) -> [U] { | |
| var results = [U]() | |
| for x in self { | |
| if let mapped = transform(x) { | |
| results.append(mapped) |
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
| // Similar to `enumerate` but provides the collection's index type | |
| // rather than an Int for the position | |
| public func iterate<C: CollectionType>(collection: C) -> SequenceOf<(C.Index, C.Generator.Element)> { | |
| var index = collection.startIndex | |
| // type-inference doesn't want to work without this | |
| return SequenceOf { _ -> GeneratorOf<(C.Index, C.Generator.Element)> in | |
| return GeneratorOf { | |
| if index == collection.endIndex { | |
| return 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
| /* | |
| Erica Sadun, http://ericasadun.com | |
| Basic Errors | |
| */ | |
| import Foundation | |
| /// A basic utility error type that stores the reason for |
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 Value { | |
| let num: Int | |
| init(_ n: Int) { num = n } | |
| } | |
| let a = [Value(3), Value(2), Value(1), Value(4), Value(5)] | |
| let min1: Value = a.reduce(Value(Int.max)) { | |
| ($0.num < $1.num) ? $0 : $1 |
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
| let userjs: NSData = "{\"name\": \"max\", \"age\": 10, \"tweets\": [\"hello\"], \"attrs\": {\"one\": \"1\", \"more\": {\"stuff\": \"again\"}}}" | |
| .dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false) | |
| let val = JSValue.decode(userjs) | |
| let traversal = JSValue.key("attrs") • JSValue.key("more") • JSValue.key("stuff") | |
| let getIt = traversal.get(val) | |
| let setIt = traversal.set(val, JSValue.JSString("bob")) |
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
| //bind | |
| operator infix >>= { | |
| associativity left | |
| } | |
| struct Writer<A> { | |
| let a:A | |
| let s:String[] | |
| init(_ a:A, _ s:String[]) { |
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
| //this is a playground | |
| struct Reader<R,A> { | |
| let f:R -> A | |
| init(_ fun:R -> A) { | |
| f = fun | |
| } | |
| static func wrap(val:A) -> Reader<R,A> { | |
| return Reader({_ in val}) |
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
| /// Generator for Fibonacci sequence | |
| func fibonacciGenerator() -> AnyGenerator<Int> { | |
| var a = -1 | |
| var b = 1 | |
| return anyGenerator { | |
| let next = a + b | |
| a = b | |
| b = next | |
| return next | |
| } |
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
| /// Given two sequences and a function, return a sequence of results of applying | |
| /// the function to sucessive elements of the sequences. | |
| /// | |
| /// Like Haskell's zipWith. | |
| func zipCombine<S1: SequenceType, S2: SequenceType, T>( | |
| sequence1: S1, | |
| _ sequence2: S2, | |
| combine: (S1.Generator.Element, S2.Generator.Element) -> T | |
| ) -> AnySequence<T> { | |
| return AnySequence { () -> AnyGenerator<T> in |