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 Foundation | |
/* | |
Toying with tools to help read binary formats. | |
I've seen lots of approaches in swift that create | |
an intermediate object per-read (usually another NSData) | |
but even if these are lightweight under the hood, | |
it seems like overkill. Plus this taught me about <()> | |
*/ |
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
// Original by Erica Sadun | |
// Source: http://ericasadun.com/2014/06/24/swift-reflection-dump/ | |
import UIKit | |
import Foundation | |
func typestring(x : Any) -> String | |
{ | |
if let obj = x as? NSObject { | |
return NSStringFromClass((x as NSObject).dynamicType) |
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
for (key, value) in components { | |
if (value === f) { | |
let component = components.removeValueForKey(key) | |
println(component) | |
break | |
} | |
} |
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
// see also https://gist.github.com/griotspeak/8bb4c46611fc90d3043b | |
func findFirst<S: SequenceType>(seq: S, predicate: S.Generator.Element -> Bool) -> S.Generator.Element? { | |
for x in seq { | |
if predicate(x) { return x } | |
} | |
return nil | |
} | |
func not<T>(predicate: T -> Bool) -> (T -> Bool) { |
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
protocol OptionalType { | |
typealias T | |
func flatMap<U>(@noescape f: (T) -> U?) -> U? | |
} | |
extension Optional : OptionalType { } | |
extension SequenceType where Generator.Element: OptionalType { | |
func flatten() -> [Generator.Element.T] { | |
return self.map { $0.flatMap { $0 } } |
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 string = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Morbi enim lacus, ullamcorper in gravida a, semper id dolor. Mauris quis metus id" | |
extension String { | |
func split(separator: Character, maxSplit: Int = .max, allowEmptySlices: Bool = false) -> [String] { | |
return characters.split(separator, maxSplit: maxSplit, allowEmptySlices: allowEmptySlices).map(String.init) | |
} | |
} | |
let words = try string.split(" ") | |
let counts = words.map { $0.characters.count } |
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
func modifiedCopy<Struct, Arg>(var start: Struct, @noescape mutator: (inout Struct) -> Arg -> (), arg: Arg) -> Struct { | |
mutator(&start)(arg) | |
return start | |
} | |
extension Array { | |
func arrayByAppending(e: Element) -> Array { | |
return modifiedCopy(self, mutator: Array.append, arg: e) | |
} | |
} |
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
protocol FactoryAType { | |
typealias Product | |
} | |
protocol FactoryBType { | |
typealias Product | |
} | |
struct CombinedFactory<T: FactoryAType, U: FactoryBType where T.Product == U.Product> { | |
let factoryA: 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
#!/bin/sh | |
set -e | |
if [ -z "${CONFIGURATION}" ]; then | |
CONFIGURATION=debug | |
fi | |
# Create the development toolchain. | |
rm -rf ~/public/swift-project/build/Ninja-ReleaseAssert/swift-dev.xctoolchain |
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 | |
import CoreFoundation | |
// A variable we can use with exit() to ensure that the optimizer | |
// doesn't remove code we want to time | |
var undead = 0 | |
// A filtered collection that can be more efficiently counted than the stock one. | |
struct LazyFilterBidirectionalCollection2<Base : BidirectionalCollection> : Collection { | |
typealias Impl = LazyFilterBidirectionalCollection<Base> |
OlderNewer