0Pricing
Scala for Backend Engineering & Functional Programming · 강의

커링

부분 적용을 알아봅니다

커링은(는) CoddyKit의 무료 Scala for Backend Engineering & Functional Programming 강의입니다. 이것은 4개 중 2번째 강의입니다. 아래에서 전체 강의를 무료로 읽을 수 있으며, 내장 코드 에디터와 24/7 AI 튜터와 함께 브라우저에서 직접 실습할 수 있습니다. 이 강의는 Scala for Backend Engineering & Functional Programming 학습 경로의 일부이며, 진행 상황이 웹과 CoddyKit 앱에 동기화됩니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

이 강의의 일부는 아직 번역되지 않았으며 영어로 표시됩니다.

What is currying?

Currying transforms a function of several arguments into a chain of functions, each taking one argument. In Scala this is written with multiple parameter lists.

object Main {
  def add(a: Int)(b: Int): Int = a + b
  def main(args: Array[String]): Unit = {
    println(add(3)(4))
  }
}

Multiple parameter lists

A curried method has several parameter lists: def f(a: Int)(b: Int). You call it by supplying each list in turn: f(1)(2).

object Main {
  def combine(prefix: String)(value: Int): String = s"$prefix$value"
  def main(args: Array[String]): Unit = {
    println(combine("id-")(42))
  }
}

Partial application

You can apply only some argument lists and get back a function awaiting the rest. This is called partial application.

object Main {
  def add(a: Int)(b: Int): Int = a + b
  def main(args: Array[String]): Unit = {
    val addFive = add(5) _
    println(addFive(10))
    println(addFive(20))
  }
}

Specializing a general function

Partial application lets you build specialized functions from a general one, reducing repetition.

object Main {
  def power(exp: Int)(base: Int): Int = math.pow(base, exp).toInt
  def main(args: Array[String]): Unit = {
    val square = power(2) _
    val cube = power(3) _
    println(square(5))
    println(cube(2))
  }
}

Currying a normal function

An ordinary function value has a .curried method that converts it into curried form.

object Main {
  def main(args: Array[String]): Unit = {
    val add = (a: Int, b: Int) => a + b
    val curriedAdd = add.curried
    println(curriedAdd(3)(4))
  }
}

Uncurrying back

The Function.uncurried helper reverses the process, turning a curried function back into one with a single parameter list.

object Main {
  def main(args: Array[String]): Unit = {
    val curried: Int => Int => Int = a => b => a + b
    val normal = Function.uncurried(curried)
    println(normal(3, 4))
  }
}

Why currying is useful

Currying enables reuse: fix the first arguments once, then call the result many times with different remaining arguments. It also helps the compiler infer types in later lists.

object Main {
  def discount(rate: Double)(price: Double): Double = price * (1 - rate)
  def main(args: Array[String]): Unit = {
    val blackFriday = discount(0.3) _
    println(blackFriday(100))
    println(blackFriday(250))
  }
}

Type inference in the last list

A practical reason for multiple parameter lists: the compiler can infer the lambda's parameter types from earlier lists, so you can omit them.

object Main {
  def fold[A](xs: List[A])(init: A)(op: (A, A) => A): A =
    xs.foldLeft(init)(op)
  def main(args: Array[String]): Unit = {
    val total = fold(List(1, 2, 3))(0)((a, b) => a + b)
    println(total)
  }
}

Currying with three lists

You can have as many parameter lists as you like. Each call supplies one list and returns a function for the next.

object Main {
  def make(a: Int)(b: Int)(c: Int): Int = a + b + c
  def main(args: Array[String]): Unit = {
    println(make(1)(2)(3))
    val step = make(10) _
    println(step(20)(30))
  }
}

Building configurable validators

A common real use: curry a validation rule, fix the threshold, and reuse the resulting predicate across a dataset.

object Main {
  def atLeast(min: Int)(value: Int): Boolean = value >= min
  def main(args: Array[String]): Unit = {
    val adult = atLeast(18) _
    println(List(15, 21, 18, 12).map(adult))
  }
}

Currying vs partial application

Currying is the structural transformation into one-argument functions. Partial application is the act of supplying some arguments now and the rest later. Curried functions make partial application natural.

object Main {
  def greet(greeting: String)(name: String): String = s"$greeting, $name!"
  def main(args: Array[String]): Unit = {
    val sayHi = greet("Hi") _
    println(sayHi("Ann"))
    println(sayHi("Bob"))
  }
}

Quick Check

How do you define a curried method in Scala?

Recap

You learned currying and partial application:

  • Curried methods use multiple parameter lists: f(a)(b)
  • Partially apply with f(x) _ to get a new function
  • .curried and Function.uncurried convert forms
  • Helps reuse, specialization, and type inference

자주 묻는 질문

“커링” 강의는 무료인가요?

네 — “커링” 전체 내용을 이 웹사이트에서 무료로 읽을 수 있습니다. 인터랙티브하게 실습하려면(내장 코드 에디터와 24/7 AI 튜터), CoddyKit PRO로 업그레이드하면 Scala for Backend Engineering & Functional Programming 강의 전체를 잠금 해제할 수 있습니다. Scala for Backend Engineering & Functional Programming 강의에는 총 4개의 강의가 포함되어 있습니다.

“커링”에서 뭘 배우나요?

부분 적용을 알아봅니다 브라우저에서 직접 실행하는 실습 코드로 Scala for Backend Engineering & Functional Programming을(를) 배우며, 24/7 AI 튜터가 강의를 진행하면서 질문에 답변해줍니다.

Scala for Backend Engineering & Functional Programming을(를) 시작하는 데 경험이 필요한가요?

사전 경험은 필요하지 않습니다. CoddyKit의 Scala for Backend Engineering & Functional Programming은(는) 초급자부터 고급 학습자까지를 위해 구성되어 있으므로, 여기서 시작하거나 처음부터 시작할 수 있으며 자신의 속도대로 진행할 수 있습니다. 이것은 4개 중 2번째 강의입니다.

“커링” 강의는 얼마나 걸리나요?

대부분의 CoddyKit 강의는 약 5~10분이 소요됩니다. 각 강의는 간결하고 인터랙티브하여 꾸준한 진행이 가능하며, 웹과 앱에서 중단한 부분부터 바로 시작할 수 있습니다.

이 Scala for Backend Engineering & Functional Programming 강의에서 코드를 작성하고 실행할 수 있나요?

네. 모든 Scala for Backend Engineering & Functional Programming 강의에는 내장 코드 에디터가 포함되어 있으므로, 브라우저에서 바로 실제 코드를 작성하고 실행한 후 즉시 AI 피드백을 받을 수 있습니다 — 로컬 설정이 필요 없습니다.

이 강의의 모든 강의

  1. 값으로서의 함수
  2. 커링
  3. 함수 조합
  4. 클로저
← Scala for Backend Engineering & Functional Programming(으)로 돌아가기