์ถ์ฒ
https://softwarepatternslexicon.com/patterns-kotlin/functional/combinator/
Combinator in Kotlin
Explore the Combinator Design Pattern in Kotlin, a powerful functional programming technique to combine functions, promoting flexible and reusable code. Learn how to implement and use combinators with real-world examples and programmatic illustrations.
softwarepatternslexicon.com
https://kotlinlang.org/docs/lambdas.html
Higher-order functions and lambdas | Kotlin
kotlinlang.org
ChatGPT
https://ko.wikipedia.org/wiki/%EA%B3%A0%EC%B0%A8_%ED%95%A8%EC%88%98
๊ณ ์ฐจ ํจ์ - ์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์
์ํค๋ฐฑ๊ณผ, ์ฐ๋ฆฌ ๋ชจ๋์ ๋ฐฑ๊ณผ์ฌ์ . ๊ณ ์ฐจ ํจ์(้ซๆฌกๅฝๆธ, higher-order function)๋ ์ํ๊ณผ ์ปดํจํฐ ๊ณผํ์์ ์ ์ด๋ ๋ค์ ์ค ํ๋๋ฅผ ์ํํ๋ ํจ์์ด๋ค.[1][2][3] ํ๋ ์ด์์ ํจ์๋ฅผ ์ธ์๋ก ์ทจํ๋ค. (์: ์
ko.wikipedia.org
https://www.linkedin.com/pulse/higher-order-function-amit-nadiger
Higher-order function
In programming, functions are considered to be first-class citizens if they can be treated as any other value, such as an integer or a string. Higher-order functions (HOFs) are functions that take one or more functions as arguments and/or return a function
www.linkedin.com
https://medium.com/softaai-blogs/kotlin-higher-order-functions-d70a05eff805
Kotlin Higher-Order Functions
Kotlin is a modern programming language that is designed to be both functional and object-oriented. One of the features that makes Kotlin…
medium.com
๊ณ ์ฐจํจ์ High-order Function HOF
ํจ์๋ฅผ ์ธ์๋ก ๋ฐ๊ฑฐ๋, ํจ์๋ฅผ ๊ฒฐ๊ณผ๋ก ๋ฐํํ๋ ํจ์.
๊ณ ์ฐจํจ์์ ์ฅ์
1. ์ฌ์ฌ์ฉ์ฑ
๊ณ ์ฐจํจ์๋ ๋ค์ํ๊ฒ ์ ์ฉํ ์ ์๋ ๊ณตํต ๊ธฐ๋ฅ์ ์ถ์ํ์์ผ ์ฌ์ฌ์ฉ์ฑ์ ์ด์งํ๋ค.
2. ์ ์ฐ์ฑ
ํจ์๋ฅผ ๋ค๋ฅธ ํจ์์ ์ธ์๋ก ์ ๋ฌํ ์๋ ์๊ณ , ๊ฒฐ๊ณผ๋ก ๋ฐํํ ์๋ ์์ด์ ์ฝ๋ ์ค๊ณ์ ๋์ ์ ์ฐ์ฑ์ ์ ๊ณตํ๋ค.
3. ํจ์ ํฉ์ฑ
ํจ์๋ค์ ์กฐํฉํด ๊ณ ์ฐจ ํจ์๋ฅผ ๋ง๋ค ์ ์๋ค. ํจ์๋ค์ ์ฌ์ฌ์ฉํ๊ณ , ๋ณต์กํ ์ฐ์ฐ์ ์ฝ๊ฒ ์ ์ํ ์๋ ์๋ค.
4 ํฅ์๋ ์ถ์ํ
๋ณต์กํ ๋ก์ง์ ์์ ํจ์๋ก ๋๋ ์ถ์ํํ๊ณ , ๊ณ ์ฐจ ํจ์๋ฅผ ์ฌ์ฉํด ์ด๋ค์ ๊ฒฐํฉํ๋ฉด ์ฝ๋์ ๋ชจ๋ํ์ ๊ตฌ์ฑ ์์(ํจ์๋ ๋ชจ๋)๋ค์ ๊ฒฐํฉ์ด ์ฉ์ดํด์ง๋ค.
๊ณ ์ฐจํจ์์ ๋จ์
1. ์ฑ๋ฅ
ํจ์ ์์์ ๋ค๋ฅธ ํจ์๋ฅผ ํธ์ถํ๋ฉฐ ์๊ธฐ๋ ์ค๋ฒํค๋๊ฐ ๋ฐ์ํ ์ ์๋ค. ๊ทธ๋ฌ๋ ์ด ์ค๋ฒํค๋๋ ์ผ๋ฐ์ ์ผ๋ก ๋๋ถ๋ถ ์ ํ๋ฆฌ์ผ์ด์ ์์ ๋ฌด์ํ ์ ์๋ ์์ค์ด๋ค.
2. ๋ณต์ก์ฑ ์ฆ๊ฐ
์ฝ๋๋ฅผ ๋ ๋ณต์กํ๊ณ ์ดํดํ๊ธฐ ์ด๋ ต๊ฒ ๋ง๋ค ์ ์๋ค. ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ๊ฐ๋ ์ด ์ต์ํ์ง ์์ ๊ฐ๋ฐ์์๊ฒ๋ ๋์ฑ ์ดํดํ๊ธฐ ์ด๋ ต๋ค.
3. ๋๋ฒ๊น ์ด ์ด๋ ต๋ค.
๊ณ ์ฐจ ํจ์๋ฅผ ์ฌ์ฉํ๋ ์ฝ๋๋ฅผ ๋๋ฒ๊น ํ๋ ๊ฒ์ ์ค์ฒฉ๋ ํจ์ ํธ์ถ๊ณผ ๋ณต์กํ ์ ์ด ํ๋ฆ์ผ๋ก ์ธํด ๊น๋ค๋ก์ธ ์ ์๋ค.
Combinator Pattern
๊ฐ๋จํ ํจ์๋ค์ ๊ฒฐํฉํด ๋ณต์กํ ๊ธฐ๋ฅ์ ๋ง๋๋ ํจ์ํ ํ๋ก๊ทธ๋๋ฐ ๋์์ธ ํจํด.
Combinator
์ธ์๋ก ํจ์๋ฅผ ๋ฐ์์ ์๋ก์ด ํจ์๋ฅผ ๋ง๋๋ ํจ์.
Combinator Pattern์ ์ฅ์
1. ์ฌ์ฌ์ฉ์ฑ
ํจ์๋ฅผ ์ฌ๋ฌ ๊ณณ์์ ์กฐํฉํด ๋ค์ํ ๊ฒฐ๊ณผ๋ฅผ ๋ง๋ค ์ ์๋ค.
2. ์ ์ฐ์ฑ
์ฝ๋์ ๋์์ ํจ์ ๊ฒฐํฉ์ผ๋ก ๋์ ์ผ๋ก ๋ณ๊ฒฝํ ์ ์๋ค.
์์
1. ์ฝํ๋ฆฐ์์ ์ ๊ณตํ๋ fold ํจ์
fun <T, R> Collection<T>.fold(
initial: R,
combine: (acc: R, nextElement: T) -> R
): R {
var accumulator: R = initial
for (element: T in this) {
accumulator = combine(accumulator, element)
}
return accumulator
}
fold ํจ์์์, combine ๋งค๊ฐ๋ณ์๋ ํจ์ ํ์ (R, T) -> R์ ๊ฐ์ง๊ณ , R ๋ฐ T ํ์ ์ ๋ ์ธ์๋ฅผ ๋ฐ์ R ์ ํ์ ๊ฐ์ ๋ฐํํ๋ ํจ์์ด๋ค. ์ด๋ Collection์ for ๋ฃจํ ๋ด๋ถ์์ ํธ์ถ๋๊ณ ๋ฐํ ๊ฐ์ accumulator์ ํ ๋น๋ฉ๋๋ค.
2. ๋ค๋ฅธ ์์
fun <T, R, V> ((T) -> R).andThen(next: (R) -> V): (T) -> V = {
next(this(it))
}
val multiplyBy2: (Int) -> Int = { it * 2 }
val add10: (Int) -> Int = { it + 10 }
val combinedFunction = multiplyBy2.andThen(add10)
println(combinedFunction(5)) // ์ถ๋ ฅ: 20 (5 * 2 + 10)
andThen ํจ์๋ ๋ ํจ์๋ฅผ ์กฐํฉํด ์๋ก์ด ํจ์๋ฅผ ๋ฐํํ๋ค. multiplyBy2 ํจ์์ add10 ํจ์๋ฅผ ๊ฒฐํฉํด, ์ซ์๋ฅผ 2๋ฐฐ๋ก ๊ณฑํ ํ 10์ ๋ํ๋ ํจ์๊ฐ ๋ง๋ค์ด์ก๋ค.
'๋น ๊ตฌ๋ฉ ์ฑ์ฐ๊ธฐ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Kotlin][Java] Kotlin ๋ฌธ์์ด ์ฐ๊ฒฐ์ด ์๋ฐ๋ก ์ปดํ์ผ ์์ ๋์ (3) | 2024.10.29 |
---|---|
[Kotlin][Java] Java์ Stream์ด ์์์๋ Kotlin์ Sequence๋ ์ ๋ง๋ค์์๊น (1) | 2024.10.17 |
[Kotlin][ํจ์ํ ํ๋ก๊ทธ๋๋ฐ] ๋ฉค๋ฒ ์ฐธ์กฐ (0) | 2024.10.17 |
[ํจ์ํ ํ๋ก๊ทธ๋๋ฐ] ์ํ ๋ณํ eta conversion (1) | 2024.10.16 |
[Java][Kotlin] ๊ฐ์์ฑ ๋ณ๊ฒฝ์ ์ฐจ์ด ๋น๊ต (0) | 2024.10.15 |