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

Mobile Programming Closure

Mobile Programming Closure

Mobile Programmingの授業のスライドです

yuichiro_takahashi

October 22, 2018
Tweet

More Decks by yuichiro_takahashi

Other Decks in Technology

Transcript

  1. Ϋϩʔδϟ෼ΫοΩϯά { (number1: Int, number2: Int) -> Int in }

    w ࣍ʹΫϩʔδϟ͕ड͚औΔ஋Ҿ਺Λઃఆ͍͖ͯ͠·͢ w ࠓճ͸଍͠ࢉͷؔ਺ͳͷͰҾ਺͸ೋͭʹͳΓ·͢ w ؔ਺ͱಉ͡Α͏ʹҾ਺͸໊લܕͱ͍͏ܗͰఆٛ͞Ε w ෳ਺͋Δ৔߹͸ΧϯϚͰ۠੾Γ·͢ Ҿ਺ Ҿ਺ Ҿ਺ಉ࢜͸ΧϯϚͰ۠੾Δ
  2. Ϋϩʔδϟ෼ΫοΩϯά { (number1: Int, number2: Int) -> Int in let

    result = number1 + number2 return result } w ࠷ޙʹΫϩʔδϟͷத਎Λهड़͍͖ͯ͠·͢ w ໭Γ஋͕͋Δؔ਺ͷ৔߹͸ඞͣ࠷ޙʹ w SFUVSOΛ࢖༻ͯ͠஋Λฦ͢ඞཁ͕͋Γ·͢ ໭Γ஋Λฦ͢ ؔ਺Ͱߦ͏ॲཧ
  3. Ϋϩʔδϟͷݺͼग़͠ w ؔ਺ͱಉ༷ʹΫϩʔδϟ͸ݺͼग़͞ΕΔ·Ͱ͸࣮ߦ͞Εͳ͍ w ͦͷͨΊఆٛͨ͠ޙʹඞͣݺͼग़͢ඞཁ͕͋Δ w ΫϩʔδϟΛݺͼग़͢৔߹͸جຊతʹҰ౓ม਺ʹೖΕΔඞཁ͕͋Δ // Ϋϩʔδϟͷఆٛ let

    closure = { (number1: Int, number2: Int) -> Int in let result = number1 + number2 return result } // Ϋϩʔδϟͷݺͼग़͠ let added = closure(10, 10) print("result is: \(added)") // 20
  4. ؔ਺ܕҾ਺ʁ // ΫϩʔδϟΛҾ਺ʹؚΉؔ਺ఆٛ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ؔ਺ݺͼग़͠(ΫϩʔδϟҎ֎ͷҾ਺ΛຒΊΔ) calculate(number1: 10, number2: 10, calc:)
  5. ؔ਺ܕҾ਺ʁ // ΫϩʔδϟΛҾ਺ʹؚΉؔ਺ఆٛ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ؔ਺ݺͼग़͠(Ϋϩʔδϟͷ࢝·ΓͷΧοίΛॻ͍ͯɾɾɾ)͘ calculate(number1: 10, number2: 10, calc: { })
  6. ؔ਺ܕҾ਺ʁ // ΫϩʔδϟΛҾ਺ʹؚΉؔ਺ఆٛ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ؔ਺ݺͼग़͠(Ҿ਺ͷ໊લͱܕͱ໭Γ஋ͷܕΛॻ͍ͯɾɾɾɾ) calculate(number1: 10, number2: 10, calc: { (num1: Int, num2: Int) -> Int })
  7. ؔ਺ܕҾ਺ʁ // ΫϩʔδϟΛҾ਺ʹؚΉؔ਺ఆٛ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ؔ਺ݺͼग़͠(in ΩʔϫʔυΛॻ͍ͯɾɾɾ) calculate(number1: 10, number2: 10, calc: { (num1: Int, num2: Int) -> Int in })
  8. ؔ਺ܕҾ਺ʁ // ΫϩʔδϟΛҾ਺ʹؚΉؔ਺ఆٛ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ؔ਺ݺͼग़͠(࠷ޙʹॲཧΛॻ͘ɾɾɾ) calculate(number1: 10, number2: 10, calc: { (num1: Int, num2: Int) -> Int in return num1 + num2 })
  9. ͖ͬ͞ͷྫΛ΋͏Ұ౓ // ͬͪ͜͸লུͰ͖ͳ͍͕ɾɾɾ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ͬͪ͜͸লུ͢Δͱɾɾɾ calculate(number1: 10, number2: 10, calc: { (num1: Int, num2: Int) -> Int return num1 + num2 }) ͜ͷ௕͍Ϋϩʔδϟ͕͋Δ͡ΌΖʁ
  10. ͖ͬ͞ͷྫΛ΋͏Ұ౓ // ͬͪ͜͸লུͰ͖ͳ͍͕ɾɾɾ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ͬͪ͜͸লུ͢Δͱɾɾɾ calculate(number1: 10, number2: 10) { (num1, num2) in return num1 + num2 } ͜ΕΛ͜͏ͯ͠ɾɾɾ
  11. ͖ͬ͞ͷྫΛ΋͏Ұ౓ // ͬͪ͜͸লུͰ͖ͳ͍͕ɾɾɾ func calculate(number1: Int, number2: Int, calc: ((Int,

    Int) -> Int)) -> Int { return calc(number1, number2) } // ͬͪ͜͸লུ͢Δͱɾɾɾ calculate(number1: 10, number2: 10ʣ{ $0 + $1 } ͜͏͡Όʂ
  12. Ϋϩʔδϟͷޙஔه๏ w ؔ਺ͷҾ਺ͷ࠷ޙ͕Ϋϩʔδϟͷ৔߹ͦͷΫϩʔδϟΛҾ ਺Ϧετͷ֎ʹग़͢͜ͱ͕Ͱ͖·͢ w ͜ΕΛΫϩʔδϟͷޙஔه๏ 5SBJMMJOH$MPTVSF ͱݺͼ· ͢ calculate(number1:

    10, number2: 10) { (num1: Int, num2: Int) -> Int in return num1 + num2 } calculate(number1: 10, number2: 10, calc: { (num1: Int, num2: Int) -> Int return num1 + num2 }) #FGPSF "GUFS
  13. Ϋϩʔδϟͷܕͷলུ w 4XJGUʹ͸ܕਪ࿦͕͋ΔͨΊΫϩʔδϟͷҾ਺ͱ໭Γ஋ͷ ܕΛলུͰ͖Δ৔߹͕͋Γ·͢ calculate(number1: 10, number2: 10) { (num1,

    num2) in return num1 + num2 } calculate(number1: 10, number2: 10) { (num1: Int, num2: Int) -> Int in return num1 + num2 } #FGPSF "GUFS
  14. Ϋϩʔδϟͷม਺໊ͷলུ w Ϋϩʔδϟͷॲཧ͕ߦͰࡁΉ৔߹͸ม਺ͷ໊લ΋লུ͢Δ͜ͱ͕ Ͱ͖·͢ w ୅ΘΓʹOͱ͍͏දهΛ࢖͍·͢ w Ҿ਺͕ෳ਺͋Δ৔߹͸ࠨ͔Β ʜͱ͍͏ܗʹͳΓ·͢ w

    ·ͨɺSFUVSO΋লུՄೳͰ͢ calculate(number1: 10, number2: 10) { $0 + $1 } calculate(number1: 10, number2: 10) { (num1, num2) in return num1 + num2 } #FGPSF "GUFS