Array に each を実装してみた。

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
extension Array {
    func each(block: (T) -> ()) {
        for item in self {
            block(item)
        }
    }

    func each<U>(block: (U) -> ()) {
        for item in self {
            block(item as! U)
        }
    }

    func eachWithIndex(block: (T, Int) -> ()) {
        for (i, item) in enumerate(self) {
            block(item, i)
        }
    }

    func eachWithIndex<U>(block: (U, Int) -> ()) {
        for (i, item) in enumerate(self) {
            block(item as! U, i)
        }
    }
}

使い方はこんな感じ。

1
2
3
4
[1, 2, 3].each { (n: Int) in println(n) }
[1, 2, 3].each { println($0) }
[1, 2, 3].eachWithIndex { (n: Int, i: Int) in println("index \(i), n \(n)") }
[1, 2, 3].eachWithIndex { println("index \($1), n \($0)") }

配列の中身が AnyObject だった時に、型指定するときとしない時でどっちも動くようにするのに、メソッドを2つ定義しました。

このへん、Generics の達人のかたに、もっといい書き方あるよって教えて欲しいです。

Swift の Dictionary に map を実装してみた。

ついでに reduce も。

1
2
3
4
5
6
7
8
9
extension Dictionary {
    func map<T>(transform: (Key, Value) -> T) -> [T] {
        return Swift.map(self, transform)
    }

    func reduce<T>(initial: T, combine: (T, (Key, Value)) -> T) -> T {
        return Swift.reduce(self, initial, combine)
    }
}

使い方はこんな感じ。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
var dict = [String: String]()
dict["hello"] = "world"

let array = dict.map { (k: String, v: String) -> String in
    return v
}
println(array)      // ["world"]


dict = [String: Int]()
dict["a"] = 1
dict["b"] = 2
dict["c"] = 3
dict["d"] = 4
dict["e"] = 5
let n = dict.reduce(0) { (n, d) -> Int in n + d.1 }    // 15

Ruby の map っぽい感じで使えるにしたかったので。