Skip to content

Instantly share code, notes, and snippets.

View hermanbanken's full-sized avatar
🇳🇱

Herman hermanbanken

🇳🇱
View GitHub Profile
extension Observable : Publisher {
@available(iOS 13.0, *)
public func receive<S>(subscriber: S) where S : Subscriber, Observable.Failure == S.Failure, Observable.Output == S.Input {
_ = self.subscribe(onNext: { (next) in
let demand = subscriber.receive(next)
}, onError: { (err) in
subscriber.receive(completion: .failure(err))
}, onCompleted: {
subscriber.receive(completion: .finished)
})
let p3 = Publishers.Just(42)
p3.sink { (value) in
debugPrint(value)
}
func run () {
if #available(iOS 13.0, *) {
// Create
let p1 = Publishers.Empty<Int, Error>()
let p2 = Publishers.Just(42)
let p3 = Publishers.Sequence<[Int], Error>(sequence: [1,2,3,4])
// Operators
let p4 = p1
@available(iOS 13.0, *)
let p1 = Publishers.Empty<Any, Error>()
@available(iOS 13.0, *)
let p2 = Publishers.Just(42)
@available(iOS 13.0, *)
let p3 = Publishers.Sequence<[Int], Error>(sequence: [1,2,3,4])
@available(iOS 13.0, *)
let p = Publishers.Future { (subscriber: @escaping (Result<Int,Error>) -> Void) in
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
subscriber(.success(42))
})
}
struct MyPublisher : Publisher {
public typealias Failure = Error
public typealias Output = Int
@available(iOS 13.0, *)
public func receive<S>(subscriber: S) where S : Subscriber, Failure == S.Failure, Output == S.Input {
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
_ = subscriber.receive(42)
subscriber.receive(completion: .finished)
})
// Using RxSwift Observables
let observable: Observable<Int> = Observable.create { observer in
// could be anything async, e.g. a network call
DispatchQueue.main.asyncAfter(deadline: .now() + .seconds(2), execute: {
observer.onNext(42)
observer.onCompleted()
})
return Disposables.create()
}
@hermanbanken
hermanbanken / 1.Combine.swift
Last active June 4, 2019 23:39
Combine vs RxSwift
Publishers.Future { (n: @escaping (Result<_, _>) -> Void) in
Timer.init(timeInterval: 100, repeats: false) { (_) in
n(.value(1))
}
}
@hermanbanken
hermanbanken / README.md
Last active May 21, 2019 16:18
Redis Hashing

Redis SCAN cursor

Have you ever wondered what the cursor value that Redis returns for SCAN commands means? I did, and set out to make sense of these seemingly arbitrary numbers.

Some time ago I was reading a StackOverflow article, that linked the documentation inside the Redis SCAN implementation. At the time, I only quickly skimmed the text. I remember thinking that it should be possible to come up with an estimate of how far along the SCAN is.

Today I 'cracked' the algorithm. It is not really that much of an achievement, given it is thoroughly documented, but figuring it out without the source code documentation still gave me a Eureka-moment. The result:

@hermanbanken
hermanbanken / .gitignore
Last active February 28, 2019 08:27
Radix tree, or trie's
node_modules