-
Notifications
You must be signed in to change notification settings - Fork 59
/
Copy pathtc_wc.rb
68 lines (55 loc) · 1.29 KB
/
tc_wc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
require './test_common'
class WordCount1
include Bud
attr_reader :pattern
def initialize(pattern, *options)
super(*options)
@pattern = pattern
end
state do
file_reader :txt, 'text/ulysses.txt'
scratch :wc, [:word] => [:cnt]
end
bloom do
wc <= txt.flat_map do |t|
t.text.split.enum_for(:each_with_index).map {|w, i| [t.lineno, i, w]}
end.rename(:loo, [:lineno, :wordno, :word]).group([:word], count)
end
end
class TestWC1 < Minitest::Test
def test_wc1
program = WordCount1.new(/[Bb]loom/)
program.tick
assert_equal(23, program.wc[["yes"]].cnt)
end
end
class WordCount2
include Bud
attr_reader :pattern
def initialize(pattern, *options)
super(*options)
@pattern = pattern
end
state do
file_reader :txt, 'text/ulysses.txt'
scratch :words, [:lineno, :wordno] => [:word]
scratch :wc, [:word] => [:cnt]
end
bloom do
words <= txt.flat_map do |t|
t.text.split.enum_for(:each_with_index).map {|w, i| [t.lineno, i, w]}
end
wc <= words.reduce(Hash.new) do |memo, t|
memo[t.word] ||= 0
memo[t.word] += 1
memo
end
end
end
class TestWC2 < Minitest::Test
def test_wc2
program = WordCount2.new(/[Bb]loom/)
program.tick
assert_equal(23, program.wc[["yes"]].cnt)
end
end