0
0
Rubyprogramming~10 mins

Curry and partial application in Ruby - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Curry and partial application
Define multi-arg function
Apply curry method
Get curried function
Call with one argument
Return new function waiting for next arg
Call with next argument
Execute original function with all args
Result
Currying transforms a function so you can call it with fewer arguments, returning a new function waiting for the rest.
Execution Sample
Ruby
def add(a, b)
  a + b
end
curried_add = method(:add).curry
add_five = curried_add.call(5)
result = add_five.call(3)
This code creates a curried version of add, then partially applies 5, and finally adds 3 to get 8.
Execution Table
StepActionFunction StateArguments GivenResult / Next Function
1Define add methodadd(a,b)nonemethod(:add) created
2Apply currycurried_addnonecurried function expecting 2 args
3Call curried_add with 5waiting for 1 more arg5function waiting for 1 more arg
4Call returned function with 3waiting for 1 arg3executes add(5,3) = 8
5Result storednonenone8
💡 All arguments provided, original function executed and returned 8
Variable Tracker
VariableStartAfter Step 3After Step 4Final
curried_addcurried functioncurried functioncurried functioncurried function
add_fiveundefinedfunction waiting for 1 argfunction waiting for 1 argfunction waiting for 1 arg
resultundefinedundefined88
Key Moments - 2 Insights
Why does calling curried_add.call(5) not immediately give the sum?
Because currying returns a new function waiting for the remaining arguments, as shown in execution_table step 3.
What happens if you call the curried function with all arguments at once?
It executes immediately and returns the result, unlike partial calls that return new functions (see step 4).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what does the function returned by curried_add.call(5) expect?
ANo more arguments, it returns the result immediately
BOne more argument to complete the call
CTwo more arguments to complete the call
DIt expects a block instead of arguments
💡 Hint
Check the 'Arguments Given' and 'Result / Next Function' columns at step 3
At which step does the original add method actually execute?
AStep 2
BStep 3
CStep 4
DStep 5
💡 Hint
Look for when the result 8 appears in the 'Result / Next Function' column
If you changed add_five = curried_add.call(5) to curried_add.call(5, 3), what would happen?
Aadd_five immediately becomes 8
Badd_five becomes a function waiting for one argument
CAn error occurs because too many arguments are given
Dadd_five becomes a function waiting for two arguments
💡 Hint
Recall that calling curried function with all arguments executes immediately (see key_moments answer 2)
Concept Snapshot
Currying in Ruby transforms a multi-argument method into a chain of single-argument functions.
Use method(:name).curry to get a curried function.
Calling it with fewer args returns a new function waiting for the rest.
Calling with all args executes the original method.
Partial application means calling with some args to get a new function.
Example: add_five = curried_add.call(5) creates a function adding 5.
Full Transcript
Currying changes a method so you can give arguments one at a time. First, you define a method with multiple arguments. Then you get a curried version using method(:name).curry. Calling this curried function with one argument returns a new function waiting for the next argument. When all arguments are given, the original method runs and returns the result. Partial application means giving some arguments early to get a new function that remembers those arguments. For example, calling curried_add.call(5) returns a function that adds 5 to its input. Calling that function with 3 returns 8. This step-by-step process helps break down complex calls into simpler parts.