Skip to content

Instantly share code, notes, and snippets.

@AquaGeek
AquaGeek / BinaryHeap.swift
Created April 6, 2021 18:53
Binary heap implementation in Swift
import Swift
public struct BinaryHeap<Element> {
public typealias Index = Int
var storage: [Element]
let comparator: (Element, Element) -> Bool
public var count: Int {
storage.count
@AquaGeek
AquaGeek / GitHubAPI.swift
Created August 19, 2020 21:19
Example of how to use SignalProducer to fetch multiple pages
private func fetchAllUsers() -> SignalProducer<[User], Error> {
let decoder = JSONDecoder()
decoder.keyDecodingStrategy = .convertFromSnakeCase
func fetchUsers(since lastSeenUserID: Int?) -> SignalProducer<[User], Error> {
var components = URLComponents(string: "https://api.github.com/users")!
if let userID = lastSeenUserID {
components.queryItems = [
URLQueryItem(name: "since", value: userID.description)
@AquaGeek
AquaGeek / XcodeKit.h
Created April 9, 2020 23:57
XcodeKit Headers
//
// Generated by class-dump 3.5 (64 bit).
//
// class-dump is Copyright (C) 1997-1998, 2000-2001, 2004-2013 by Steve Nygard.
//
#pragma mark Blocks
typedef void (^CDUnknownBlockType)(void); // return type and parameters are unknown
struct L10n {
struct Login {
static func welcomeMessage(placeholder: String) -> String {
return String.localizedStringWithFormat(
NSLocalizedString("login.welcome_message", "Greeting to show users upon successful login"),
placeholder
)
}
}
}
extension NSColor {
// Derived from logic in TextMate
func soften(color inColor: NSColor, factor: CGFloat) -> NSColor {
guard let color = inColor.usingColorSpace(NSColorSpace.sRGB) else {
return inColor
}
var red = color.redComponent
var green = color.greenComponent
var blue = color.blueComponent
@AquaGeek
AquaGeek / Tips.md
Created March 22, 2017 22:29
macOS/AppKit tips

Override stupid system default that disables state restoration.

UserDefaults.standard.set(true, forKey: "NSQuitAlwaysKeepsWindows")  // override stupid global preference, so our state restore works properly
@AquaGeek
AquaGeek / Manifesto.md
Created February 17, 2017 20:13
Swift Ownership Manifesto

Ownership

Introduction

Adding "ownership" to Swift is a major feature with many benefits for programmers. This document is both a "manifesto" and a "meta-proposal" for ownership: it lays out the basic goals of the work, describes a general approach for achieving those goals, and proposes a number of specific changes and features, each of which will need to be separately discussed in a smaller and more

@AquaGeek
AquaGeek / Rx.swift
Last active February 6, 2017 11:27
RxSwift
// Retry with backoff
// Adapted from http://blog.danlew.net/2016/01/25/rxjavas-repeatwhen-and-retrywhen-explained/
Observable
.range(start: 1, count: 10)
.doOn(onNext: { i in
if i == 5 {
throw NSError(domain: "com.example", code: 1, userInfo: ["any": "thing"])
}
})
.retryWhen { (attempts: Observable<ErrorType>) -> Observable<Int64> in
@AquaGeek
AquaGeek / Storyboards.swift
Created January 20, 2016 22:21
Swift Snippets
// FROM: http://ankit.im/swift/2016/01/18/first-pure-swift-ios-app-swiflytics/
protocol StoryboardInstantiable {
typealias ViewController
static var storyboardID: String { get }
static func instance(storyboard: UIStoryboard) -> ViewController?
}
extension StoryboardInstantiable {
static func instance(storyboard: UIStoryboard) -> ViewController? {
return storyboard.instantiateViewControllerWithIdentifier(Self.storyboardID) as? ViewController
@AquaGeek
AquaGeek / AdjacentList.m
Created September 11, 2015 22:42
Graphs
@interface TSGraph : NSObject
- (void)addEdgeFrom:(id)vertexA to:(id)vertexB;
@end
@implementation TSGraph
{
NSMutableDictionary *_vertices;
}