Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 144 lines (110 sloc) 2.457 kB
7213c40 @nate Update spec/spec_helper.rb (formerly helper.rb) and set a default rak…
nate authored
1 require "spec_helper"
99f505d @igrigorik sieve of Eratosthenes example
authored
2
3 describe "sieve of Eratosthenes" do
4
5 # http://golang.org/doc/go_tutorial.html#tmp_353
6
7 it "should work using Channel primitives" do
8
9 # send the sequence 2,3,4, ... to returned channel
10 def generate
492632f @nate Import of gore and making the specs work with the new code
nate authored
11 ch = channel!(:type => Integer)
99f505d @igrigorik sieve of Eratosthenes example
authored
12
492632f @nate Import of gore and making the specs work with the new code
nate authored
13 go! do
99f505d @igrigorik sieve of Eratosthenes example
authored
14 i = 1
15 loop { ch << i+= 1 }
16 end
17
18 return ch
19 end
20
21 # filter out input values divisible by *prime*, send rest to returned channel
22 def filter(in_channel, prime)
492632f @nate Import of gore and making the specs work with the new code
nate authored
23 out = channel!(:type => Integer)
99f505d @igrigorik sieve of Eratosthenes example
authored
24
492632f @nate Import of gore and making the specs work with the new code
nate authored
25 go! do
99f505d @igrigorik sieve of Eratosthenes example
authored
26 loop do
f702c57 @nate Changing how receive is used in a few places
nate authored
27 i, _ = in_channel.receive
99f505d @igrigorik sieve of Eratosthenes example
authored
28 out << i if (i % prime) != 0
29 end
30 end
31
32 return out
33 end
34
35 def sieve
492632f @nate Import of gore and making the specs work with the new code
nate authored
36 out = channel!(:type => Integer)
99f505d @igrigorik sieve of Eratosthenes example
authored
37
492632f @nate Import of gore and making the specs work with the new code
nate authored
38 go! do
99f505d @igrigorik sieve of Eratosthenes example
authored
39 ch = generate
40 loop do
f702c57 @nate Changing how receive is used in a few places
nate authored
41 prime, _ = ch.receive
99f505d @igrigorik sieve of Eratosthenes example
authored
42 out << prime
43 ch = filter(ch, prime)
44 end
45 end
46
47 return out
48 end
49
50 # run the sieve
51 n = 20
52 nth = false
53
54 primes = sieve
55 result = []
56
57 if nth
58 n.times { primes.receive }
492632f @nate Import of gore and making the specs work with the new code
nate authored
59 puts primes.receive[0]
99f505d @igrigorik sieve of Eratosthenes example
authored
60 else
61 loop do
f702c57 @nate Changing how receive is used in a few places
nate authored
62 p, _ = primes.receive
99f505d @igrigorik sieve of Eratosthenes example
authored
63
64 if p <= n
65 result << p
66 else
67 break
68 end
69 end
70 end
71
72 result.should == [2,3,5,7,11,13,17,19]
73 end
74
75 it "should work with Ruby blocks" do
76
77 # send the sequence 2,3,4, ... to returned channel
78 generate = Proc.new do
492632f @nate Import of gore and making the specs work with the new code
nate authored
79 ch = channel!(:type => Integer)
99f505d @igrigorik sieve of Eratosthenes example
authored
80
492632f @nate Import of gore and making the specs work with the new code
nate authored
81 go! do
99f505d @igrigorik sieve of Eratosthenes example
authored
82 i = 1
83 loop { ch << i+= 1 }
84 end
85
86 ch
87 end
88
89 # filter out input values divisible by *prime*, send rest to returned channel
90 filtr = Proc.new do |in_channel, prime|
492632f @nate Import of gore and making the specs work with the new code
nate authored
91 out = channel!(:type => Integer)
99f505d @igrigorik sieve of Eratosthenes example
authored
92
492632f @nate Import of gore and making the specs work with the new code
nate authored
93 go! do
99f505d @igrigorik sieve of Eratosthenes example
authored
94 loop do
f702c57 @nate Changing how receive is used in a few places
nate authored
95 i, _ = in_channel.receive
99f505d @igrigorik sieve of Eratosthenes example
authored
96 out << i if (i % prime) != 0
97 end
98 end
99
100 out
101 end
102
bef6e7f @igrigorik stabby lambdas are java.lang.NullPointerException's in jruby atm :-)
authored
103 sieve = Proc.new do
492632f @nate Import of gore and making the specs work with the new code
nate authored
104 out = channel!(:type => Integer)
99f505d @igrigorik sieve of Eratosthenes example
authored
105
492632f @nate Import of gore and making the specs work with the new code
nate authored
106 go! do
99f505d @igrigorik sieve of Eratosthenes example
authored
107 ch = generate.call
108
109 loop do
f702c57 @nate Changing how receive is used in a few places
nate authored
110 prime, _ = ch.receive
99f505d @igrigorik sieve of Eratosthenes example
authored
111 out << prime
112 ch = filtr.call(ch, prime)
113 end
114 end
115
116 out
117 end
118
119 # run the sieve
120 n = 20
121 nth = false
122
123 primes = sieve.call
124 result = []
125
126 if nth
127 n.times { primes.receive }
492632f @nate Import of gore and making the specs work with the new code
nate authored
128 puts primes.receive[0]
99f505d @igrigorik sieve of Eratosthenes example
authored
129 else
130 loop do
f702c57 @nate Changing how receive is used in a few places
nate authored
131 p, _ = primes.receive
99f505d @igrigorik sieve of Eratosthenes example
authored
132
133 if p <= n
134 result << p
135 else
136 break
137 end
138 end
139 end
140
141 result.should == [2,3,5,7,11,13,17,19]
142 end
143 end
Something went wrong with that request. Please try again.