Created
February 8, 2013 15:41
-
-
Save jgaskins/4739788 to your computer and use it in GitHub Desktop.
Clojure's `iterate` function 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 Object | |
def iterate(&block) | |
Enumerator.new do |yielder| | |
current = self | |
loop do | |
yielder << current | |
current = yield(current) | |
end | |
end | |
end | |
end | |
p 1.iterate{|i| i + 1}.take(5) | |
# => [1, 2, 3, 4, 5] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment