Science Knowings: JavaScript Course For Social Media

Currying vs Partial Application

Currying vs Partial Application

Last time, we explored side effects in JavaScript. Today, we're diving into currying and partial application, two powerful techniques for creating reusable and flexible functions.



Currying is a technique that transforms a function with multiple arguments into a series of functions with a single argument.

Currying in JavaScript

function add(a, b) { return a + b; }

We can curry this function using the following syntax:

const addCurried = a => b => a + b;

Benefits of Currying

  • Partially apply arguments
  • Create reusable functions
  • Improve code readability
  • Support functional programming patterns
  • Partial Application

    Partial Application

    Partial application is similar to currying, but it fixes some of the arguments while leaving others open.

    Partial Application in JavaScript

    const add10 = addCurried(10);

    This creates a new function that adds 10 to any number passed to it.

    Benefits of Partial Application

  • Simplify complex functions
  • Create specialized functions
  • Enhance code reusability
  • Support object-oriented programming principles
  • How to Decide When to Use Currying or Partial Application?

    Use currying when you want to create a series of functions with a single argument, while partial application is suitable when you want to fix some arguments and leave others open.


    Example of currying:

    const multiply = (a, b, c) => a * b * c;

    Curried version:

    const multiplyCurried = a => b => c => a * b * c;

    Code Examples

    Example of partial application:

    const add5 = addCurried(5);

    Now, add5 can be used as a simplified function for adding 5 to any number.

    When to use Currying vs Partial Application

    CurryingPartial Application
    Create a series of single-argument functionsFix some arguments of a function and leave others open
    Support functional programmingSimplify complex functions
    Enhance code reusabilitySupport object-oriented programming

    Advantages of Currying

  • Improved code readability
  • Enhanced composability
  • Support for memoization
  • Facilitation of testing
  • Advantages of Partial Application

  • Simplified codebase
  • Increased code reusability
  • Enhanced flexibility
  • Support for object-oriented design
  • Disadvantages of Currying

  • Potential performance overhead
  • Increased complexity for beginners
  • Less intuitive in some scenarios
  • Not always suitable for all use cases
  • Disadvantages of Partial Application

  • Code can become less readable
  • May lead to function bloat
  • Not as widely used as currying
  • Less efficient in some cases
  • Conclusion

    Currying and partial application are powerful techniques that can enhance your JavaScript codebase. Choose the right technique based on your specific requirements. Remember, practice makes perfect, so experiment with these concepts to master them.

    Next Topic: Monads

    In the next session, we'll dive into monads, a powerful concept in functional programming. Monads provide a structured way to manage complex computations, making your code more concise and easier to reason about. Follow us for the next exciting topic!