Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@havenwood
Last active August 15, 2018 17:30
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 havenwood/b6d55b412fbf583758c91b8ee339a822 to your computer and use it in GitHub Desktop.
Save havenwood/b6d55b412fbf583758c91b8ee339a822 to your computer and use it in GitHub Desktop.
A "fun" solution to this_dude's problem in the #ruby irc channel: http://www.codewars.com/kata/sum-of-digits-slash-digital-root/train/ruby
class Proc
def >> other
-> arg { other.to_proc.call call arg }
end
end
class Symbol
def >> other
to_proc >> other.to_proc
end
end
class Enumerator
class << self
def generate value
raise ArgumentError, 'No block given' unless block_given?
Enumerator.new Float::INFINITY do |yielder|
loop do
yielder << value
value = yield value
end
end
end
end
end
Enumerator.generate(942, &:digits >> :sum).find(&:digits >> :one?)
#=> 6
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment