Created
September 20, 2019 18:56
-
-
Save llopez/f41f444c85316db14b71fcbf978d2f66 to your computer and use it in GitHub Desktop.
Playing with the idea of pipelining functions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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