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
public final class UnfairLockPointer { | |
deinit { | |
pointer.deinitialize(count: 1) | |
pointer.deallocate() | |
} | |
private let pointer: os_unfair_lock_t | |
public init() { |
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 True {} | |
struct IsBidirectional<C> {} | |
extension IsBidirectional: True where C : BidirectionalCollection { } | |
extension Collection { | |
var isBidirectional: Bool { | |
return IsBidirectional<Self>() is True | |
} | |
} | |
func testIsBidirectional() { |
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 | |
// All the complexity needed to avoid this code probably isn't worth it in the majority of cases. | |
// Particularly note that the data model doesn't 100% line up with the JSON (some keys have | |
// slightly different names for instance). A system flexible enough to handle that (say, something | |
// like Go's json.Marshal) would be nice, but this code, while slightly tedious, isn't really bad. | |
// Basically I'm saying that a richer JSON<->Swift system built into stdlib would be nice, but the | |
// bar is pretty high to go pulling in a helper library. | |
// | |
// That said, compare the Go code below, and I think it really is much simpler, and scales much better |
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 | |
/// NSURLSession synchronous behavior | |
/// Particularly for playground sessions that need to run sequentially | |
public extension NSURLSession { | |
/// Return data from synchronous URL request | |
public static func requestSynchronousData(request: NSURLRequest) -> NSData? { | |
var data: NSData? = nil | |
let semaphore: dispatch_semaphore_t = dispatch_semaphore_create(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
func modifiedCopy<Struct, Arg>(start: Struct, @noescape mutator: (inout Struct) -> Arg -> (), arg: Arg) -> Struct { | |
var new = start | |
mutator(&new)(arg) | |
return new | |
} | |
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
[ | |
{ | |
"code": "AF", | |
"name": "Afghanistan" | |
}, | |
{ | |
"code": "AX", | |
"name": "Aland Islands" | |
}, | |
{ |
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 unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? { | |
switch (optional1, optional2) { | |
case let (.Some(value1), .Some(value2)): | |
return (value1, value2) | |
default: | |
return nil | |
} | |
} | |
func unwrap<T1, T2, T3>(optional1: T1?, optional2: T2?, optional3: T3?) -> (T1, T2, T3)? { |
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
// uniq func for Swift 1.2 | |
func uniq<C: ExtensibleCollectionType where C.Generator.Element: Hashable>(collection: C) -> C { | |
var seen:Set<C.Generator.Element> = Set() | |
return reduce(collection, C()) { result, item in | |
if seen.contains(item) { | |
return result | |
} | |
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
# Tell system when Xcode utilities live: | |
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer | |
# Set "opendiff" as the default mergetool globally: | |
git config --global merge.tool opendiff |