Skip to content

Instantly share code, notes, and snippets.

View ramunasjurgilas's full-sized avatar

Ramūnas ramunasjurgilas

View GitHub Profile
@ramunasjurgilas
ramunasjurgilas / MergeWith.Swift
Created February 27, 2020 13:52
Combining operator: merge(with:)
let strings1 = PassthroughSubject<String, Never>()
let strings2 = PassthroughSubject<String, Never>()
strings1
.merge(with: strings2)
.sink { print($0) }
strings2.send("A")
strings1.send("B")
@ramunasjurgilas
ramunasjurgilas / SwitchToLatest.swift
Last active February 27, 2020 12:30
Combining operator: switchToLatest()
let intPublisher1 = PassthroughSubject<Int, Never>()
let intPublisher2 = PassthroughSubject<Int, Never>()
let publishers = PassthroughSubject<PassthroughSubject<Int, Never>, Never>()
publishers.switchToLatest().sink { print($0) }
publishers.send(intPublisher1)
intPublisher1.send(-3)
@ramunasjurgilas
ramunasjurgilas / Append.swift
Created February 27, 2020 12:08
Combining operator: append(_:)
let integers = (10...12).publisher // A
let moreIntegers = (20...22).publisher // F
integers
.append(1, 2) // B
.append(99...101) // C
.append([-100, -200]) // D
.append(moreIntegers) // E
.sink { print($0) }
@ramunasjurgilas
ramunasjurgilas / Prepend.swift
Last active February 27, 2020 12:07
Combining operator: prepend(_:)
let integers = (10...12).publisher // F
let moreIntegers = (20...22).publisher // A
integers
.prepend(1, 2) // B
.prepend(99...101) // C
.prepend([-100, -200]) // D
.prepend(moreIntegers) // E
.sink { print($0) }
@ramunasjurgilas
ramunasjurgilas / PrefixUntilOutputFrom.swift
Created February 27, 2020 08:21
prefix(untilOutputFrom:) as filtering operator
let on = PassthroughSubject<Void, Never>()
let strings = PassthroughSubject<String, Never>()
strings
.prefix(untilOutputFrom: on)
.sink(receiveValue: { print($0) })
["a1", "b1", "c3", "d1", "e5"]
.forEach {
strings.send($0)
@ramunasjurgilas
ramunasjurgilas / PrefixWhile.swift
Created February 27, 2020 08:05
prefix(while:) as filtering operator
let strings = PassthroughSubject<String, Never>()
strings
.prefix(while: { $0.hasSuffix("1") })
.sink(receiveValue: { print($0) })
["a1", "b1", "c3", "d1", "e5"]
.forEach {
strings.send($0)
}
@ramunasjurgilas
ramunasjurgilas / Prefix.swift
Created February 27, 2020 07:58
prefix() as filtering operator
let strings = PassthroughSubject<String, Never>()
strings
.prefix(3)
.sink(receiveValue: { print($0) })
["a", "b", "c", "d", "e"]
.forEach {
strings.send($0)
}
@ramunasjurgilas
ramunasjurgilas / DropUntilOutputFrom.swift
Created February 27, 2020 07:40
dropUntilOutputFrom() as filtering operator
let on = PassthroughSubject<Void, Never>()
let strings = PassthroughSubject<String, Never>()
strings
.drop(untilOutputFrom: on)
.sink(receiveValue: { print($0) })
["a0", "a1", "b2", "a3", "c4", "a5", "d6"]
.forEach {
strings.send($0)
@ramunasjurgilas
ramunasjurgilas / DropWhile.swift
Created February 27, 2020 07:21
dropWhile() as filtering operator
import Combine
["a0", "a1", "b2", "a3", "c4", "a5", "d6"]
.publisher
.drop(while: { $0.hasPrefix("a") })
.sink { print($0) }
// Output:
// b2
// a3
@ramunasjurgilas
ramunasjurgilas / DropFirst.swift
Created February 27, 2020 07:10
dropFirst() as filtering operator
[1, 2, 3]
.publisher
.dropFirst()
.sink { print($0) }
// Output:
// 2
// 3