Created
June 29, 2021 15:45
-
-
Save JoshCheek/5a55cfc61ea57f9157280f6e053b7b5b to your computer and use it in GitHub Desktop.
Ruby pipeline, based on JS example
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
# https://babeljs.io/blog/2018/07/19/whats-happening-with-the-pipeline-proposal | |
module Pipeable | |
def |(fn) | |
fn.to_proc.call self | |
end | |
refine(Enumerable) { include Pipeable } | |
refine(Proc) { include Pipeable } | |
refine(Array) { include Pipeable } | |
refine Kernel do | |
def array_map(&b) | |
-> arr { arr.map &b } | |
end | |
def array_filter(&b) | |
-> arr { arr.select &b } | |
end | |
def count | |
:count.to_proc | |
end | |
end | |
end | |
using Pipeable | |
(1..10) | # => 1..10 | |
array_map { |x| x * x } | # => [1, 4, 9, 16, 25, 36, 49, 64, 81, 100] | |
array_filter(&:even?) | # => [4, 16, 36, 64, 100] | |
:sum # => 220 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment