Skip to content

Instantly share code, notes, and snippets.

@kwstannard
Last active October 21, 2016 19:06
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 kwstannard/323c4c86774a66f580fd055fc118b4f3 to your computer and use it in GitHub Desktop.
Save kwstannard/323c4c86774a66f580fd055fc118b4f3 to your computer and use it in GitHub Desktop.
equivalent to elixer pipe in ruby
class Proc
def <<(*args)
args = args.flatten if args.count == 1
-> (*items) { self.call(*items, *args) }
end
end
adder = -> (*args) { args.inject(&:+) }
puts [1,2,3,4,5]
.map(&adder << 1)
.map(&adder << [10, 100]).inspect
class Method
def <<(*args)
args = args.flatten if args.count == 1
-> (*items) { self.call(*items, *args) }
end
end
class Mathx
def self.product(*args)
args.inject(&:*)
end
def self.division(*args)
args.inject(&:/)
end
end
puts [1,2,3,4,5]
.map(&Mathx.method(:product) << 4)
.map(&Mathx.method(:division) << 2).inspect
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment