HyunWoo Lee
May 12, 2023
150

# Functional Kotlin makes Kotlin functional

KotlinConf 2023 Global in Songdo에서 발표한 코틀린 함수형 프로그래밍 발표 자료입니다.

May 12, 2023

## Transcript

1. @l2hyunwoo

KotlinConf’23 Global in Songdo

Mathpresso Inc. Android/React Native Developer
Functional Kotlin

Makes

Kotlin functional

HyunWoo Lee

2. ೣࣻഋ ೐۽Ӓې߁җ য়೧
Lambda Algebra Monoid
Functor / Applicative Functor Tail recursion
Currying

3. ೣࣻഋ ೐۽Ӓې߁җ য়೧
Lambda Algebra Monoid
Functor / Applicative Functor Tail recursion
Currying
੿݈ ੉ѱ पઁ ѐߊী ੄޷о ੓ਸөਃ?

4. ೣࣻഋ ೐۽Ӓې߁ ੽Ӕ
ݾ੸ਸ ੉೧ೞҊ

যڌѱ ࢎਊ೧ঠೡ ૑ ঌইঠ ೞݴ

Ӓ ੉റী ޖ঺੉ ੓ח ૑ ౵ঈ೧ب ן૑ ঋ׮

5. Why we should use it?
ೣࣻഋ ೐۽Ӓې߁ਸ ഝਊೞݶ

௏٘ܳ рѾೞѱ ੘ࢿೡ ࣻ ੓Ҋ ӝמ੄ ഛ੢੉ ए਋ݴ

ࠁ׮ উ੹ೠ ௏٘ܳ ੘ࢿೡ ࣻ ੓׮

6. рѾೠ ௏٘ ੘ࢿೞӝ

7. рѾೠ ௏٘ ੘ࢿೞӝ

8. рѾೠ ௏٘ ੘ࢿೞӝ

9. рѾೠ ௏٘ ੘ࢿೞӝ

10. ߸زਸ ઴੉ݶࢲ উ੿ࢿ ֫੉ӝ

11. ߸زਸ ઴੉ݶࢲ উ੿ࢿ ֫੉ӝ

12. ߸زਸ ઴੉ݶࢲ উ੿ࢿ ֫੉ӝ
ё୓/৻ࠗ ࢚క੄

߸ز੉ ੓ח ࠗ࠙਷

࠶۾ Scopeਵ۽

Ѻܻೠ׮

13. ਫ਼Ӭ! ੉Ѣ ೣࣻഋ ೐۽Ӓې߁੉ ݏաਃ?
Functional Programming is sometimes treated as synonymous with

Purely functional programming.

When a pure function is called with some given arguments,

it will always return the same result,

and cannot be affected by any mutable state or other side effect.

-
Hudak, Paul (Conception, evolution and application of functional programming lauguages)

14. Pure Function and Side Effect
function sum(x: int, y: int)

15. Pure Function and Side Effect
function sum(x: int, y: int)
(
2, 5
)

16. Pure Function and Side Effect
function sum(x: int, y: int)
(
2, 5
)
7

17. function sum(x: int, y: int)
(
2, 5
)
7
ੑ۱੉ زੌೞݶ

Ѿҗо ೦࢚ э਷ ೣࣻܳ

ࣽࣻ ೣࣻ
(
Pure Function)

ۄ ೠ׮
Pure Function and Side Effect

18. Pure Function and Side Effect
function sumWithSideEffect(x: int, y: int)

19. Pure Function and Side Effect
function sumWithSideEffect(x: int, y: int)
(
2, 5
)
1st

20. Pure Function and Side Effect
function sumWithSideEffect(x: int, y: int)
(
2, 5
)
7
1st

21. Pure Function and Side Effect
function sumWithSideEffect(x: int, y: int)
(
3, 6
)
2nd

22. Pure Function and Side Effect
function sumWithSideEffect(x: int, y: int)
(
3, 6
)
2nd
16

23. Pure Function and Side Effect
function sumWithSideEffect(x: int, y: int)
(
3, 6
)
2nd
16

24. Pure Function and Side Effect

25. Pure Function and Side Effect
ೣࣻ ղীࢲ ҙ੢ೞ૑ ঋח,

੹৉ ࢚క/߸ࣻ ഑਷ (౵ੌ, ചݶ) ੑ୹۱١ਸ ನೣೠ

৻ࠗ ࢚కী ৔ೱਸ ઴ ࣻ ੓Ҋ ੉ܳ ҙ଴ೞח ೯ਤܳ ನೣೞח ೣࣻܳ

Side Effectۄ ೠ׮.

26. Pure Function and Side Effect
ೣࣻഋ ೐۽Ӓې߁ਸ ഝਊೞݶ

௏٘ܳ рѾೞѱ ੘ࢿೡ ࣻ ੓Ҋ ӝמ੄ ഛ੢੉ ए਋ݴ

ࠁ׮ উ੹ೠ ௏٘ܳ ੘ࢿೡ ࣻ ੓׮

27. Pure Function and Side Effect
ࣽࣻೣࣻܳ ࢎਊೞݶ زੌೠ Ѿҗܳ ৘ஏೡ ࣻ ੓ਵ޲۽ (ଵઑ ైݺࢿ)

ೣࣻ Ӓ ੗୓ܳ ݽٕചػ ӝמਵ۽ ࢎਊೡ ࣻ ੓׮.

ژೠ ৻ࠗ о߸ ࢚కܳ ߸҃ೞ૑ ঋਵ޲۽

زदࢿ ೐۽Ӓې߁ীࢲ ҕਬ ੗ਗ ߸҃ী ؀ೠ ҙबࢎܳ ઁೡ ࣻ ੓׮.

੉۠ ੉੼ਵ۽ ೣࣻഋ ೐۽Ӓې߁ীࢲח ࣽࣻ ೣࣻ੄ ࢎਊਸ ૑ೱೠ׮.

28. Aspect of Functional Programming
Ӓ۞ݶ ࣽࣻ ೣࣻ݅ਸ ࢎਊೞח Ѫ੉

ೣࣻഋ ೐۽Ӓې߁ ߑधੌө?

29. Aspect of Functional Programming
Ӓۧӝח ೞա,

Ӓېо૑Ҍ рױೠ ӝמ ೞաઑର

ઁ؀۽ ٜ݅ ࣻ হ׮

30. Aspect of Functional Programming
৻ࠗ ࣁ҅ܳ ߸҃ਸ दெঠ ೞա

߸҃ਸ दఃח ࠗ࠙ਸ ୭ࣗചೞҊ ҳ࠙(Ѻܻ)दః੗

31. ߸زਸ ઴੉ݶࢲ উ੿ࢿ ֫੉ӝ
ё୓/৻ࠗ ࢚క੄

߸ز੉ ੓ח ࠗ࠙਷

࠶۾ Scopeਵ۽

Ѻܻೠ׮

32. Ҋରೣࣻܳ ഝਊೠ ӝמ ഛ੢

33. Ҋରೣࣻܳ ഝਊೠ ӝמ ഛ੢
CSVо ইצ ׮ܲ ౵ੌ ഋध

഑਷ Plain Text۽ Append
ೞח ӝמ੉ ୶оػ׮ݶ?

34. Ҋରೣࣻܳ ഝਊೠ ӝמ ഛ੢
ё୓ܳ Stringਵ۽ ੹ജೞח ۽૒ਸ
৻ࠗীࢲ ઱ੑदெ

ҕా ۽૒ী ׮ഋࢿਸ ઁҕ

35. ઺р ੼Ѩ
ਸ ੉೧ೞҊ

ࢎਊ೧ঠೡ ૑ ঌইঠ ೞݴ

