Last active
June 12, 2018 17:08
-
-
Save pcreux/2f87847e5e4aad37db02 to your computer and use it in GitHub Desktop.
Revisions
-
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 7 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -65,10 +65,10 @@ def pipe(input, *pipeline) Mooify = ->(string) { "Cow said: " + string } Say = ->(string) { system %|say "#{string}"|; string } TweetTo = Struct.new(:recipient) do def call(input) puts %|Tweeting "#{input}" to #{@recipient}!| input end end @@ -97,10 +97,13 @@ def call(string) # => Cow said: maaw # ZOMG! Multiplexing! # "moo" => Mooify => :downcase => Reverse # => :upcase => Reverse p Pipeline.new(Mooify, [:downcase, :upcase], Reverse).call("moo") # => ["oom :dias woc", "OOM :DIAS WOC"] # Multi-Multiplexing... let me tell you... p Pipeline.new(Mooify, [:downcase, :upcase], Reverse, [:reverse, Leet]).call("moo") # => [["cow said: moo", "00m :d145 w0c"], ["COW SAID: MOO", "OOM :DIAS WOC"]] -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -101,6 +101,6 @@ def call(string) # => ["oom :dias woc", "OOM :DIAS WOC"] # Multi-Multiplexing... let me tell you... p Pipeline.new(Mooify, [:downcase, :upcase], Reverse, [:reverse, Leet]).call("moo") # => [["cow said: moo", "00m :d145 w0c"], ["COW SAID: MOO", "OOM :DIAS WOC"]] -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 9 additions and 8 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,23 +11,24 @@ # Ugly implementation starts here... def pipe_it(input, filter) # multiplexed input! if input.is_a? Array return input.map { |input_item| pipe_it(input_item, filter) } end case filter when Symbol input.send(filter) when Hash method = filter.keys.first arguments = Array(filter.values.first) input.send(method, *arguments) when Array # multiplex! filter.map { |filter_item| pipe_it(input, filter_item) } else filter.call(input) end end -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,6 @@ # Elixir has pipes `|>`. Let's try to implement those in Ruby. # # I want to write this: # # email.body | RemoveSignature | HighlightMentions | :html_safe # -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 5 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -98,3 +98,8 @@ def call(string) # ZOMG! Multiplexing! p Pipeline.new(Mooify, [:downcase, :upcase], Reverse).call("moo") # => ["oom :dias woc", "OOM :DIAS WOC"] # Multi-Multiplexing... let me tell you... p Pipeline.new(Mooify, [:downcase, :upcase], Reverse, [:reverse, :capitalize]).call("moo") # => [["cow said: moo", "Oom :dias woc"], ["COW SAID: MOO", "Oom :dias woc"]] -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 11 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -12,13 +12,20 @@ # Ugly implementation starts here... def pipe_it(input, other) if input.is_a? Array return input.map { |element| pipe_it(element, other) } end case other when Symbol input.send(other) when Hash method = other.keys.first arguments = Array(other.values.first) input.send(method, *arguments) when Array # multiplex! other.map { |element| pipe_it(input, element) } else other.call(input) end @@ -87,3 +94,7 @@ def call(string) puts pipeline.call("moo") # => Cow said: maaw # ZOMG! Multiplexing! p Pipeline.new(Mooify, [:downcase, :upcase], Reverse).call("moo") # => ["oom :dias woc", "OOM :DIAS WOC"] -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 40 additions and 20 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -11,31 +11,43 @@ # Ugly implementation starts here... def pipe_it(input, other) case other when Symbol input.send(other) when Hash method = other.keys.first arguments = Array(other.values.first) input.send(method, *arguments) else other.call(input) end end class Pipeline def initialize(*filters) @filters = filters end attr_accessor :filters def call(input) filters.inject(input) do |input, filter| pipe_it(input, filter) end end end def pipable(input) input.define_singleton_method(:|) do |filter| pipable pipe_it(input, filter) end input end def pipe(input, *pipeline) Pipeline.new(*pipeline).call(input) end # Let's define a few filters @@ -45,25 +57,33 @@ def pipable(obj) Mooify = ->(string) { "Cow said: " + string } Say = ->(string) { system %|say "#{string}"|; string } class TweetTo < Struct.new(:recipient) def call(string) puts %|Tweeting "#{string}" to #{@recipient}!| string end end # Time to play with different approaches... # 1 - We make the first element pipable and we can then just pipe through! result = pipable("moo") | Reverse | Leet | Mooify | :downcase | TweetTo.new('@pcreux') | { delete: 'o' } puts result # => cw said: 00m # 2 - Pipe without defining any `|` method puts pipe("moo", Mooify, :upcase) # => COW SAID: MOO # 3 - Pipeline object pipeline = Pipeline.new(Mooify, :downcase, { gsub: ["o", "a"] }) pipeline.filters << ->(input) { input.gsub("moo", "maow") } puts pipeline.call("moo") # => caw said: maa pipeline.filters.reverse! puts pipeline.call("moo") # => Cow said: maaw -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 11 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,12 @@ # Elixir as pipes `|>`. Let's try to implement those in Ruby. # # I want to write something like this: # # email.body | RemoveSignature | HighlightMentions | :html_safe # # instead of: # # HighlightMentions.call(RemoveSignature.call(email.body)).html_safe # # Ugly implementation starts here... @@ -50,10 +58,12 @@ def call(string) # Time to play with it! # We make the first element pipable and we can then just pipe through! result = pipable("moo") | Reverse | Leet | Mooify | :downcase | TweetTo('@pcreux') | { delete: 'o' } puts result # => cw said: 00m # No syntactic sugar here so that we don't define the singleton method `|` puts Pipe("moo", Mooify, :upcase) # => COW SAID: MOO -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -30,7 +30,7 @@ def pipable(obj) obj end # Let's define a few filters Reverse = ->(string) { string.reverse } Leet = ->(string) { string.gsub(/[aeiost]/,'a'=>'4','e'=>'3','i'=>'1','o'=>'0','s'=>'5','t'=>'7') } -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 6 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,6 +1,8 @@ # Let's try to implement sexy pipes as they have in Elixir # # Ugly implementation starts here... def pipe_it(obj, other) case other when Symbol @@ -28,6 +30,8 @@ def pipable(obj) obj end # Let's define some transformations Reverse = ->(string) { string.reverse } Leet = ->(string) { string.gsub(/[aeiost]/,'a'=>'4','e'=>'3','i'=>'1','o'=>'0','s'=>'5','t'=>'7') } Mooify = ->(string) { "Cow said: " + string } @@ -44,6 +48,8 @@ def call(string) end end # Time to play with it! result = pipable("moo") | Reverse | Leet | Mooify | :downcase | TweetTo('@pcreux') | { delete: 'o' } puts result # => cw said: 00m -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 31 additions and 14 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,22 +1,28 @@ # Let's try to implement sexy pipes as they have in Elixir # def pipe_it(obj, other) case other when Symbol obj.send(other) when Hash method = other.keys.first arguments = Array(other.values.first) obj.send(method, *arguments) else other.call(obj) end end def Pipe(*args) args.inject do |obj, other| pipe_it(obj, other) end end def pipable(obj) obj.define_singleton_method(:|) do |other| pipable pipe_it(obj, other) end obj @@ -27,10 +33,21 @@ def pipable(obj) Mooify = ->(string) { "Cow said: " + string } Say = ->(string) { system %|say "#{string}"|; string } def TweetTo(recipient) TweetTo.new(recipient) end class TweetTo < Struct.new(:recipient) def call(string) puts %|Tweeting "#{string}" to #{@recipient}!| string end end result = pipable("moo") | Reverse | Leet | Mooify | :downcase | TweetTo('@pcreux') | { delete: 'o' } puts result # => cw said: 00m puts Pipe("moo", Mooify, :upcase) # => COW SAID: MOO -
pcreux revised this gist
Sep 24, 2014 . 1 changed file with 27 additions and 7 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,16 +1,36 @@ # Let's try to implement sexy pipes as they have in Elixir # def Pipe(*args) args.inject do |obj, other| if other.is_a? Symbol pipable obj.send(other) else pipable other.call(obj) end end end def pipable(obj) obj.define_singleton_method(:|) do |other| if other.is_a? Symbol pipable obj.send(other) else pipable other.call(self) end end obj end Reverse = ->(string) { string.reverse } Leet = ->(string) { string.gsub(/[aeiost]/,'a'=>'4','e'=>'3','i'=>'1','o'=>'0','s'=>'5','t'=>'7') } Mooify = ->(string) { "Cow said: " + string } Say = ->(string) { system %|say "#{string}"|; string } result = pipable("moo") | Reverse | Leet | Mooify | :downcase puts result # => cow said: 00m puts Pipe("moo", Mooify, Say, :downcase) # => cow said: moo -
pcreux revised this gist
Sep 11, 2014 . 1 changed file with 8 additions and 10 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,18 +1,16 @@ def Pipe(obj) obj.define_singleton_method(:|) do |other| Pipe other.call(self) end obj end Reverse = ->(string) { string.reverse } Capitalize = ->(string) { string.capitalize } Funnify = ->(string) { "ZOMG " + string } result = Pipe("moo") | Reverse | Capitalize | Funnify puts result # => ZOMG Oom -
pcreux created this gist
Sep 11, 2014 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ module Pipeable def |(other) other.call(self) end end class String include Pipeable end Reverse = ->(string) { string.reverse } Capitalize = ->(string) { string.capitalize } result = "moo" | Reverse | Capitalize | Reverse puts result # => moO