mug 0.2.5 → 0.2.6

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/mug.rb CHANGED
@@ -3,11 +3,13 @@ require_relative 'mug/and-or'
3
3
  require_relative 'mug/apply'
4
4
  require_relative 'mug/array/extend'
5
5
  require_relative 'mug/bool'
6
+ require_relative 'mug/counts'
6
7
  require_relative 'mug/fragile-method-chain'
7
8
  require_relative 'mug/hash/map'
8
9
  require_relative 'mug/hash/operations'
9
10
  require_relative 'mug/iterator/for'
10
11
  require_relative 'mug/iterator/method'
12
+ require_relative 'mug/loop-with'
11
13
  require_relative 'mug/maybe'
12
14
  require_relative 'mug/self'
13
15
  require_relative 'mug/tau'
data/lib/mug/counts.rb ADDED
@@ -0,0 +1,34 @@
1
+
2
+ module Enumerable
3
+
4
+ #
5
+ # Returns a hash of item=>count showing how many
6
+ # of each +item+ are in this Enumerable.
7
+ #
8
+ def counts &block
9
+ return counts_by(&block) if block_given?
10
+ hsh = Hash.new{|h,k| h[k] = 0 }
11
+ each do |k|
12
+ hsh[k] += 1
13
+ end
14
+ hsh
15
+ end
16
+
17
+ #
18
+ # Passes each element in turn to the block, and returns a
19
+ # hash of result=>count.
20
+ #
21
+ # If no block is given, an enumerator is returned.
22
+ #
23
+ def counts_by &block
24
+ return enum_for(:counts_by) unless block_given?
25
+ hsh = Hash.new{|h,k| h[k] = 0 }
26
+ each do |j|
27
+ k = yield j
28
+ hsh[k] += 1
29
+ end
30
+ hsh
31
+ end
32
+
33
+ end
34
+
@@ -0,0 +1,34 @@
1
+
2
+ module Kernel
3
+ #
4
+ # Repeatedly executes the block, yielding the current iteration
5
+ # count, which starts from +offset+. If no block is given, returns
6
+ # a new Enumerator that includes the iteration count, starting
7
+ # from +offset+
8
+ #
9
+ def loop_with_index(offset=0)
10
+ return c=enum_for(:loop_with_index, offset) unless block_given?
11
+ c = 0 + offset
12
+ while true
13
+ yield c
14
+ c += 1
15
+ end
16
+ rescue StopIteration
17
+ ensure
18
+ return c
19
+ end
20
+
21
+ #
22
+ # Repeatedly executes the block, yielding an arbitrary object, +obj+.
23
+ #
24
+ def loop_with_object(obj)
25
+ return obj=enum_for(:loop_with_object, obj) unless block_given?
26
+ while true
27
+ yield obj
28
+ end
29
+ rescue StopIteration
30
+ ensure
31
+ return obj
32
+ end
33
+ end
34
+
@@ -0,0 +1,28 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/counts'
5
+ class Test_hashmap < Test::Unit::TestCase
6
+ def test_counts
7
+ a = %w(a b b c c c)
8
+ c = {'a'=>1, 'b'=>2, 'c'=>3}
9
+ assert_equal( c, a.counts )
10
+ end
11
+ def test_counts__block
12
+ a = %w(a b b c c c)
13
+ c = {'A'=>1, 'B'=>2, 'C'=>3}
14
+ assert_equal( c, a.counts{|i|i.upcase} )
15
+ end
16
+ def test_counts_by
17
+ a = %w(a b b c c c)
18
+ c = {'A'=>1, 'B'=>2, 'C'=>3}
19
+ assert_equal( c, a.counts_by{|i|i.upcase} )
20
+ end
21
+ def test_counts_by__noblock
22
+ a = %w(a b b c c c)
23
+ c = {'A'=>1, 'B'=>2, 'C'=>3}
24
+ e = a.counts_by
25
+ assert_equal( c, e.each{|i|i.upcase} )
26
+ end
27
+ end
28
+
@@ -0,0 +1,75 @@
1
+ require 'test/unit'
2
+ $VERBOSE = true
3
+
4
+ require_relative '../lib/mug/loop-with'
5
+ class Test_loop_with < Test::Unit::TestCase
6
+
7
+ def test_loop_with_index__block
8
+ a = []
9
+ b = []
10
+ x = loop_with_index do |i|
11
+ a << i
12
+ break if a.length >= 3
13
+ end
14
+ y = loop_with_index do |i|
15
+ b << i
16
+ throw StopIteration if b.length >= 3
17
+ end
18
+ assert_equal( [0,1,2], a )
19
+ assert_equal( [0,1,2], b )
20
+ assert_equal( 2, x )
21
+ assert_equal( 2, y )
22
+ end
23
+ def test_loop_with_index__block_offset
24
+ a = []
25
+ b = []
26
+ x = loop_with_index(10) do |i|
27
+ a << i
28
+ break if a.length >= 3
29
+ end
30
+ y = loop_with_index(10) do |i|
31
+ b << i
32
+ throw StopIteration if b.length >= 3
33
+ end
34
+ assert_equal( [10,11,12], a )
35
+ assert_equal( [10,11,12], b )
36
+ assert_equal( 12, x )
37
+ assert_equal( 12, y )
38
+ end
39
+ def test_loop_with_index__enum
40
+ a = []
41
+ b = []
42
+ enum = loop_with_index
43
+ x = enum.each do |i|
44
+ a << i
45
+ break if a.length >= 3
46
+ end
47
+ y = enum.each do |i|
48
+ b << i
49
+ throw StopIteration if b.length >= 3
50
+ end
51
+ assert_equal( [0,1,2], a )
52
+ assert_equal( [0,1,2], b )
53
+ assert_equal( 2, x )
54
+ assert_equal( 2, y )
55
+ end
56
+ def test_loop_with_index__enum_offset
57
+ a = []
58
+ b = []
59
+ enum = loop_with_index(10)
60
+ x = enum.each do |i|
61
+ a << i
62
+ break if a.length >= 3
63
+ end
64
+ y = enum.each do |i|
65
+ b << i
66
+ throw StopIteration if b.length >= 3
67
+ end
68
+ assert_equal( [10,11,12], a )
69
+ assert_equal( [10,11,12], b )
70
+ assert_equal( 12, x )
71
+ assert_equal( 12, y )
72
+ end
73
+
74
+ end
75
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mug
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-10-08 00:00:00.000000000 Z
12
+ date: 2014-12-01 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: ! '== MUG: Matty''s Ultimate Gem
15
15
 
