Skip to content

Instantly share code, notes, and snippets.

@llopez
Created September 20, 2019 18:56
Show Gist options
  • Save llopez/f41f444c85316db14b71fcbf978d2f66 to your computer and use it in GitHub Desktop.
Save llopez/f41f444c85316db14b71fcbf978d2f66 to your computer and use it in GitHub Desktop.
Playing with the idea of pipelining functions
class Pipe
attr_reader :value
def initialize(value)
@value = value
end
def pipe(func)
Pipe.new(func.call(@value))
end
alias >> pipe
end
r = Pipe.new(10)
.pipe(-> (x) {x * 2})
.pipe(-> (x) {x + 1})
.value
puts r
r = Pipe.new([1, 2, 3])
.pipe(-> (x) {
x.map{|x| x + 1}
})
.pipe(-> (x) {
x.select { |x| x > 2 }
})
.value
puts r
r = Pipe.new("hello world!!!") >>
-> (x) { x.upcase } >>
-> (x) { x + " yeah" } >>
-> (x) { x.length }
puts r.value
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment