This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array { | |
| func quickSorted(_ areInIncreasingOrder: (Element, Element) -> Bool) -> Array { | |
| guard count > 1, let lastElement = last else { return self } | |
| var (preceding, successing) = (Array(), Array()) | |
| dropLast().forEach { areInIncreasingOrder($0, lastElement) ? preceding.append($0) : successing.append($0) } | |
| return preceding.quickSorted(areInIncreasingOrder) + [lastElement] + successing.quickSorted(areInIncreasingOrder) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Differentiator | |
| import RxCocoa | |
| import RxDataSources | |
| import RxSwift | |
| import UIKit | |
| class RxTableViewSectionedUpdatingDataSource<S: AnimatableSectionModelType>: | |
| TableViewSectionedDataSource<S>, RxTableViewDataSourceType { | |
| typealias UpdateCell = (UITableViewCell, IndexPath, S.Item) -> Void | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Collection where Element: Comparable & Numeric { | |
| typealias Triplet = (Element, Element, Element) | |
| func threeSum() -> [Triplet] { | |
| var result = [Triplet]() | |
| let sortedSelf = sorted() | |
| for (offset, a) in sortedSelf.dropLast().enumerated() { | |
| var slice = sortedSelf.dropFirst(offset + 1) | |
| while slice.count > 1, let b = slice.first, let c = slice.last { | |
| if a + b + c == 0 { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| @interface Pair<FirstType, SecondType>: NSObject | |
| @property FirstType first; | |
| @property SecondType second; | |
| + (instancetype)pairWithFirst:(FirstType)first second:(SecondType)second; | |
| @end | |
| @implementation Pair |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <numeric> | |
| bool lift(std::vector<int>& vec, int limit) { | |
| if (vec.back() < limit) { | |
| vec.back() += 1; | |
| return true; | |
| } | |
| for (auto rit = std::next(vec.rbegin()); rit != vec.rend(); rit++) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <iostream> | |
| #include <vector> | |
| #include <algorithm> | |
| #include <map> | |
| std::vector<bool> sieve() { | |
| std::vector<bool> v(5001, true); | |
| v[0] = v[1] = false; | |
| for (int p = 2; p < 5001; p++) { | |
| if (v[p]) { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import Network | |
| extension NWPathMonitor: ReactiveCompatible {} | |
| extension Reactive where Base == NWPathMonitor { | |
| func pathUpdate(on queue: DispatchQueue = .global(qos: .background)) -> Observable<NWPath> { | |
| return Observable.create { [weak base] observer in | |
| base?.pathUpdateHandler = observer.onNext | |
| base?.start(queue: queue) | |
| return Disposables.create { base?.cancel() } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import RxSwift | |
| struct MaybeNever<S: AnyObject> { | |
| private weak var take: S? | |
| init(_ t: S) { | |
| take = t | |
| } | |
| func run<A, B, R>(_ f: @escaping (S, A, B) -> Observable<R>) -> (A, B) -> Observable<R> { | |
| return { a, b in | |
| if let t = self.take { |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| extension Array { | |
| /// Union with newElements, comparing by keyPath value and replacing old elements with new | |
| /// Expected complexity ~ 3*n | |
| func union<T: Hashable>(newElements: Array, byKeyPath keyPath: KeyPath<Element, T>) -> Array { | |
| var copy = self | |
| // Hash indices for faster replacement | |
| let sample = copy.enumerated().reduce(into: [T: Index]()) { result, entry in | |
| result[entry.element[keyPath: keyPath]] = entry.offset | |
| } | |
| copy.reserveCapacity(copy.count + newElements.count) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import UIKit | |
| extension Comparable { | |
| func clamping(_ range: ClosedRange<Self>) -> Self { | |
| return Swift.min(range.upperBound, max(range.lowerBound, self)) | |
| } | |
| } | |
| extension UIColor { | |
| func components() -> (red: CGFloat, green: CGFloat, blue: CGFloat, alpha: CGFloat) { |