Skip to content

Instantly share code, notes, and snippets.

View dennisvennink's full-sized avatar

Dennis Vennink dennisvennink

  • Rotterdam, The Netherlands
View GitHub Profile
@dennisvennink
dennisvennink / ZipLongest1.swift
Last active November 8, 2018 21:57
Implementations of `zipLongest(_:_:)` Without Any Custom Types
// 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)
@dennisvennink
dennisvennink / headAndTail.swift
Last active January 17, 2021 09:49 — forked from ole/headAndTail.swift
Swift challenge: Sequence.headAndTail
extension Sequence {
var headAndTail: (head: Element, tail: SubSequence)? {
var head: Element?
let tail = drop {
if head == nil {
head = $0
return true
} else {
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
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) {
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 {
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 {
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 {
/// 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()
/// 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
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)
}