Junior — Middle
Can you explain the concept of currying in the context of programming?
sobes.tech AI
Answer from AI
Currying is a technique in functional programming where a function with multiple arguments is transformed into a sequence of functions, each taking a single argument.
In simpler terms, instead of calling a function with multiple parameters at once, you call a function that returns another function expecting the next parameter, and so on until all arguments are provided.
Example in Python:
def multiply(x):
def multiply_by(y):
return x * y
return multiply_by
mul_by_3 = multiply(3) # returns a function that multiplies by 3
result = mul_by_3(5) # 15
print(result)
Currying is useful for creating partially applied functions and increasing code flexibility.