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 ReversibleRange.swift
public struct ReversibleRange<Bound>: Equatable where Bound: Comparable {
public let startingBound: Bound
public let endingBound: Bound
public init(startingBound: Bound, endingBound: Bound) {
self.startingBound = startingBound
self.endingBound = endingBound
}
public var isReversed: Bool {
View BinaryFloating+Scaling.swift
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
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
You can’t perform that action at this time.