@@ -41,8 +41,10 @@ files:
41
41
  - lib/mug/apply.rb
42
42
  - lib/mug/to_h.rb
43
43
  - lib/mug/array/extend.rb
44
+ - lib/mug/loop-with.rb
44
45
  - lib/mug/fragile-method-chain.rb
45
46
  - lib/mug/hashmap.rb
47
+ - lib/mug/counts.rb
46
48
  - lib/mug/iterator_c.rb
47
49
  - lib/mug/hashop.rb
48
50
  - lib/mug/iterator.rb
@@ -55,11 +57,13 @@ files:
55
57
  - test/test_apply.rb
56
58
  - test/test-bool.rb
57
59
  - test/test-top.rb
60
+ - test/test-loop-with.rb
58
61
  - test/test-to_h.rb
59
62
  - test/test-array-extend.rb
60
63
  - test/test-self.rb
61
64
  - test/test-iterator-method.rb
62
65
  - test/test-maybe.rb
66
+ - test/test-counts.rb
63
67
  - test/test-fragile-method-chain.rb
64
68
  homepage: http://phluid61.github.com/mug
65
69
  licenses:
@@ -95,10 +99,12 @@ test_files:
95
99
  - test/test_apply.rb
96
100
  - test/test-bool.rb
97
101
  - test/test-top.rb
102
+ - test/test-loop-with.rb
98
103
  - test/test-to_h.rb
99
104
  - test/test-array-extend.rb
100
105
  - test/test-self.rb
101
106
  - test/test-iterator-method.rb
102
107
  - test/test-maybe.rb
108
+ - test/test-counts.rb
103
109
  - test/test-fragile-method-chain.rb
104
110
  has_rdoc: true