forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathalgorithms.swift
45 lines (37 loc) · 1.04 KB
/
algorithms.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
// RUN: %target-run-simple-swift | FileCheck %s
// REQUIRES: executable_test
func fib() {
var (a, b) = (0, 1)
while b < 10 {
print(b)
(a, b) = (b, a+b)
}
}
fib()
// CHECK: 1
// CHECK: 1
// CHECK: 2
// CHECK: 3
// CHECK: 5
// CHECK: 8
// From: <rdar://problem/17796401>
// FIXME: <rdar://problem/21993692> type checker too slow
let two_oneA = [1, 2, 3, 4].lazy.reverse()
let two_one = Array(two_oneA.filter { $0 % 2 == 0 }.map { $0 / 2 })
print(two_one)
// CHECK: [2, 1]
// rdar://problem/18208283
func flatten<Element, Sequence: SequenceType, InnerSequence: SequenceType
where Sequence.Generator.Element == InnerSequence, InnerSequence.Generator.Element == Element> (outerSequence: Sequence) -> [Element] {
var result = [Element]()
for innerSequence in outerSequence {
result.appendContentsOf(innerSequence)
}
return result
}
// CHECK: [1, 2, 3, 4, 5, 6]
let flat = flatten([[1,2,3], [4,5,6]])
print(flat)
// rdar://problem/19416848
func observe<T:SequenceType, V where V == T.Generator.Element>(g:T) { }
observe(["a":1])