Skip to content

Instantly share code, notes, and snippets.

@franckverrot
Last active September 23, 2015 11:21
Show Gist options
  • Save franckverrot/6a477a0864aed0392aa4 to your computer and use it in GitHub Desktop.
Save franckverrot/6a477a0864aed0392aa4 to your computer and use it in GitHub Desktop.
Pipeline operator in Ruby
# mul_x_y :: [Fixnum, Fixnum] -> Fixnum
mul_x_y = ->(x, y) { x * y }
# adder :: Fixnum -> (Fixnum -> Fixnum)
adder = ->(base) {
->(input) { input + base }
}
# add_two :: Fixnum -> Fixnum
add_two = adder[2]
# mul_add :: [Fixnum, Fixnum] -> String
mul_add = (mul_x_y |> add_two)
p mul_x_y[2, 3] # => 6
p add_two[2] # => 4
p mul_add[2, 3] # => 8
p (mul_add.
|> adder[2].
|> (->(x) { [x-1, x+1] })
)[2, 3] # => [9, 11]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment