Elixir/Unix style pipe operations in Ruby
This proof of concept demonstrates a concise/elegant syntax for pipelining and method composition!
-9.pipe { abs | Math.sqrt | to_i } #=> 3
[9, 64].map(&Math.pipe.sqrt) #=> [3.0, 8.0]
[9, 64].map(&Math.pipe.sqrt.to_i.to_s) #=> ["3", "8"]
"https://api.github.com/repos/ruby/ruby".pipe do
URI.parse
Net::HTTP.get
JSON.parse.fetch("stargazers_count")
yield_self { |n| "Ruby has #{n} stars" }
Kernel.puts
end
#=> Ruby has 15120 stars
There’s been some recent discussions in the Ruby tracker related to method referencing and composition. The pipe_operator gem was created to propose an alternative syntax for this kind of behavior:
- #6284 - Add composition for procs
- #13581 - Syntax sugar for method reference
- #12125 - Shorthand operator for Object#method
Is this something that you’d like to see in Ruby core? Please share your opinions!
Post a comment