Ӓ ੉റী ޖ঺੉ ੓ח ૑ ౵ঈ೧ب ן૑ ঋ׮
ݾ੸
যڌѱ

36. ઺р ੼Ѩ
→ উ੿ࢿ, ਬ૑ࠁࣻࢿ, оةࢿ ૐо

→ ࣽࣻೣࣻ ഝਊ, Side Effect Ѻܻ
ݾ੸
যڌѱ

37. More, Functional Programming
Ҋରೣࣻܳ ഝਊೞৈ ੤ࢎਊࢿਸ ՑযৢܻҊ

ࠁ׮ ؊ উ੿੸ਵ۽ ೐۽Ӓې߁ਸ ೞҊ र׮ݶ?

38. More, Functional Programming
Ҋରೣࣻܳ ഝਊೞৈ ੤ࢎਊࢿਸ ՑযৢܻҊ

ࠁ׮ ؊ উ੿੸ਵ۽ ೐۽Ӓې߁ਸ ೞҊ र׮ݶ?
Ҋରೣࣻܳ ഝਊೞח ё୓(ё୓ ஶప੉ց)੄

ౠࢿਸ ঌҊ, ੉ܳ ഝਊೞݶ ػ׮

39. Category Theory
Ҋରೣࣻܳ ഝਊೞৈ ࢜۽਍ ఋੑ/ӝמਸ ઁҕೞӝ ਤ೧ࢶ

੉ܳ ҳࢿೞח ఋੑী ؀೧ ঌইঠ ೠ׮.

40. ೣࣻഋী ੸੺ೠ “ஶప੉ց"

41. ೣࣻഋী ੸੺ೠ “ஶప੉ց"

42. ೣࣻഋী ੸੺ೠ “ஶప੉ց"

43. Category Theory
ߧ઱ۿ
(
Category Theory)ীࢲח ҳઑ୓
(
Object)৬

ҳઑ୓ٜр੄ ҙ҅
(
Morphism, ࢎ࢚)ܳ ׮ܘ׮

44. Functor
ف ஠పҊܻ р੄ ೣࣻ੉Ҋ

ҳઑ୓ח ҳઑ୓۽, ೣࣻח ೣࣻ۽ ؀਽दఃח ೣࣻ

45. Functor
੉۞ݶ য٣ࢲ ݆੉ࠄ Ѫ э૑ ঋաਃ?

46. Functor

47. Functor੄ ઺୏ ޙઁ

48. Monoid
ਤ੄ ࢿ૕ਸ ݅઒ೞח ஠పҊܻ

49. Monoid
ਤ੄ ࢿ૕ਸ ݅઒ೞח ஠పҊܻ

(ো࢑઺ যו ੌࠗ࠙ਸ ݢ੷ ࣻ೯ೞ؊ۄب ҡଳ׮ ੿ب੄ ੄޷)

੉ ࢿ૕ਸ ഝਊೞৈ ઺୏ػ ఋੑਸ ߩӡ ࣻ ੓׮

flatMap ೣࣻܳ ࢤп೧ࠁ੗!

Nested Optional۽ оٙସ؍ ௏٘ب

56. Monad ਽ਊ: ৈ۞ ா੉झܳ ೠ ଃਵ۽

57. Monad ਽ਊ: ৈ۞ ா੉झܳ ೠ ଃਵ۽
ী۞ ߊࢤ द ী۞ ఋੑਸ Monad ఋੑਵ۽ ೠߣ ؊ хऱળ׮ݶ?

58. Monadܳ ഝਊೠ ࢚క ୊ܻ੄ ੉੼
Monad ղࠗ ࢚కܳ ইח Ѫҗ ࢚ҙহ੉

ѐߊ੗о ۽૒ਸ ୓੉׬೧ب ޙઁо হب۾ ୊ܻ

ೡ ࣻ ੓׮

Spring Reactor(Mono)

RxJava (Kotlin)

Kotlin Result