Skip to content

Instantly share code, notes, and snippets.

@alskipp
alskipp / uniq.swift
Last active August 29, 2015 14:06
Generic uniq function in 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
}
[
{
"code": "AF",
"name": "Afghanistan"
},
{
"code": "AX",
"name": "Aland Islands"
},
{
anonymous
anonymous / modifiedCopy.swift
Created October 28, 2015 23:52
turn foo.xInPlace(arg) into a foo.x(arg)...
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)
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)
@tomlokhorst
tomlokhorst / Optional+Unwrap.swift
Last active December 26, 2017 19:50
Unwrap multiple optionals in Swift 1.0
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)? {
@rnapier
rnapier / 1-simplejson.swift
Last active February 3, 2018 22:59
Yeah, @krzyzanowskim is probably right. What problem were we solving?
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
public final class UnfairLockPointer {
deinit {
pointer.deinitialize(count: 1)
pointer.deallocate()
}
private let pointer: os_unfair_lock_t
public init() {
@kylefox
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.
# 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
@dabrahams
dabrahams / IsNonExistentialTest.swift
Created April 23, 2020 04:59
Test for conformance to non-existential protocol
protocol True {}
struct IsBidirectional<C> {}
extension IsBidirectional: True where C : BidirectionalCollection { }
extension Collection {
var isBidirectional: Bool {
return IsBidirectional<Self>() is True
}
}
func testIsBidirectional() {