Permalink
Browse files

Infinite sequences in ruby

  • Loading branch information...
1 parent 6451d54 commit 037bc8263a0383645d0c60d70a22a9dce13b8171 @tomafro committed Dec 31, 2012
View
@@ -0,0 +1,157 @@
+# map, flat_map, select, reject, grep, zip, take, take_while, drop, drop_while, cycle
+
+class LazyEnumerator < Enumerator
+ def select(&block)
+ chain do |yielder, element, index|
+ yielder.yield element if block.call(element)
+ end
+ end
+
+ def reject(&block)
+ chain do |yielder, element, index|
+ yielder.yield element unless block.call(element)
+ end
+ end
+
+ def map(&block)
+ chain do |yielder, element, index|
+ yielder.yield block.call(element)
+ end
+ end
+
+ def drop(n)
+ self.class.new do |yielder|
+ each.with_index do |element, index|
+ yielder.yield element if index >= n
+ end
+ end
+ end
+
+ def drop_while(&block)
+ self.class.new do |yielder|
+ match_found = false
+ each do |element|
+ match_found ||= !block.call(element)
+ yielder.yield element if match_found
+ end
+ end
+ end
+
+ def cycle
+ self.class.new do |yielder|
+ loop do
+ each do |element|
+ yielder.yield element
+ end
+ end
+ end
+ end
+
+ private
+
+ def chain(&block)
+ self.class.new do |yielder|
+ each do |element|
+ block.call yielder, element
+ end
+ end
+ end
+end
+
+e = LazyEnumerator.new do |seq|
+ n = 0
+ loop do
+ seq << n += 1
+ end
+end
+
+p e.take(10)
+
+p e.select {|n| n % 2 == 0}.take(10)
+p e.reject {|n| n % 2 == 0}.take(10)
+p
+p e.map {|n| n * 3}.take(10)
+
+p e.take_while {|n| p n;n < 3}
+
+p e.drop(5).take(10).drop(2).take(2)
+
+p e.drop_while {|n| n < 10}.take(10)
+p e.take_while {|n| n < 10}.to_a
+
+f = LazyEnumerator.new do |seq|
+ n = 0
+ while n < 10
+ seq << n += 1
+ end
+end
+
+p f.cycle.take(100)
+
+
+
+
+
+def next_occurrences(n, previous_occurrence, period)
+ 1.upto(n).map do |n|
+ previous_occurrence + (n * period)
+ end
+end
+
+require 'active_support/all'
+
+p next_occurrences(5, 1.minute.ago, 1.week)
+
+evens = Enumerator.new do |yielder|
+ n = 1
+ loop do
+ yielder.yield n * 2
+ n = n + 1
+ end
+end
+
+p evens.take(5)
+
+p evens.include?(1_000_000)
+
+previous_occurrence = 1.day.ago
+period = 5.days
+occurrences = Enumerator.new do |yielder|
+ n = 1
+ loop do
+ yielder.yield previous_occurrence + (n * period)
+ n = n + 1
+ end
+end
+
+p occurrences.take(4)
+
+p occurrences.take_while {|t| t < 1.month.from_now}
+
+p occurrences.drop_while {|t| t < 2.weeks.from_now}.take(3)
+
+e = Enumerator.new do |yielder|
+ n = 1
+ loop do
+ puts "Calculating value #{n}"
+ yielder.yield n
+ n = n + 1
+ end
+end
+
+e.take(5)
+
+def sequence(&generator)
+ Enumerator.new do |yielder|
+ n = 1
+ loop do
+ yielder.yield generator.call(n)
+ n = n + 1
+ end
+ end
+end
+
+sequence do |n|
+ puts "Calculating value #{n}"
+ n * n
+end
@@ -105,10 +105,11 @@
<h1>Related posts:</h1>
<ul>
<li>
- <a class='title' href="/2012/06/tip-bundler-with-binstubs">Tip: Bundler with --binstubs</a>
+ <a class='title' href="/2011/09/tip-automatic-bundle-exec-for-rake-and-more">Tip: Automatic bundle exec for rake and other gems</a>
<div class='meta'>
- <span class='date'><a href="/2012/06">21st June 2012</a></span>
+ <span class='date'><a href="/2011/09"> 1st September 2011</a></span>
<ul class='tags'>
+ <li><a href="/tags/zsh" rel="tag">zsh</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
<li><a href="/tags/gem" rel="tag">gem</a></li>
<li><a href="/tags/rake" rel="tag">rake</a></li>
@@ -118,11 +119,10 @@
</div>
</li>
<li>
- <a class='title' href="/2011/09/tip-automatic-bundle-exec-for-rake-and-more">Tip: Automatic bundle exec for rake and other gems</a>
+ <a class='title' href="/2012/06/tip-bundler-with-binstubs">Tip: Bundler with --binstubs</a>
<div class='meta'>
- <span class='date'><a href="/2011/09"> 1st September 2011</a></span>
+ <span class='date'><a href="/2012/06">21st June 2012</a></span>
<ul class='tags'>
- <li><a href="/tags/zsh" rel="tag">zsh</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
<li><a href="/tags/gem" rel="tag">gem</a></li>
<li><a href="/tags/rake" rel="tag">rake</a></li>
@@ -132,15 +132,14 @@
</div>
</li>
<li>
- <a class='title' href="/2009/05/automatching-rails-paths-in-cucumber">Automatching rails paths in cucumber</a>
+ <a class='title' href="/2010/02/updated-rails-template-for-bundler">An updated rails template for gem bundler</a>
<div class='meta'>
- <span class='date'><a href="/2009/05">13th May 2009</a></span>
+ <span class='date'><a href="/2010/02">28th February 2010</a></span>
<ul class='tags'>
- <li><a href="/tags/cucumber" rel="tag">cucumber</a></li>
- <li><a href="/tags/rails" rel="tag">rails</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
- <li><a href="/tags/testing" rel="tag">testing</a></li>
- <li><a href="/tags/tip" rel="tag">tip</a></li>
+ <li><a href="/tags/rails" rel="tag">rails</a></li>
+ <li><a href="/tags/gem" rel="tag">gem</a></li>
+ <li><a href="/tags/bundler" rel="tag">bundler</a></li>
</ul>
</div>
</li>
@@ -157,14 +156,15 @@
</div>
</li>
<li>
- <a class='title' href="/2010/02/updated-rails-template-for-bundler">An updated rails template for gem bundler</a>
+ <a class='title' href="/2009/05/automatching-rails-paths-in-cucumber">Automatching rails paths in cucumber</a>
<div class='meta'>
- <span class='date'><a href="/2010/02">28th February 2010</a></span>
+ <span class='date'><a href="/2009/05">13th May 2009</a></span>
<ul class='tags'>
- <li><a href="/tags/ruby" rel="tag">ruby</a></li>
+ <li><a href="/tags/cucumber" rel="tag">cucumber</a></li>
<li><a href="/tags/rails" rel="tag">rails</a></li>
- <li><a href="/tags/gem" rel="tag">gem</a></li>
- <li><a href="/tags/bundler" rel="tag">bundler</a></li>
+ <li><a href="/tags/ruby" rel="tag">ruby</a></li>
+ <li><a href="/tags/testing" rel="tag">testing</a></li>
+ <li><a href="/tags/tip" rel="tag">tip</a></li>
</ul>
</div>
</li>
@@ -158,11 +158,10 @@
</div>
</li>
<li>
- <a class='title' href="/2011/09/tip-automatic-bundle-exec-for-rake-and-more">Tip: Automatic bundle exec for rake and other gems</a>
+ <a class='title' href="/2012/06/tip-bundler-with-binstubs">Tip: Bundler with --binstubs</a>
<div class='meta'>
- <span class='date'><a href="/2011/09"> 1st September 2011</a></span>
+ <span class='date'><a href="/2012/06">21st June 2012</a></span>
<ul class='tags'>
- <li><a href="/tags/zsh" rel="tag">zsh</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
<li><a href="/tags/gem" rel="tag">gem</a></li>
<li><a href="/tags/rake" rel="tag">rake</a></li>
@@ -172,15 +171,16 @@
</div>
</li>
<li>
- <a class='title' href="/2011/02/rails-mongo-instrumentation-gem">Mongo instrumentation released as a gem</a>
+ <a class='title' href="/2011/09/tip-automatic-bundle-exec-for-rake-and-more">Tip: Automatic bundle exec for rake and other gems</a>
<div class='meta'>
- <span class='date'><a href="/2011/02">19th February 2011</a></span>
+ <span class='date'><a href="/2011/09"> 1st September 2011</a></span>
<ul class='tags'>
+ <li><a href="/tags/zsh" rel="tag">zsh</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
- <li><a href="/tags/rails" rel="tag">rails</a></li>
- <li><a href="/tags/rails3" rel="tag">rails3</a></li>
- <li><a href="/tags/mongo" rel="tag">mongo</a></li>
- <li><a href="/tags/instrumentation" rel="tag">instrumentation</a></li>
+ <li><a href="/tags/gem" rel="tag">gem</a></li>
+ <li><a href="/tags/rake" rel="tag">rake</a></li>
+ <li><a href="/tags/bundler" rel="tag">bundler</a></li>
+ <li><a href="/tags/tip" rel="tag">tip</a></li>
</ul>
</div>
</li>
@@ -138,15 +138,16 @@
</div>
</li>
<li>
- <a class='title' href="/2011/02/rails-mongo-instrumentation-gem">Mongo instrumentation released as a gem</a>
+ <a class='title' href="/2011/09/tip-automatic-bundle-exec-for-rake-and-more">Tip: Automatic bundle exec for rake and other gems</a>
<div class='meta'>
- <span class='date'><a href="/2011/02">19th February 2011</a></span>
+ <span class='date'><a href="/2011/09"> 1st September 2011</a></span>
<ul class='tags'>
+ <li><a href="/tags/zsh" rel="tag">zsh</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
- <li><a href="/tags/rails" rel="tag">rails</a></li>
- <li><a href="/tags/rails3" rel="tag">rails3</a></li>
- <li><a href="/tags/mongo" rel="tag">mongo</a></li>
- <li><a href="/tags/instrumentation" rel="tag">instrumentation</a></li>
+ <li><a href="/tags/gem" rel="tag">gem</a></li>
+ <li><a href="/tags/rake" rel="tag">rake</a></li>
+ <li><a href="/tags/bundler" rel="tag">bundler</a></li>
+ <li><a href="/tags/tip" rel="tag">tip</a></li>
</ul>
</div>
</li>
@@ -221,15 +221,14 @@
</div>
</li>
<li>
- <a class='title' href="/2009/09/tip-the-case-for-from-param">Tip: The case for from_param</a>
+ <a class='title' href="/2009/05/adam-sandersons-open-gem">Adam Sanderson's open_gem</a>
<div class='meta'>
- <span class='date'><a href="/2009/09">23rd September 2009</a></span>
+ <span class='date'><a href="/2009/05"> 6th May 2009</a></span>
<ul class='tags'>
- <li><a href="/tags/tip" rel="tag">tip</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
- <li><a href="/tags/rails" rel="tag">rails</a></li>
- <li><a href="/tags/active-record" rel="tag">active-record</a></li>
- <li><a href="/tags/from-param" rel="tag">from-param</a></li>
+ <li><a href="/tags/gem" rel="tag">gem</a></li>
+ <li><a href="/tags/tip" rel="tag">tip</a></li>
+ <li><a href="/tags/gem-open" rel="tag">gem-open</a></li>
</ul>
</div>
</li>
@@ -151,15 +151,14 @@
</div>
</li>
<li>
- <a class='title' href="/2009/09/tip-the-case-for-from-param">Tip: The case for from_param</a>
+ <a class='title' href="/2009/05/adam-sandersons-open-gem">Adam Sanderson's open_gem</a>
<div class='meta'>
- <span class='date'><a href="/2009/09">23rd September 2009</a></span>
+ <span class='date'><a href="/2009/05"> 6th May 2009</a></span>
<ul class='tags'>
- <li><a href="/tags/tip" rel="tag">tip</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
- <li><a href="/tags/rails" rel="tag">rails</a></li>
- <li><a href="/tags/active-record" rel="tag">active-record</a></li>
- <li><a href="/tags/from-param" rel="tag">from-param</a></li>
+ <li><a href="/tags/gem" rel="tag">gem</a></li>
+ <li><a href="/tags/tip" rel="tag">tip</a></li>
+ <li><a href="/tags/gem-open" rel="tag">gem-open</a></li>
</ul>
</div>
</li>
@@ -215,15 +215,14 @@
</div>
</li>
<li>
- <a class='title' href="/2009/09/tip-the-case-for-from-param">Tip: The case for from_param</a>
+ <a class='title' href="/2009/05/adam-sandersons-open-gem">Adam Sanderson's open_gem</a>
<div class='meta'>
- <span class='date'><a href="/2009/09">23rd September 2009</a></span>
+ <span class='date'><a href="/2009/05"> 6th May 2009</a></span>
<ul class='tags'>
- <li><a href="/tags/tip" rel="tag">tip</a></li>
<li><a href="/tags/ruby" rel="tag">ruby</a></li>
- <li><a href="/tags/rails" rel="tag">rails</a></li>
- <li><a href="/tags/active-record" rel="tag">active-record</a></li>
- <li><a href="/tags/from-param" rel="tag">from-param</a></li>
+ <li><a href="/tags/gem" rel="tag">gem</a></li>
+ <li><a href="/tags/tip" rel="tag">tip</a></li>
+ <li><a href="/tags/gem-open" rel="tag">gem-open</a></li>
</ul>
</div>
</li>
Oops, something went wrong.

0 comments on commit 037bc82

Please sign in to comment.