func reduce<T>(_ initialResult: T,
_ nextPartialResult: @escaping (T, Self.Output) -> T) -> Publishers.Reduce<Self, T>
/******demo code*******/
_ = [1,2,3,4,5].publisher
.reduce(0, +)
.sink{print("get value:", $0)}
/******console*********/
get value: 15
/*******Rarbles********/
---1---2---3---4---5---|
reduce(0,+)
------------------------15--|
func collect(_ count: Int) -> Publishers.CollectByCount<Self>
/******demo code*******/
_ = [1,2,3,4,5].publisher
.collect(3)
.sink{print("get value:", $0)}
/******console*********/
get value: [1, 2, 3]
get value: [4, 5]
/*******Rarbles********/
---1---2---3---4---5---|
.collect(3)
-----------[1,2,3]-----[4,5]--|
這系列的 Operator 很像 isSOME:Bool
func allSatisfy(_ predicate: @escaping (Self.Output) -> Bool) -> Publishers.AllSatisfy<Self>
/******demo code*******/
_ = (1...5).publisher
.allSatisfy{$0 > 0}
.print()
.sink{print("get value:", $0)}
/******console*********/
receive subscription: (Once)
request unlimited
receive value: (true)
get value: true
receive finished
/*******Rarbles********/
---1---2---3---4---5---|
allSatisfy{$0 > 0}
-----------------------T--|
將這類的 Operator 想成 Sequence,會好一些。
func first() -> Publishers.First<Self>
/******demo code*******/
_ = (1...5).publisher
.first()
.print()
.sink{print("get value:", $0)}
/******console*********/
receive subscription: (Optional)
request unlimited
receive value: (1)
get value: 1
receive finished
/*******Rarbles********/
---1---2---3---4---5---|
.first()
---1---|
/******demo code*******/
_ = (1...5).publisher
.last()
.sink{print("get value:", $0)}
/******console*********/
get value: 5
/*******Rarbles********/
---1---2---3---4---5---|
.first()
-----------------------5---|
/*
func drop(untilOutputFrom:)
/******demo code*******/
_ = (0...5).publisher
.drop{$0.isMultiple(of: 2)}
.sink{print("get value:", $0)}
/******console*********/
get value: 1
get value: 2
get value: 3
get value: 4
get value: 5
/*******Rarbles********/
---0---1---2---3---4---5---|
drop($0.isMultiple(of: 2))
-------1---2---3---4---5---|
func append<S>(_ elements: S) -> Publishers.Concatenate<Self, Publishers.Sequence<S, Self.Failure>> where S : Sequence, Self.Output == S.Element
/******demo code*******/
_ = (0...2).publisher
.append(3, 4, 5)
.sink{print("get value:", $0)}
/******console*********/
get value: 0
get value: 1
get value: 2
get value: 3
get value: 4
get value: 5
/*******Rarbles********/
---1---2---|
append(3, 4, 5)
---3---4---5---|
---0---1---2---3---4---5---|
func prepend(_ elements: Self.Output...) -> Publishers.Concatenate<Publishers.Sequence<[Self.Output], Self.Failure>, Self>
/******demo code*******/
_ = (3...5).publisher
.prepend(1, 2)
.sink{print("get value:", $0)}
/******console*********/
get value: 1
get value: 2
get value: 3
get value: 4
get value: 5
/*******Rarbles********/
---3---4---5---|
prepend(1, 2)
---1---2---|
---0---1---2------3---4---5---|