Upgrade to Pro — share decks privately, control downloads, hide ads and more …

関数を引数として渡す書き方のポイント

 関数を引数として渡す書き方のポイント

Yoshikuni Kato

September 17, 2017
Tweet

More Decks by Yoshikuni Kato

Other Decks in Programming

Transcript

  1. Who am I? • Ճ౻༝܇ʢYoshikuni Katoʣ @yoshikuni_kato • iOSΤϯδχΞʢ2.5 yearsʣ

    • Yahoo! Japan -> Φϋί • ʮϥδ΁͐ʯ͘Μ → • Interests: ઃܭ / FRP / Coordinator PaFern / UI࣮૷ 2
  2. ॻ͖ํ̍ - ΫϩʔδϟΛ௚઀ॻ͘ let array: [Int] = [1, 2, 3]

    array.map { number -> Int in return number * 2 } 5
  3. arrayͷmapͷఆٛ func map<T>(_ transform: (Element) throws -> T) rethrows ->

    [T] • mapͷҾ਺ɿElementΛड͚औͬͯTΛฦ͢Ϋϩʔδϟ • ؔ਺͸ɺ໊લ෇͖ͷΫϩʔδϟͱଊ͑ΒΕΔ • ؔ਺ࣗମΛ౉͢͜ͱ͕Ͱ͖Δ 6
  4. ॻ͖ํ̎ - ؔ਺Λ౉͢ // ઌʹؔ਺Λఆٛ func twoTimes(of number: Int) ->

    Int { return number * 2 } let array: [Int] = [1, 2, 3] array.map(twoTimes) // ؔ਺Λ౉͢ 7
  5. ύϥϝʔλʔ͕ෳ਺͋Δ৔߹ func someFunc(a: Int, b: Int) -> String { return

    "a = \(a), b = \(b)" } let array: [Int] = [1, 2, 3] array .map { number -> (a: Int, b: Int) in return (a: number, b: number) // Ұ୴λϓϧʹ͢Δ } .map(someFunc) 8
  6. ॻ͖ํ̍ - ΫϩʔδϟΛ௚઀ॻ͘ let array: [Int] = [1, 2, 3]

    array.map { number -> Sample in return Sample(number: number) } 10
  7. ॻ͖ํ̎ - ؔ਺Λ౉͢ let array: [Int] = [1, 2, 3]

    array.map(Sample.init) • ΠχγϟϥΠβ(.init) = ͦͷObjectΛฦؔ͢਺ 11
  8. ҧ͍ 2 array .map { number -> Int in return

    number * 2 } .map { number -> Sample in return Sample(number: number) } .map { sample -> Foo in return Foo(sample: sample) } array .map(twoTimes) .map(Sample.init) .map(Foo.init) 13
  9. ࢀߟ • Connec'ng View Controllers, Swi3 Talk1 • From Run'me

    Programming to Func'ons, Swi3 Talk2 2 h$ps:/ /talk.objc.io/episodes/S01E19-from-run@me-programming-to-func@ons 1 h$ps:/ /talk.objc.io/episodes/S01E05-connec;ng-view-controllers 15
  10. ͓·͚ func someFunc(a: Int, b: Int) -> String { return

    "a: \(a), b: \(b)" } // λϓϧ͸౉ͤͳ͍ let parameters = (a: 0, b: 0) someFunc(parameters) // !ʢswift3~ʣ // mapͩͱ౉ͤΔ let array: [(Int, Int)] = [(0, 0)] array.map(someFunc) // "ʢswift3Ͱ΋ʣ • ͳͥॻ͚Δͷ͔෼͔͍ͬͯΔਓ͕͍ͨΒڭ͍͑ͯͩ͘͞ 16