What if you could create many custom methods from one, without rewriting code every time?
Why Curry and partial application in Ruby? - Purpose & Use Cases
Imagine you have a method that adds three numbers, and you need to use it many times with some numbers fixed but others changing.
Without special tools, you write many similar methods or repeat code to handle each case.
This manual way is slow and boring because you rewrite similar code again and again.
It's easy to make mistakes and hard to change later.
Curry and partial application let you fix some inputs of a method and create new simpler methods automatically.
This saves time, reduces errors, and makes your code cleaner and easier to understand.
def add(a, b, c) a + b + c end add(1, 2, 3) add(1, 2, 4)
add = ->(a, b, c) { a + b + c }
add_curried = add.curry
add_one_two = add_curried.call(1).call(2)
add_one_two.call(3)
add_one_two.call(4)You can create many customized versions of a method quickly, making your programs flexible and easy to maintain.
Think of a coffee shop where you always order a coffee with milk and sugar, but sometimes change the size.
Partial application lets you fix milk and sugar once, then quickly choose the size each time.
Manual repetition is slow and error-prone.
Curry and partial application fix some inputs to create new simpler methods.
This makes code cleaner, faster to write, and easier to change.
Practice
Solution
Step 1: Understand currying concept
Currying transforms a function so you can call it with fewer arguments than it needs.Step 2: Result of currying
When called with fewer arguments, it returns a new function waiting for the rest.Final Answer:
Call the function with fewer arguments and get a new function -> Option AQuick Check:
Currying = call fewer args, get new function [OK]
- Thinking currying speeds up code
- Confusing currying with memoization
- Believing currying changes function to class method
Solution
Step 1: Recall Ruby syntax for currying
In Ruby, you call the .curry method on a lambda or Proc object.Step 2: Identify correct usage
Callingmy_lambda.curryreturns a curried version of the lambda.Final Answer:
my_lambda.curry -> Option DQuick Check:
Use .curry method on lambda object [OK]
- Trying to call curry as a standalone function
- Using .partial instead of .curry
- Passing lambda as argument to curry method
add = ->(x, y) { x + y }
curried_add = add.curry
add_five = curried_add.call(5)
puts add_five.call(3)Solution
Step 1: Understand currying and calls
The lambda add takes two arguments and returns their sum. Currying allows calling with one argument to get a new function.Step 2: Trace the calls
curried_add.call(5) returns a new function that adds 5 to its argument. Calling add_five.call(3) returns 5 + 3 = 8.Final Answer:
8 -> Option CQuick Check:
Curried add with 5 then 3 = 8 [OK]
- Expecting add_five to be a number, not a function
- Confusing call with direct invocation
- Thinking currying changes output type
multiply = ->(x, y) { x * y }
curried_multiply = multiply.curry
result = curried_multiply(2)(3)
puts resultSolution
Step 1: Check syntax for currying and calling
In Ruby, lambdas can be curried and called using parentheses like functions.Step 2: Verify code behavior
curried_multiply(2) returns a function that multiplies by 2; calling (3) returns 6. The code runs without error.Final Answer:
No error, code runs correctly and prints 6 -> Option BQuick Check:
Currying with lambdas and calls works as shown [OK]
- Thinking .call is required for every call
- Believing curry is not available for lambdas
- Assuming lambdas must be defined with def
concat = ->(a, b, c) { "#{a}-#{b}-#{c}" }
curried_concat = concat.curry
partial = curried_concat.call('X').call('Y')
puts partial.call('Z')
What does this demonstrate about currying and partial application?Solution
Step 1: Analyze the currying calls
The original lambda takes 3 arguments and joins them with dashes. Currying allows calling with one argument at a time.Step 2: Understand partial application here
Calling curried_concat.call('X') fixes the first argument. Then .call('Y') fixes the second. The resulting function waits for the third argument.Step 3: Output and meaning
Calling partial.call('Z') returns "X-Y-Z". This shows partial application by fixing some arguments step-by-step.Final Answer:
You can fix some arguments step-by-step, creating simpler functions -> Option AQuick Check:
Currying enables partial application stepwise [OK]
- Thinking all args must be given at once
- Confusing currying with separate partial method
- Assuming partial calls cause errors
Ready to prove it?
Pass the test and this pattern moves unknown
