computation as a resolution of mathematical functions • Avoids changes of state and data mutability • Has its origins from lambda calculus, in 1930 • LISP, 1950s • Clojure, Erlang, Elixir…. And now Ruby ❤
functions if it is possible to use functions as the same way that we use primitive types. • It means that is possible send a function as an argument, return a function, etc.
f.call(1, 2) # ArgumentError (wrong number of arguments (given 2, expected 1)) def foo f = Proc.new { return "return from proc" } f.call # control leaves foo here return "return from foo" end def bar f = lambda { return "return from lambda" } f.call # control does not leave bar here return "return from bar" end puts foo # => "return from proc" puts bar # => "return from bar"
translating the evaluation of a function that takes multiple arguments into evaluating a sequence of functions, each with a single argument. 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).(2) z = 3 # another expensive operation… puts partial_function.(z)
of some pattern case 0 in 0 | 1 true end # => true case [1, 2, 3, 4, 5] in a, *b b end # => [2, 3, 4, 5] case { a: 'ofoo', b: 'fddffd' } in a: a, **h h end # => {:b=>"fddffd"}