Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Joe Nelson and Jaymes Waters
authored and
bendyworks
committed
May 18, 2012
1 parent
c926c0e
commit 7076216
Showing
3 changed files
with
80 additions
and
67 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,79 +1,18 @@ | ||
require "itertools/version" | ||
require "itertools/infinite_iterators" | ||
require "itertools/terminating_iterators" | ||
require "fiber" | ||
|
||
module Itertools | ||
class << self | ||
def count start, step = 1 | ||
include InfiniteIterators | ||
include TerminatingIterators | ||
def forever | ||
Fiber.new do | ||
while true | ||
Fiber.yield start | ||
start += step | ||
yield | ||
end | ||
end | ||
end | ||
|
||
def cycle wheel | ||
Fiber.new do | ||
spoke = 0 | ||
loop do | ||
Fiber.yield wheel[spoke] | ||
spoke = (spoke + 1) % wheel.length | ||
end | ||
end | ||
end | ||
|
||
def repeat element, count = -1 | ||
Fiber.new do | ||
while count != 0 | ||
Fiber.yield element | ||
count -= 1 | ||
end | ||
raise StopIteration | ||
end | ||
end | ||
|
||
def chain *fibers | ||
Fiber.new do | ||
for f in fibers | ||
begin | ||
loop { Fiber.yield f.resume } | ||
rescue StopIteration | ||
end | ||
end | ||
raise StopIteration | ||
end | ||
end | ||
|
||
def compress seq, mask | ||
Fiber.new do | ||
while true | ||
s, m = seq.resume, mask.resume | ||
Fiber.yield s if m == 1 | ||
end | ||
end | ||
end | ||
|
||
def dropwhile pred, seq | ||
Fiber.new do | ||
while true | ||
x = seq.resume | ||
if !(pred.call x) | ||
Fiber.yield x | ||
break | ||
end | ||
end | ||
loop { Fiber.yield seq.resume } | ||
end | ||
end | ||
|
||
def filterfalse pred, seq | ||
Fiber.new do | ||
while true | ||
s = seq.resume | ||
Fiber.yield s if !pred.call s | ||
end | ||
end | ||
end | ||
|
||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
module Itertools | ||
module InfiniteIterators | ||
def count start, step = 1 | ||
forever do | ||
Fiber.yield start | ||
start += step | ||
end | ||
end | ||
|
||
def cycle wheel | ||
Fiber.new do | ||
spoke = 0 | ||
loop do | ||
Fiber.yield wheel[spoke] | ||
spoke = (spoke + 1) % wheel.length | ||
end | ||
end | ||
end | ||
|
||
def repeat element, count = -1 | ||
Fiber.new do | ||
while count != 0 | ||
Fiber.yield element | ||
count -= 1 | ||
end | ||
raise StopIteration | ||
end | ||
end | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
module Itertools | ||
module TerminatingIterators | ||
|
||
def chain *fibers | ||
Fiber.new do | ||
for f in fibers | ||
begin | ||
loop { Fiber.yield f.resume } | ||
rescue StopIteration | ||
end | ||
end | ||
raise StopIteration | ||
end | ||
end | ||
|
||
def compress seq, mask | ||
forever do | ||
s, m = seq.resume, mask.resume | ||
Fiber.yield s if m == 1 | ||
end | ||
end | ||
|
||
def dropwhile pred, seq | ||
Fiber.new do | ||
while true | ||
x = seq.resume | ||
if !(pred.call x) | ||
Fiber.yield x | ||
break | ||
end | ||
end | ||
loop { Fiber.yield seq.resume } | ||
end | ||
end | ||
|
||
def filterfalse pred, seq | ||
forever do | ||
s = seq.resume | ||
Fiber.yield s if !pred.call s | ||
end | ||
end | ||
|
||
end | ||
end |