This file contains 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
// Using `sequence(state:next:).lazy`. | |
func zipLongest <Sequence1: Sequence, Sequence2: Sequence> | |
(_ sequence1: Sequence1, _ sequence2: Sequence2) -> | |
LazySequence<UnfoldSequence<(Sequence1.Element?, Sequence2.Element?), | |
(Sequence1.Iterator, Sequence2.Iterator)>> { | |
return sequence(state: (sequence1.makeIterator(), sequence2.makeIterator())) { | |
let element1 = $0.0.next() | |
let element2 = $0.1.next() | |
return element1 == nil && element2 == nil ? nil : (element1, element2) |
This file contains 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 Sequence { | |
var headAndTail: (head: Element, tail: SubSequence)? { | |
var head: Element? | |
let tail = drop { | |
if head == nil { | |
head = $0 | |
return true | |
} else { |
This file contains 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
zipLongest :: [a] -> [b] -> [(Maybe a, Maybe b)] | |
zipLongest [] [] = [] | |
zipLongest [] (y:ys) = [(Nothing, Just y)] ++ zipLongest [] ys | |
zipLongest (x:xs) [] = [(Just x, Nothing)] ++ zipLongest xs [] | |
zipLongest (x:xs) (y:ys) = [(Just x, Just y)] ++ zipLongest xs ys |
This file contains 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
let xs = [1, 2, 3, 4, 5] | |
for (element, index) in zip(xs, xs.indices) { | |
if index == xs.startIndex { | |
print("START") | |
} | |
print(element) | |
if index == xs.index(before: xs.endIndex) { |
This file contains 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
public struct Pair <Type1, Type2> { | |
public let first: Type1 | |
public let second: Type2 | |
public init (_ first: Type1, _ second: Type2) { | |
(self.first, self.second) = (first, second) | |
} | |
} | |
extension Pair { |
This file contains 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
public struct Zip <Sequence1: Sequence, Sequence2: Sequence> { | |
public struct ShortestSequence: Sequence { | |
internal let sequence1: Sequence1 | |
internal let sequence2: Sequence2 | |
internal init (_ sequence1: Sequence1, _ sequence2: Sequence2) { | |
(self.sequence1, self.sequence2) = (sequence1, sequence2) | |
} | |
public struct Iterator: IteratorProtocol { |
This file contains 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 XCTest | |
@testable import SetUnion | |
final class SetUnionTests: XCTestCase { | |
let a1: [Set<Int>?] = [[1, 2], [2, 3], nil] | |
let n = 100000 | |
func testX1 () { | |
self.measure { | |
for _ in 1...self.n { |
This file contains 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
/// The iterator for `LazyCycleSequence`. | |
public struct LazyCycleIterator <BaseSequence: Sequence>: IteratorProtocol { | |
public typealias Element = BaseSequence.Iterator.Element | |
private let baseSequence: BaseSequence | |
private var baseIterator: BaseSequence.Iterator | |
internal init (_ baseSequence: BaseSequence) { | |
self.baseSequence = baseSequence | |
self.baseIterator = self.baseSequence.makeIterator() |
This file contains 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
/// The iterator for `LazyAppendSequence`. | |
public struct LazyAppendIterator <BaseIterator1: IteratorProtocol, BaseIterator2: IteratorProtocol>: IteratorProtocol | |
where BaseIterator1.Element == BaseIterator2.Element { | |
public typealias Element = BaseIterator1.Element | |
private var baseIterator1: BaseIterator1 | |
private var baseIterator2: BaseIterator2 | |
private var reachedEndOfBaseIterator1 = false | |
private var reachedEnd = false |
This file contains 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
let s1 = [1, 2] | |
let s2 = [3, 4, 5] | |
func product <Sequence1: Sequence, Sequence2: Sequence> (_ s1: Sequence1, _ s2: Sequence2) -> AnySequence<(Sequence1.Element, Sequence2.Element)> { | |
return AnySequence( | |
s1.flatMap { e1 in | |
s2.map { e2 in | |
print(e1, e2) | |
return (e1, e2) | |
} |
NewerOlder