Bird
Raised Fist0
Rubyprogramming~20 mins

Curry and partial application in Ruby - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Curried Ruby Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of curried function with partial application
What is the output of this Ruby code using currying and partial application?
Ruby
def multiply(a, b, c)
  a * b * c
end

curried_multiply = method(:multiply).curry
partial = curried_multiply.call(2)
result = partial.call(3).call(4)
puts result
ANoMethodError
B14
C24
DTypeError
Attempts:
2 left
💡 Hint
Remember that currying transforms a method into a chain of functions each taking one argument.
❓ Predict Output
intermediate
2:00remaining
Result of partial application with Proc
What will this Ruby code print when using partial application with a Proc?
Ruby
adder = ->(x, y, z) { x + y + z }
curried_adder = adder.curry
add_five = curried_adder.call(5)
result = add_five.call(10).call(15)
puts result
A30
B20
CArgumentError
DNoMethodError
Attempts:
2 left
💡 Hint
Currying a Proc works similarly to currying a method.
🔧 Debug
advanced
2:00remaining
Identify the error in currying usage
What error does this Ruby code raise when trying to curry a method incorrectly?
Ruby
def concat(a, b)
  a + b
end

curried = method(:concat).curry
result = curried.call('Hello').call
puts result
ASyntaxError
BNoMethodError: undefined method `call' for nil:NilClass
CTypeError: no implicit conversion of nil into String
DArgumentError: wrong number of arguments (given 0, expected 1)
Attempts:
2 left
💡 Hint
Check how many arguments the curried function expects at each call.
🧠 Conceptual
advanced
2:00remaining
Understanding partial application with default arguments
Given this Ruby method with default arguments, what is the output of the curried calls below?
Ruby
def greet(greeting, name = 'friend')
  "#{greeting}, #{name}!"
end

curried_greet = method(:greet).curry
say_hello = curried_greet.call('Hello')
puts say_hello.call
A"Hello, "
BArgumentError
C"Hello, friend!"
D"Hello, nil!"
Attempts:
2 left
💡 Hint
Currying does not automatically apply default arguments in Ruby methods.
🚀 Application
expert
3:00remaining
Create a curried function for filtering and transforming
You want to create a curried Ruby function that filters an array of numbers by a minimum value and then multiplies the filtered numbers by a factor. Which option correctly defines and uses this curried function to get [20, 30, 40, 60] from [5, 10, 15, 20, 30] with min 10 and factor 2?
A
filter_and_multiply = ->(min, factor, arr) { arr.select { |x| x >= min }.map { |x| x * factor } }.curry
result = filter_and_multiply.call(10).call(2).call([5,10,15,20,30])
puts result.inspect
B
filter_and_multiply = ->(arr, min, factor) { arr.select { |x| x >= min }.map { |x| x * factor } }.curry
result = filter_and_multiply.call([5,10,15,20,30]).call(10).call(2)
puts result.inspect
C
filter_and_multiply = ->(min, arr, factor) { arr.select { |x| x >= min }.map { |x| x * factor } }.curry
result = filter_and_multiply.call(10).call([5,10,15,20,30]).call(2)
puts result.inspect
D
filter_and_multiply = ->(min, factor, arr) { arr.map { |x| x * factor if x >= min } }.curry
result = filter_and_multiply.call(10).call(2).call([5,10,15,20,30])
puts result.inspect
Attempts:
2 left
💡 Hint
Remember the order of arguments matters for currying and that map with conditional returns nil for false conditions.

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

  1. Step 1: Understand currying concept

    Currying transforms a function so you can call it with fewer arguments than it needs.
  2. Step 2: Result of currying

    When called with fewer arguments, it returns a new function waiting for the rest.
  3. Final Answer:

    Call the function with fewer arguments and get a new function -> Option A
  4. 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

  1. Step 1: Recall Ruby syntax for currying

    In Ruby, you call the .curry method on a lambda or Proc object.
  2. Step 2: Identify correct usage

    Calling my_lambda.curry returns a curried version of the lambda.
  3. Final Answer:

    my_lambda.curry -> Option D
  4. 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

  1. 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.
  2. 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.
  3. Final Answer:

    8 -> Option C
  4. 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

  1. Step 1: Check syntax for currying and calling

    In Ruby, lambdas can be curried and called using parentheses like functions.
  2. Step 2: Verify code behavior

    curried_multiply(2) returns a function that multiplies by 2; calling (3) returns 6. The code runs without error.
  3. Final Answer:

    No error, code runs correctly and prints 6 -> Option B
  4. Quick Check:

    Currying with lambdas and calls works as shown [OK]
Hint: Parentheses work for calling curried lambdas [OK]
Common Mistakes:
  • Thinking .call is required for every call
  • Believing curry is not available for lambdas
  • Assuming lambdas must be defined with def
5. Given this code, what will be the output?
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?
hard
A. You can fix some arguments step-by-step, creating simpler functions
B. Currying only works if all arguments are given at once
C. Partial application requires a special method, not currying
D. Calling curried functions with fewer arguments causes errors

Solution

  1. 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.
  2. 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.
  3. Step 3: Output and meaning

    Calling partial.call('Z') returns "X-Y-Z". This shows partial application by fixing some arguments step-by-step.
  4. Final Answer:

    You can fix some arguments step-by-step, creating simpler functions -> Option A
  5. Quick Check:

    Currying enables partial application stepwise [OK]
Hint: Currying lets you fix args one by one [OK]
Common Mistakes:
  • Thinking all args must be given at once
  • Confusing currying with separate partial method
  • Assuming partial calls cause errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard