Skip to content

Instantly share code, notes, and snippets.

public extension BinaryFloatingPoint {
public func scaled(from source: ReversibleRange<Self>, to destination: ReversibleRange<Self>) -> Self {
let destinationStart = destination.lowerBound
let destinationEnd = destination.upperBound
let selfMinusLower = self - source.lowerBound
let sourceUpperMinusLower = source.upperBound - source.lowerBound
let destinationUpperMinusLower = destinationEnd - destinationStart
extension CharacterSet {
func contains(_ character: Character) -> Bool {
guard let firstScalar = character.unicodeScalars.first else { return false }
return self.contains(firstScalar)
}
}
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
class PassThroughView: UIView {
override func hitTest(_ point: CGPoint, with event: UIEvent?) -> UIView? {
return nil
}
}
@khanlou
khanlou / Sequence+Stride.swift
Last active April 24, 2018 18:09
tweaks by @ericasadun
/// 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()
}
}
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 April 12, 2018 01:46
Swiftlint rules file with all built in rules disabled
disabled_rules:
- file_length
- line_length
- function_body_length
- redundant_discardable_let
- identifier_name
- void_return
- todo
- trailing_whitespace
- force_cast
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 {