Skip to content

Instantly share code, notes, and snippets.

import Foundation
import UIKit
public protocol Configure {}
extension Configure {
/// Makes it available to set properties with closures just after initializing.
///
/// let frame = UIView().configure {
enum InfiniteCollectionIndex<WrappedIndex: Comparable> {
case reachable(WrappedIndex)
case unreachable
}
extension InfiniteCollectionIndex: Comparable {
static func == (lhs: InfiniteCollectionIndex, rhs: InfiniteCollectionIndex) -> Bool {
switch (lhs, rhs) {
case (.unreachable, .unreachable):
return true
enum SharedNetworkClient {
static let googleMaps: NetworkClient = {
let keyBehavior = AddQueryItemsBehavior(name: "key", value: "$YR_KEY")
let configuration = RequestConfiguration(baseURLString: "https://maps.googleapis.com/", defaultRequestBehavior: keyBehavior)
let client = NetworkClient(configuration: configuration)
return client
}()
}
struct AddQueryItemsBehavior: RequestBehavior {
@khanlou
khanlou / CleanUpGit.md
Last active July 7, 2021 08:08
Clean up all merged branches

To clean up git, run:

git remote prune origin; git branch --merged | grep -v '^* master$' | grep -v '^  master$' | xargs git branch -d

If it doesn't like that because it thinks the repo is not there, change the remote from http://github.com to http://[email protected]

struct CyclicSequence<Base: Sequence>: Sequence {
let base: Base
init(base: Base) {
self.base = base
}
func makeIterator() -> CyclicIterator<Base> {
return CyclicIterator(base: base)
let zipped = zip([1,2,3], default: 0, ["a"], default: "-")
print(Array(zipped))
let zippedSameType = zip([1,2,3], [1], default: 0)
print(Array(zippedSameType))
@khanlou
khanlou / Data+PushNotifications.swift
Created March 10, 2018 16:00
How to generate a hex string for push notifications
import Foundation
extension Data {
var hexString: String {
return self.map({ return String(format: "%02hhx", $0) }).joined()
}
}
extension Int {
static func random(upTo max: Int) -> Int {
return Int(arc4random_uniform(UInt32(max)))
}
}
extension String {
static func randomString(length: Int) -> String {
let letters = Array("ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789")
@khanlou
khanlou / LazyDropLast.swift
Last active February 17, 2018 00:31
LazyDropLast
extension Sequence {
func lazyDropLast(_ n: Int) -> AnySequence<Element> {
precondition(n >= 0, "Can't drop a negative number of elements from a sequence")
return AnySequence<Element>({ () -> AnyIterator<Element> in
var iterator = self.makeIterator()
if n == 0 { return AnyIterator(iterator) }
@khanlou
khanlou / Intersperse.swift
Last active February 16, 2018 23:51
This is a lazy intersperse function in Swift
enum IntersperseState {
case element, separator
mutating func flip() {
self = self == .element ? .separator : .element
}
}
extension Sequence {