Currying is a technique where a method with multiple arguments is transformed into a series of methods each taking one argument. It allows you to call a method step-by-step with one argument at a time.
Click to reveal answer
beginner
What does partial application mean?
Partial application means fixing some arguments of a function and creating a new function that takes the remaining arguments. It helps reuse functions with some preset values.
Click to reveal answer
intermediate
How do you create a curried function in Ruby?
You can create a curried function by calling curry on a Proc or lambda. For example: add = ->(a, b) { a + b }.curry creates a curried version of the add function.
Click to reveal answer
intermediate
What is the difference between currying and partial application?
Currying transforms a function to take one argument at a time, returning new functions until all arguments are given. Partial application fixes some arguments and returns a function for the rest. Partial application is a special case of currying.
Click to reveal answer
beginner
Example: How to partially apply a function in Ruby using currying?
Given multiply = ->(a, b) { a * b }.curry, you can fix the first argument like double = multiply.call(2). Now double.call(5) returns 10.
Click to reveal answer
What does the curry method do in Ruby?
ADeletes a function
BTransforms a multi-argument function into a chain of single-argument functions
CConverts a function to a string
DRuns a function multiple times
✗ Incorrect
The curry method changes a function so it takes one argument at a time, returning new functions until all arguments are provided.
What is partial application useful for?
AMaking a function run faster
BDeleting arguments from a function
CChanging the function's name
DFixing some arguments of a function to reuse it easily
✗ Incorrect
Partial application lets you fix some arguments, creating a new function that needs fewer arguments.
Which Ruby object can be curried?
AProc or lambda
BInteger
CString
DArray
✗ Incorrect
Only Proc or lambda objects can be curried using the curry method.
What will add = ->(a, b) { a + b }.curry; add.call(2).call(3) return?
Anil
B23
C5
DError
✗ Incorrect
Currying allows calling the function one argument at a time. Here, 2 + 3 equals 5.
Partial application is a special case of which concept?
ACurrying
BRecursion
CInheritance
DPolymorphism
✗ Incorrect
Partial application fixes some arguments, and currying transforms functions to take one argument at a time. Partial application is considered a special case of currying.
Explain in your own words what currying is and how it works in Ruby.
Think about how a function with many arguments can be called step-by-step.
You got /3 concepts.
Describe partial application and how it differs from currying with an example in Ruby.
Focus on fixing some arguments vs. taking one argument at a time.
You got /3 concepts.
Practice
(1/5)
1. What does currying a function in Ruby allow you to do?
easy
A. Call the function with fewer arguments and get a new function
B. Run the function faster by optimizing code
C. Convert the function into a class method
D. Automatically memoize the function results
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 A
Quick Check:
Currying = call fewer args, get new function [OK]
Hint: Currying means breaking function calls into smaller steps [OK]
Common Mistakes:
Thinking currying speeds up code
Confusing currying with memoization
Believing currying changes function to class method
2. Which of the following is the correct way to curry a lambda in Ruby?
easy
A. lambda.curry(my_lambda)
B. curry(my_lambda)
C. my_lambda.partial
D. my_lambda.curry
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
Calling my_lambda.curry returns a curried version of the lambda.
Final Answer:
my_lambda.curry -> Option D
Quick Check:
Use .curry method on lambda object [OK]
Hint: Use .curry directly on lambda or Proc [OK]
Common Mistakes:
Trying to call curry as a standalone function
Using .partial instead of .curry
Passing lambda as argument to curry method
3. What is the output of this Ruby code?
add = ->(x, y) { x + y }
curried_add = add.curry
add_five = curried_add.call(5)
puts add_five.call(3)
medium
A. 5
B. 3
C. 8
D. Error
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 C
Quick Check:
Curried add with 5 then 3 = 8 [OK]
Hint: Currying splits args; add 5 + 3 = 8 [OK]
Common Mistakes:
Expecting add_five to be a number, not a function
Confusing call with direct invocation
Thinking currying changes output type
4. Identify the error in this Ruby code using currying:
multiply = ->(x, y) { x * y }
curried_multiply = multiply.curry
result = curried_multiply(2)(3)
puts result
medium
A. multiply should be defined with def, not lambda
B. No error, code runs correctly and prints 6
C. curry method does not exist for lambdas
D. curried_multiply(2)(3) should be curried_multiply.call(2).call(3)
Solution
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 B
Quick Check:
Currying with lambdas and calls works as shown [OK]
Hint: Parentheses work for calling curried lambdas [OK]