Created
September 13, 2013 22:45
-
-
Save ruliana/6557043 to your computer and use it in GitHub Desktop.
Testing pipes in Ruby :)
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 PipeSource | |
def initialize(stream) | |
@stream = stream | |
@head_lambda = ->(it) { it } | |
end | |
def |(lambd) | |
temp = @head_lambda | |
@head_lambda = ->(it) do | |
lambd.call(temp.call(it)) | |
end | |
self | |
end | |
def !@ | |
call | |
end | |
def call | |
@stream.each { |it| @head_lambda.call(it) } | |
end | |
end | |
class Array | |
def |(lambd) | |
PipeSource.new(self) | lambd | |
end | |
end | |
class Proc | |
def |(lambd) | |
->(*args) { lambd.call(*self.call(*args)) } | |
end | |
end | |
_upcase = ->(it) { it.upcase } | |
_print = ->(it) { p it; it } | |
_subtract = ->(start, it) { it[start..-1] } | |
x = ['abc', 'def'] | _upcase | _print | |
x.call | |
y = _upcase | _print | _subtract.curry[2] | _print | |
z = ['xyz', 'htc'] | y | |
!z |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment