函數
sequence(state:
sequence(state:next:)
將由
next
的重複惰性應用程序形成的序列返回到可變的 state
。聲明
func sequence<T, State>(
state: State,
next: @escaping (inout State) -> T?
) -> UnfoldSequence<T, State>
返回值
從 next
產生每個連續值的序列。
參數
state
將傳遞給閉包的初始狀態。
next
一個接受
inout
狀態並返回序列的下一個元素的閉包。
詳述
序列的元素是通過調用具有可變狀態的next
來獲得的。相同的狀態被傳遞給 next
的所有調用,因此後續調用將看到先前調用所做的任何突變。當 next
返回 nil
時,序列結束。如果 next
從不返回 nil
,則序列是無限的。
此函數可用於替換許多包裝閉包的AnyIterator
實例。
例子:
// Interleave two sequences that yield the same element type
sequence(state: (false, seq1.makeIterator(), seq2.makeIterator()), next: { iters in
iters.0 = !iters.0
return iters.0 ? iters.1.next() : iters.2.next()
})
可用版本
iOS 8.0+, iPadOS 8.0+, macOS 10.10+, Mac Catalyst 13.0+, tvOS 9.0+, watchOS 2.0+
相關用法
- Swift sequence(first:next:)用法及代碼示例
- Swift stride(from:through:by:)用法及代碼示例
- Swift stride(from:to:by:)用法及代碼示例
- Swift KeyValuePairs flatMap(_:)用法及代碼示例
- Swift String.UTF8View first用法及代碼示例
- Swift Result.Publisher zip(_:_:_:)用法及代碼示例
- Swift Optional.Publisher reduce(_:_:)用法及代碼示例
- Swift Int8 ~(_:)用法及代碼示例
- Swift SetAlgebra isStrictSubset(of:)用法及代碼示例
- Swift UInt +(_:)用法及代碼示例
- Swift Array enumerated()用法及代碼示例
- Swift FlattenSequence prefix(_:)用法及代碼示例
- Swift Slice endIndex用法及代碼示例
- Swift LazySequence split(maxSplits:omittingEmptySubsequences:whereSeparator:)用法及代碼示例
- Swift MutableCollection partition(by:)用法及代碼示例
- Swift ReversedCollection min(by:)用法及代碼示例
- Swift RandomNumberGenerator用法及代碼示例
- Swift Dictionary.Keys shuffled()用法及代碼示例
- Swift AnySequence elementsEqual(_:)用法及代碼示例
- Swift UInt &<<(_:_:)用法及代碼示例
- Swift Optional.Publisher tryDrop(while:)用法及代碼示例
- Swift DefaultIndices endIndex用法及代碼示例
- Swift Substring.UnicodeScalarView insert(contentsOf:at:)用法及代碼示例
- Swift LazyFilterSequence dropFirst(_:)用法及代碼示例
- Swift LazySequence suffix(from:)用法及代碼示例
注:本文由純淨天空篩選整理自apple.com大神的英文原創作品 sequence(state:next:)。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。