protocol-oriented integers に想うジェネリックプログラミングの未来

27

Upload: tomohiro-kumagai

Post on 21-Jan-2018

1.367 views

Category:

Environment


0 download

TRANSCRIPT

Page 1: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 2: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 3: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 4: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 5: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 6: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 7: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

func sum<T : Numeric>(of values: Array<T>) -> T {

return values.reduce(0) { $0 + $1 }}

// Int の配列でも、Float の配列でも、使える let integers = [5, 5, 5, 5]let floats = [2.5, 2.5, 2.5, 2.5] as [Float]

sum(of: integers) // 20sum(of: floats) // 10

Page 8: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 9: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 10: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

func average <C:Collection> (of values: C ) -> C.Element where C.Element : BinaryInteger {

return values.reduce(into: 0, +=) / C.Element(values.count)}

Page 11: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 12: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

extension Array {

// 変換元は、要素の型が同じならどんな Sequence でも良い init<S:Sequence>(_ s: S) where Element == S.Element}

// どんな Sequence も配列型に変換できる Array([1, 5, 8, 3, 7, 3]) // ArrayArray([1, 5, 8, 3, 7, 3][2 ..< 5]) // ArraySliceArray(1 ..< 20) // CountableRangeArray(zip([1, 3, 5], [2, 4, 6])) // Zip2Sequence<Array, Array>

Page 13: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

// この書き方はパッと思いつかなくても、

func sum<T : Numeric>(of values: Array<T>) -> T {

return values.reduce(into: 0, +=)}

// こちらなら自然と書ける人も多いはず(上記より Swift らしいと思う)

extension Array where Element : Numeric {

func sum() -> Element {

return reduce(into: 0, +=) }}

Page 14: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 15: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 16: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

struct Array : MutableCollectionType, Sliceable, ArrayLiteralConvertible {

}

Page 17: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

// Swift 1 は“写像(map)”が主体 func map<C:CollectionType, T>(source: C, transform: (C.Generator.Element) -> T) -> [T]

// Swift 2 は“コレクション”が主体 extension CollectionType {

func map<T>(transform: (Generator.Element) -> T) -> [T]

}

Page 18: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 19: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 20: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

func areaOfCircle<T:FloatingPoint>(withRadius radius: T) -> T { return radius * radius * T.pi}

areaOfCircle(withRadius: 2 as Double) // 12.566370614359172areaOfCircle(withRadius: 2 as Float) // 12.56637areaOfCircle(withRadius: 2 as Float80) // 12.566370614359172464

Page 21: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 22: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

extension String {

// 現在主流の API func repeated(_ count: Int) -> String

// Protocol-Oriented Integers を用いた API func repeated<T:BinaryInteger>(_ count: T) -> String

}

"*".repeated(5) // "*****"

Page 23: Protocol-Oriented Integers に想うジェネリックプログラミングの未来

extension String {

func repeated<T:UnsignedInteger>(_ count: T) -> String

}

"*".repeated(5)

Page 24: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 25: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 26: Protocol-Oriented Integers に想うジェネリックプログラミングの未来
Page 27: Protocol-Oriented Integers に想うジェネリックプログラミングの未来