Skip to content

Instantly share code, notes, and snippets.

Soroush Khanlou khanlou

Block or report user

Report or block khanlou

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View CharacterSet+Swift.swift
extension CharacterSet {
func contains(_ character: Character) -> Bool {
guard let firstScalar = character.unicodeScalars.first else { return false }
return self.contains(firstScalar)
}
}
View SafeDecodableArray.swift
import Foundation
/**
Decode an array of objects while simply omitting any nested objects that themselves fail to be decoded.
Inspired by https://stackoverflow.com/a/46369152/503916
*/
struct SafeDecodableArray<T: Decodable>: Decodable {
/*
An intermediate type that always succeeds at being decoded. Necessary because when iterating the
View PassThroughView.swift
class PassThroughView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return nil
}
}
@khanlou
khanlou / Sequence+Stride.swift
Last active Apr 24, 2018
tweaks by @ericasadun
View Sequence+Stride.swift
/// A strided non-contiguous sequence of elements that incorporates
/// every nth element of a base sequence.
public struct StridedSequence<BaseSequence: Sequence> : Sequence, IteratorProtocol {
public mutating func next() -> BaseSequence.Element? {
defer {
for _ in 0 ..< _strideLength - 1 {
let _ = _iterator.next()
}
}
View SortedArray.swift
struct SortedArray<Element, ComparableProperty: Comparable> {
let propertyAccessor: (Element) -> ComparableProperty
private var elements: [Element]
public init(propertyAccessor: @escaping (Element) -> ComparableProperty) {
self.elements = []
self.propertyAccessor = propertyAccessor
}
@khanlou
khanlou / .swiftlint.yml
Created Apr 12, 2018
Swiftlint rules file with all built in rules disabled
View .swiftlint.yml
disabled_rules:
- file_length
- line_length
- function_body_length
- redundant_discardable_let
- identifier_name
- void_return
- todo
- trailing_whitespace
- force_cast
View Configure.swift
import Foundation
import UIKit
public protocol Configure {}
extension Configure {
/// Makes it available to set properties with closures just after initializing.
///
/// let frame = UIView().configure {
View InfiniteCollection.swift
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
View AddQueryItemsBehavior.swift
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 May 8, 2018
Clean up all merged branches
View CleanUpGit.md

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://khanlou@github.com

You can’t perform that action at this time.