Skip to content

Instantly share code, notes, and snippets.

@ruliana
Created September 13, 2013 22:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ruliana/6557043 to your computer and use it in GitHub Desktop.
Save ruliana/6557043 to your computer and use it in GitHub Desktop.
Testing pipes in Ruby :)
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