0
0
Rubyprogramming~10 mins

Curry and partial application in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to create a curried function that adds two numbers.

Ruby
add = ->(x, y) { x + y }.[1]
add_two = add.call(2)
result = add_two.call(3)
puts result
Drag options to blanks, or click blank then click option'
Alambda
Bpartial
Cbind
Dcurry
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'partial' which is not a Ruby method for currying.
Trying to call 'bind' which is unrelated here.
2fill in blank
medium

Complete the code to partially apply the first argument of the curried function.

Ruby
multiply = ->(x, y) { x * y }.curry
double = multiply.[1](2)
puts double.call(5)
Drag options to blanks, or click blank then click option'
Acall
Bcall_partial
Ccall_proc
Attempts:
3 left
💡 Hint
Common Mistakes
Using non-existent methods like 'call_partial' or 'call_proc'.
Confusing method names for calling lambdas.
3fill in blank
hard

Fix the error in the code to correctly curry the lambda.

Ruby
adder = lambda { |x, y| x + y }.[1]
puts adder.call(1).call(2)
Drag options to blanks, or click blank then click option'
Acurrying
Bcurry
Cpartial
Dcurry!
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'currying' which is not a method.
Using 'curry!' which does not exist.
4fill in blank
hard

Fill both blanks to create a curried function and partially apply the first argument.

Ruby
concat = ->(a, b) { a + b }.[1]
hello = concat.[2]('Hello, ')
puts hello.call('World!')
Drag options to blanks, or click blank then click option'
Acurry
Bcall
Dpartial
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'partial' instead of 'curry' for the first blank.
Using 'partial' instead of 'call' for the second blank.
5fill in blank
hard

Fill all three blanks to create a curried function, partially apply arguments, and call the final function.

Ruby
power = ->(base, exponent) { base ** exponent }.[1]
square = power.[2](2)
cube = power.[3](3)
puts square.call(4)
puts cube.call(3)
Drag options to blanks, or click blank then click option'
Acurry
Bcall
Dpartial
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'partial' instead of 'curry' for the first blank.
Using 'partial' instead of 'call' for the second and third blanks.