forked from pNre/ExSwift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sequence.swift
48 lines (42 loc) · 1.11 KB
/
Sequence.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//
// Sequence.swift
// ExSwift
//
// Created by Colin Eberhardt on 24/06/2014.
// Copyright (c) 2014 pNre. All rights reserved.
//
import Foundation
extension SequenceOf {
func first () -> T? {
var generator = self.generate();
return generator.next()
}
func skip (n:Int) -> SequenceOf<T> {
var generator = self.generate();
for _ in 0..n {
generator.next()
}
return SequenceOf(generator)
}
func skipWhile(condition:(T) -> Bool) -> SequenceOf<T> {
var generator = self.generate();
var keepSkipping = true
while keepSkipping {
if let nextItem = generator.next() {
keepSkipping = condition(nextItem)
} else {
keepSkipping = false
}
}
return SequenceOf(generator)
}
func contains<T:Equatable> (item: T) -> Bool {
var generator = self.generate();
while let nextItem = generator.next() {
if nextItem as T == item {
return true;
}
}
return false
}
}