-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcurrying.rb
More file actions
21 lines (14 loc) · 822 Bytes
/
currying.rb
File metadata and controls
21 lines (14 loc) · 822 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# currying is the technique of translating the evaluation of a function that takes multiple arguments
# into evaluating a sequence of functions, each with a single argument.
# It provides a way of automatically managing how arguments are passed to functions.
# For example, a function that takes two arguments, one from X and one from Y, and produces outputs in Z,
# by currying is translated into a function that takes a single argument from X and produces as outputs functions from Y to Z.
add = ->(x, y, z) { x + y + z}
puts add.curry.(1).(2).(3)
# Currying is related to, but not the same as, partial function.
add_curry = add.curry
partial_function = add_curry.(1)
puts partial_function.(2).(3)
# References:
# https://en.wikipedia.org/wiki/Currying
# https://gist.github.com/KamilLelonek/eda2a5476d6e8dc1c351