1 min read

TIL: Lambda Composition in Ruby

Joining lambdas together for fun and profit
TIL: Lambda Composition in Ruby
Photo by Toa Heftiba / Unsplash

Joining lambdas together for fun and profit.

Let's create two lambdas:

double = -> (n) { n + n }
square = -> (n) { n * n }

I can join them together to create something new:

double_and_square = double >> square
double_and_square.call(3)
# 36

You can compose right to left to (though I find this harder to read):

square_and_double = double << square
square_and_double.call(3)
# 18