← Back to Scala Course | Chapter 9: Functional Programming | Lesson 6 of 8

Partial Application क्या है

Partial application किसी function के कुछ inputs को पहले से fix कर देता है और बाकी को बाद के लिए छोड़ देता है, जैसे एक form जिसमें कुछ blanks पहले से भरे हों। आपको वापस एक नया सरल function मिलता है।

In this page:

  1. Partial Application
Syntax
scala
def function_name(a: Type, b: Type): ReturnType = expression

val partial = function_name(value1, _: Type)
partial(value2)

Partial Application

Partial application किसी function के कुछ arguments को पहले से fix कर देता है, जिससे एक नया function बनता है जिसे सिर्फ बाकी arguments की ज़रूरत होती है, अनfixed positions के लिए _ placeholder का उपयोग करते हुए, जैसे add(5, _: Int)।

यह currying से काफ़ी मिलता-जुलता है लेकिन साधारण (non-curried) multi-parameter functions पर भी काम करता है। यह बिना नया code लिखे किसी general function को ज़्यादा specific function में बदलने का एक तरीका है।

Note: _ से partial application सिर्फ curried functions पर नहीं बल्कि कई parameter lists वाले regular multi-parameter functions पर भी काम करता है।

उदाहरण: Partial Application

markup
object Main extends App {
  def add(a: Int, b: Int): Int = a + b

  val addFive: Int => Int = add(5, _: Int)

  println(addFive(10))
  println(addFive(20))
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. add(5) लिखना और एक function की उम्मीद करना, जबकि partial application के लिए आपको add(5, _: Int) जैसा एक placeholder देना चाहिए।
  2. placeholder का type छोड़ देना, जिससे compiler parameter type infer नहीं कर पाता।
  3. partial application को currying के साथ confuse करना, जबकि currying के लिए method को कई parameter lists के साथ declare होना चाहिए।
🔒

Chapter Quiz — Complete all 8 topics to unlock

0/8 topics done

Complete these topics first:

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.