mug 0.5.3 → 0.5.4
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.
- checksums.yaml +4 -4
- data/lib/mug/and-or.rb +26 -26
- data/lib/mug/any-and-all.rb +16 -16
- data/lib/mug/apply.rb +37 -37
- data/lib/mug/array/extend.rb +43 -43
- data/lib/mug/array/minus.rb +32 -32
- data/lib/mug/bool.rb +71 -71
- data/lib/mug/clamp.rb +35 -35
- data/lib/mug/counts.rb +27 -27
- data/lib/mug/fragile-method-chain.rb +42 -42
- data/lib/mug/hash/map.rb +75 -75
- data/lib/mug/hash/operations.rb +47 -47
- data/lib/mug/iterator/for.rb +7 -7
- data/lib/mug/iterator/method.rb +7 -7
- data/lib/mug/iterator_c.rb +14 -14
- data/lib/mug/loop-with.rb +30 -29
- data/lib/mug/matchdata/each.rb +40 -40
- data/lib/mug/matchdata/hash.rb +29 -29
- data/lib/mug/maybe.rb +33 -33
- data/lib/mug/negativity.rb +20 -20
- data/lib/mug/not.rb +6 -6
- data/lib/mug/rexproc.rb +6 -6
- data/lib/mug/self.rb +31 -31
- data/lib/mug/to_h.rb +2 -2
- data/lib/mug/top.rb +100 -100
- data/test/test-and-or.rb +32 -32
- data/test/test-any-and-all.rb +19 -19
- data/test/test-apply.rb +47 -47
- data/test/test-array-extend.rb +54 -54
- data/test/test-array-minus.rb +11 -11
- data/test/test-bool.rb +48 -48
- data/test/test-clamp.rb +42 -42
- data/test/test-counts.rb +21 -21
- data/test/test-fragile-method-chain.rb +56 -56
- data/test/test-hashmap.rb +14 -14
- data/test/test-hashop.rb +47 -47
- data/test/test-iterator-for.rb +12 -12
- data/test/test-loop-with.rb +117 -66
- data/test/test-matchdata_each.rb +50 -50
- data/test/test-matchdata_hash.rb +40 -40
- data/test/test-maybe.rb +76 -76
- data/test/test-negativity.rb +40 -40
- data/test/test-not.rb +53 -53
- data/test/test-rexproc.rb +6 -6
- data/test/test-self.rb +21 -21
- data/test/test-tau.rb +12 -12
- data/test/test-top.rb +100 -100
- metadata +4 -4
data/lib/mug/rexproc.rb
CHANGED
@@ -1,11 +1,11 @@
|
|
1
1
|
|
2
2
|
class Regexp
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
3
|
+
#
|
4
|
+
# Matches the regexp against the parameter object.
|
5
|
+
#
|
6
|
+
def to_proc
|
7
|
+
lambda {|s| self =~ s }
|
8
|
+
end
|
9
9
|
end
|
10
10
|
|
11
11
|
=begin
|
data/lib/mug/self.rb
CHANGED
@@ -1,38 +1,38 @@
|
|
1
1
|
|
2
2
|
class Object
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
3
|
+
#
|
4
|
+
# Returns this object.
|
5
|
+
#
|
6
|
+
# If a block is given, this object is yielded to it, and the result
|
7
|
+
# is returned.
|
8
|
+
#
|
9
|
+
def self(&block)
|
10
|
+
if block_given?
|
11
|
+
yield self
|
12
|
+
else
|
13
|
+
self
|
14
|
+
end
|
15
|
+
end
|
16
|
+
alias :itself :self
|
17
17
|
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
18
|
+
# Deprecated alias for #self
|
19
|
+
def yield(&block) #:nodoc:
|
20
|
+
warn 'Object#yield is deprecated; use Object#self'
|
21
|
+
self.self(&block)
|
22
|
+
end
|
23
23
|
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
24
|
+
#
|
25
|
+
# Yields this object (and any other arguments)
|
26
|
+
# to a block. If no block is given, returns an
|
27
|
+
# Enumerator.
|
28
|
+
#
|
29
|
+
def revapply(*args, &block)
|
30
|
+
if block_given?
|
31
|
+
yield self, *args
|
32
|
+
else
|
33
|
+
enum_for(:revapply, *args) { args.length + 1 }
|
34
|
+
end
|
35
|
+
end
|
36
36
|
end
|
37
37
|
|
38
38
|
=begin
|
data/lib/mug/to_h.rb
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
|
2
2
|
if {}.respond_to? :to_h
|
3
|
-
|
3
|
+
warn %|Warning: "mug/to_h" has been removed as it conflicts with core to_h behaviour|
|
4
4
|
else
|
5
|
-
|
5
|
+
warn %|Warning: "mug/to_h" has been removed; see the "to_h" gem (https://rubygems.org/gems/to_h)|
|
6
6
|
end
|
7
7
|
|
8
8
|
=begin
|
data/lib/mug/top.rb
CHANGED
@@ -1,113 +1,113 @@
|
|
1
1
|
|
2
2
|
module Enumerable
|
3
3
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
4
|
+
#
|
5
|
+
# Get the top +n+ items, in order from top to bottom.
|
6
|
+
#
|
7
|
+
# Returns an +Array+ even when +n+ is 1.
|
8
|
+
#
|
9
|
+
# @see Enumerable#sort
|
10
|
+
#
|
11
|
+
def top n=1, &blk
|
12
|
+
if block_given?
|
13
|
+
sort{|x,y| yield y, x }[0...n]
|
14
|
+
else
|
15
|
+
#top_by(n) {|x| x }
|
16
|
+
if n <= length
|
17
|
+
sort[-n..-1].reverse
|
18
|
+
else
|
19
|
+
sort.reverse
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
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
|
-
|
24
|
+
#
|
25
|
+
# Get the top +n+ items, in order from top to bottom, ordered
|
26
|
+
# by mapping the values through the given block.
|
27
|
+
#
|
28
|
+
# Returns an +Array+ even when +n+ is 1. Values that are tied
|
29
|
+
# after mapping are returned in the initial order.
|
30
|
+
#
|
31
|
+
# If no block is given, an enumerator is returned instead.
|
32
|
+
#
|
33
|
+
# @see Enumerable#sort_by
|
34
|
+
#
|
35
|
+
def top_by n=1, &blk
|
36
|
+
return enum_for(:top_by, n) unless block_given?
|
37
|
+
chain = {}
|
38
|
+
each do |x|
|
39
|
+
y = yield x
|
40
|
+
chain[y] ||= []
|
41
|
+
chain[y] << x
|
42
|
+
end
|
43
|
+
ary = []
|
44
|
+
chain.keys.sort.reverse.each do |k|
|
45
|
+
ary += chain[k]
|
46
|
+
break if ary.length > n
|
47
|
+
end
|
48
|
+
ary[0...n]
|
49
|
+
end
|
50
50
|
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
51
|
+
#
|
52
|
+
# Get the bottom +n+ items, ordered from bottom to top.
|
53
|
+
#
|
54
|
+
# Returns an +Array+ even when +n+ is 1.
|
55
|
+
#
|
56
|
+
# @see Enumerable#sort
|
57
|
+
#
|
58
|
+
def bottom n=1, &blk
|
59
|
+
if block_given?
|
60
|
+
sort(&blk)[0...n]
|
61
|
+
else
|
62
|
+
#bottom_by(n) {|x| x }
|
63
|
+
sort[0...n]
|
64
|
+
end
|
65
|
+
end
|
66
66
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
67
|
+
#
|
68
|
+
# Get the bottom +n+ items, in order from bottom to top, ordered
|
69
|
+
# by mapping the values through the given block.
|
70
|
+
#
|
71
|
+
# Returns an +Array+ even when +n+ is 1. Values that are tied
|
72
|
+
# after mapping are returned in the initial order.
|
73
|
+
#
|
74
|
+
# If no block is given, an enumerator is returned instead.
|
75
|
+
#
|
76
|
+
# @see Enumerable#sort_by
|
77
|
+
#
|
78
|
+
def bottom_by n=1, &blk
|
79
|
+
return enum_for(:bottom_by, n) unless block_given?
|
80
|
+
chain = {}
|
81
|
+
each do |x|
|
82
|
+
y = yield x
|
83
|
+
chain[y] ||= []
|
84
|
+
chain[y] << x
|
85
|
+
end
|
86
|
+
ary = []
|
87
|
+
chain.keys.sort.each do |k|
|
88
|
+
ary += chain[k]
|
89
|
+
break if ary.length > n
|
90
|
+
end
|
91
|
+
ary[0...n]
|
92
|
+
end
|
93
93
|
|
94
94
|
end
|
95
95
|
|
96
96
|
class Array
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
|
107
|
-
|
108
|
-
|
109
|
-
|
110
|
-
|
97
|
+
def top! n=1, &blk
|
98
|
+
replace(top n, &blk)
|
99
|
+
end
|
100
|
+
def top_by! n=1, &blk
|
101
|
+
return enum_for(:top_by!, n) unless block_given?
|
102
|
+
replace(top_by n, &blk)
|
103
|
+
end
|
104
|
+
def bottom! n=1, &blk
|
105
|
+
replace(bottom n, &blk)
|
106
|
+
end
|
107
|
+
def bottom_by! n=1, &blk
|
108
|
+
return enum_for(:bottom_by!, n) unless block_given?
|
109
|
+
replace(bottom_by n, &blk)
|
110
|
+
end
|
111
111
|
end
|
112
112
|
|
113
113
|
=begin
|
data/test/test-and-or.rb
CHANGED
@@ -4,38 +4,38 @@ $VERBOSE = true
|
|
4
4
|
require_relative '../lib/mug/and-or'
|
5
5
|
class Test_and_or < Test::Unit::TestCase
|
6
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
|
-
|
7
|
+
def test_and
|
8
|
+
assert_equal( 2, 1.and(2) )
|
9
|
+
assert_equal( 2, 0.and(2) )
|
10
|
+
assert_equal( 2, 'x'.and(2) )
|
11
|
+
assert_equal( 2, true.and(2) )
|
12
|
+
assert_equal( false, false.and(2) )
|
13
|
+
assert_equal( nil, nil.and(2) )
|
14
|
+
end
|
15
|
+
|
16
|
+
def test_and_2
|
17
|
+
assert_equal( [1], [1].and([2], &:empty?) )
|
18
|
+
assert_equal( [2], [].and([2], &:empty?) )
|
19
|
+
assert_equal( 'abc', 'abc'.and(''){|x|x =~ /\d/} )
|
20
|
+
assert_equal( '', 'a1c'.and(''){|x|x =~ /\d/} )
|
21
|
+
end
|
22
|
+
|
23
|
+
|
24
|
+
def test_or
|
25
|
+
assert_equal( 1, 1.or(2) )
|
26
|
+
assert_equal( 0, 0.or(2) )
|
27
|
+
assert_equal( 'x', 'x'.or(2) )
|
28
|
+
assert_equal( true, true.or(2) )
|
29
|
+
assert_equal( 2, false.or(2) )
|
30
|
+
assert_equal( 2, nil.or(2) )
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_or_2
|
34
|
+
assert_equal( [2], [1].or([2], &:empty?) )
|
35
|
+
assert_equal( [], [].or([2], &:empty?) )
|
36
|
+
assert_equal( '', 'abc'.or(''){|x|x =~ /\d/} )
|
37
|
+
assert_equal( 'a1c', 'a1c'.or(''){|x|x =~ /\d/} )
|
38
|
+
end
|
39
39
|
|
40
40
|
end
|
41
41
|
|
data/test/test-any-and-all.rb
CHANGED
@@ -3,23 +3,23 @@ $VERBOSE = true
|
|
3
3
|
|
4
4
|
require_relative '../lib/mug/any-and-all'
|
5
5
|
class Test_any_and_all < Test::Unit::TestCase
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
6
|
+
def test_any_and_all__block
|
7
|
+
b = proc {|o| o > 1 }
|
8
|
+
[
|
9
|
+
[ false, [] ],
|
10
|
+
[ false, [3,0,9] ],
|
11
|
+
[ true, [3,5,9] ],
|
12
|
+
].each do |x, a|
|
13
|
+
assert_equal( x, a.any_and_all?(&b) )
|
14
|
+
end
|
15
|
+
end
|
16
|
+
def test_any_and_all__noblock
|
17
|
+
[
|
18
|
+
[ false, [] ],
|
19
|
+
[ false, [3,nil,9] ],
|
20
|
+
[ true, [3,5,9] ],
|
21
|
+
].each do |x, a|
|
22
|
+
assert_equal( x, a.any_and_all? )
|
23
|
+
end
|
24
|
+
end
|
25
25
|
end
|
data/test/test-apply.rb
CHANGED
@@ -2,55 +2,55 @@ require 'test/unit'
|
|
2
2
|
$VERBOSE = true
|
3
3
|
|
4
4
|
class Foo
|
5
|
-
|
6
|
-
|
7
|
-
|
5
|
+
def bar a, b
|
6
|
+
(a||0) + (b||0)
|
7
|
+
end
|
8
8
|
end
|
9
9
|
|
10
10
|
require_relative '../lib/mug/apply'
|
11
11
|
class Test_apply < Test::Unit::TestCase
|
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
|
-
|
12
|
+
def test_apply_proc
|
13
|
+
prc = proc{|a,b,*c| (a||0) + (b||0) + c.inject(0, &:+) }
|
14
|
+
p1 = prc.apply(1)
|
15
|
+
assert( p1.is_a? Proc )
|
16
|
+
assert_equal( 3, p1.apply(2) )
|
17
|
+
assert_equal( 3, prc.apply(1,2) )
|
18
|
+
assert_equal( 15, prc.apply(1,2,3,4,5) )
|
19
|
+
end
|
20
|
+
def test_apply_lambda
|
21
|
+
lmb = lambda{|a,b,c| (a||0) + (b||0) + (c||0) }
|
22
|
+
|
23
|
+
l1 = lmb.apply(1)
|
24
|
+
assert( l1.is_a? Proc )
|
25
|
+
|
26
|
+
l2 = l1.apply(2)
|
27
|
+
assert( l2.is_a? Proc )
|
28
|
+
|
29
|
+
assert_equal( 6, l2.apply(3) )
|
30
|
+
assert_equal( 6, l1.apply(2,3) )
|
31
|
+
|
32
|
+
assert_raise( ArgumentError ){ l2.apply(3, 4) }
|
33
|
+
assert_raise( ArgumentError ){ lmb.apply(1, 2, 3, 4) }
|
34
|
+
end
|
35
|
+
def test_apply_method
|
36
|
+
m1 = 1.method :+
|
37
|
+
assert_equal( 3, m1.apply(2) )
|
38
|
+
assert_raise( ArgumentError ){ m1.apply(2,3) }
|
39
|
+
|
40
|
+
m2 = Foo.new.method :bar
|
41
|
+
l1 = m2.apply(1)
|
42
|
+
assert( l1.is_a? Proc )
|
43
|
+
|
44
|
+
assert_equal( 3, l1.apply(2) )
|
45
|
+
assert_equal( 3, m2.apply(1,2) )
|
46
|
+
|
47
|
+
assert_raise( ArgumentError ){ l1.apply(2, 3) }
|
48
|
+
assert_raise( ArgumentError ){ m2.apply(1, 2, 3) }
|
49
|
+
end
|
50
|
+
def test_curry_method
|
51
|
+
m1 = 1.method :+
|
52
|
+
assert( m1.curry.is_a? Proc )
|
53
|
+
assert( m1.curry(1).is_a? Proc )
|
54
|
+
assert_raise( ArgumentError ){ m1.curry(2) }
|
55
|
+
end
|
56
56
|
end
|