Skip to content

Instantly share code, notes, and snippets.


Nate Cook natecook1000

View GitHub Profile
View UnfairLockPointer.swift
public final class UnfairLockPointer {
deinit {
pointer.deinitialize(count: 1)
private let pointer: os_unfair_lock_t
public init() {
dabrahams / IsNonExistentialTest.swift
Created April 23, 2020 04:59
Test for conformance to non-existential protocol
View IsNonExistentialTest.swift
protocol True {}
struct IsBidirectional<C> {}
extension IsBidirectional: True where C : BidirectionalCollection { }
extension Collection {
var isBidirectional: Bool {
return IsBidirectional<Self>() is True
func testIsBidirectional() {
rnapier / 1-simplejson.swift
Last active February 3, 2018 22:59
Yeah, @krzyzanowskim is probably right. What problem were we solving?
View 1-simplejson.swift
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
View NSURLSession+Synchronous.swift
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)
anonymous / modifiedCopy.swift
Created October 28, 2015 23:52
turn foo.xInPlace(arg) into a foo.x(arg)...
View modifiedCopy.swift
func modifiedCopy<Struct, Arg>(start: Struct, @noescape mutator: (inout Struct) -> Arg -> (), arg: Arg) -> Struct {
var new = start
return new
extension Array {
func arrayByAppending(e: Element) -> Array {
return modifiedCopy(self, mutator: Array.append, arg: e)
View countries.json
"code": "AF",
"name": "Afghanistan"
"code": "AX",
"name": "Aland Islands"
tomlokhorst / Optional+Unwrap.swift
Last active December 26, 2017 19:50
Unwrap multiple optionals in Swift 1.0
View Optional+Unwrap.swift
func unwrap<T1, T2>(optional1: T1?, optional2: T2?) -> (T1, T2)? {
switch (optional1, optional2) {
case let (.Some(value1), .Some(value2)):
return (value1, value2)
return nil
func unwrap<T1, T2, T3>(optional1: T1?, optional2: T2?, optional3: T3?) -> (T1, T2, T3)? {
alskipp / uniq.swift
Last active August 29, 2015 14:06
Generic uniq function in Swift
View uniq.swift
// 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
kylefox / gist:4512777
Created January 11, 2013 18:15
If you want to use Xcode's FileMerge as your git mergetool, this is how you set it up.
View gist:4512777
# Tell system when Xcode utilities live:
sudo xcode-select -switch /Applications/
# Set "opendiff" as the default mergetool globally:
git config --global merge.tool opendiff