Last active
February 18, 2016 11:34
-
-
Save jumski/4a444ad8132838c6ac33 to your computer and use it in GitHub Desktop.
Alternative implementation of codequest-eu/codequest_pipes :-)
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 characters
class Trough < Array | |
alias_method :|, :push | |
def call(context) | |
reduce(context) do |ctx, pig| | |
pig.new(ctx).call | |
ctx | |
end | |
end | |
end | |
class Pig | |
def initialize(context) | |
@context = context | |
end | |
def self.|(other) | |
Trough.new.push(self).push(other) | |
end | |
private | |
attr_reader :context | |
end | |
def Closure(&block) | |
Class.new(Pig) { define_method(:call, &block) } | |
end |
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 characters
require './piglet.rb' | |
class Chrum < Pig | |
def call | |
puts 'chrum chrum' | |
context[:chrumed] = true | |
end | |
end | |
class Oink < Pig | |
def call | |
puts 'oink oink' | |
context[:oinked] = true | |
end | |
end | |
TROUGH = Chrum | | |
Oink | | |
Closure { | |
puts 'snort snort' | |
context[:snorted] = true | |
} | |
input_context = {} | |
puts input_context.inspect | |
output_context = TROUGH.call(input_context) | |
puts output_context.inspect |
marcinwyszynski
commented
Feb 18, 2016
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment