mug 0.15.0 → 1.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/mug.rb +1 -0
- data/lib/mug/iterator.rb +65 -2
- data/lib/mug/iterator/for.rb +5 -3
- data/lib/mug/iterator/method.rb +5 -3
- data/test/test-iterator-for.rb +4 -4
- data/test/test-iterator-method.rb +4 -4
- data/test/test-iterator.rb +17 -0
- metadata +28 -27
- data/lib/mug/iterator_c.rb +0 -44
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: be11c66cdcdc22f4149f088221fd1072fc075fe6b83ea3001a4c1c7d7a7398ce
|
4
|
+
data.tar.gz: 9df03bc1da33bbaed659ed753a83dbce4e72670f894038e3290d7732c9f38062
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0f5598b332bf113018a53b725e9811c7335b3bebd3cbd92368c25556ee2cfbf4236c101e4b9b186a9b74a41d38af27f08e12c3fafe7c4393cbd5fab087150091
|
7
|
+
data.tar.gz: ea4760031f9dae9856b1e627e76525e1ef87666ec7cca298589bd264e4c2ab87e100883b2c87f9b2345885a752a036088992f7a1ed598ac8a6f6a78ab81bf027
|
data/lib/mug.rb
CHANGED
@@ -17,6 +17,7 @@ require_relative 'mug/hash/map'
|
|
17
17
|
require_relative 'mug/hash/merge'
|
18
18
|
require_relative 'mug/hash/operations'
|
19
19
|
require_relative 'mug/hash/when'
|
20
|
+
require_relative 'mug/iterator'
|
20
21
|
require_relative 'mug/iterator/for'
|
21
22
|
require_relative 'mug/iterator/method'
|
22
23
|
require_relative 'mug/loop-with'
|
data/lib/mug/iterator.rb
CHANGED
@@ -1,2 +1,65 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
|
2
|
+
#
|
3
|
+
# A special class of Enumerator that repeatedly yields values
|
4
|
+
# to a block.
|
5
|
+
#
|
6
|
+
# The initial yielded value is given in the constructor, but in
|
7
|
+
# subsequent iterations the result of the previous iteration is
|
8
|
+
# yielded.
|
9
|
+
#
|
10
|
+
# Example:
|
11
|
+
#
|
12
|
+
# 0.iter_for(:next).take(5) #=> [1,2,3,4,5]
|
13
|
+
#
|
14
|
+
class Iterator < Enumerator
|
15
|
+
#
|
16
|
+
# Creates a new Iterator object, which can be used as an
|
17
|
+
# Enumerable.
|
18
|
+
#
|
19
|
+
# In the first form, iteration is defined by the given block,
|
20
|
+
# to which the current object and any other +args+ are yielded.
|
21
|
+
#
|
22
|
+
# In the second, deprecated, form, a generated Iterator sends the
|
23
|
+
# given method with any +args+ to the iterand.
|
24
|
+
#
|
25
|
+
# Use of this form is discourages. Use Object#iter_for or
|
26
|
+
# Object#to_iter instead.
|
27
|
+
#
|
28
|
+
# @call-seq new(initial, *args) { |obj, *args| ... }
|
29
|
+
# @call-seq new(initial, method=:each, *args)
|
30
|
+
#
|
31
|
+
def initialize obj, *args
|
32
|
+
if block_given?
|
33
|
+
super() do |y|
|
34
|
+
loop do
|
35
|
+
y << (obj = yield obj, *args)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
else
|
39
|
+
warn 'Iterator.new without a block is deprecated; use Object#to_iter'
|
40
|
+
args = [:each] if args.empty?
|
41
|
+
super() do |y|
|
42
|
+
loop do
|
43
|
+
y << (obj = obj.send(*args))
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
=begin
|
51
|
+
Copyright (c) 2013-2018, Matthew Kerwin <matthew@kerwin.net.au>
|
52
|
+
|
53
|
+
Permission to use, copy, modify, and/or distribute this software for any
|
54
|
+
purpose with or without fee is hereby granted, provided that the above
|
55
|
+
copyright notice and this permission notice appear in all copies.
|
56
|
+
|
57
|
+
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
58
|
+
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
59
|
+
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
60
|
+
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
61
|
+
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
62
|
+
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
63
|
+
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
64
|
+
=end
|
65
|
+
|
data/lib/mug/iterator/for.rb
CHANGED
@@ -1,18 +1,20 @@
|
|
1
1
|
|
2
|
-
require_relative '../
|
2
|
+
require_relative '../iterator'
|
3
3
|
|
4
4
|
class Object
|
5
5
|
#
|
6
6
|
# Creates a new Iterator for the method named +meth+
|
7
7
|
#
|
8
8
|
def iter_for meth, *args
|
9
|
-
Iterator.new
|
9
|
+
Iterator.new(self) do |o|
|
10
|
+
o.send(meth, *args)
|
11
|
+
end
|
10
12
|
end
|
11
13
|
alias to_iter iter_for
|
12
14
|
end
|
13
15
|
|
14
16
|
=begin
|
15
|
-
Copyright (c) 2013-
|
17
|
+
Copyright (c) 2013-2018, Matthew Kerwin <matthew@kerwin.net.au>
|
16
18
|
|
17
19
|
Permission to use, copy, modify, and/or distribute this software for any
|
18
20
|
purpose with or without fee is hereby granted, provided that the above
|
data/lib/mug/iterator/method.rb
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
|
2
|
-
require_relative '../
|
2
|
+
require_relative '../iterator'
|
3
3
|
|
4
4
|
class Method
|
5
5
|
#
|
@@ -7,12 +7,14 @@ class Method
|
|
7
7
|
# on this method's receiver.
|
8
8
|
#
|
9
9
|
def to_iter *args
|
10
|
-
Iterator.new
|
10
|
+
Iterator.new(receiver) do |o|
|
11
|
+
o.send(name, *args)
|
12
|
+
end
|
11
13
|
end
|
12
14
|
end
|
13
15
|
|
14
16
|
=begin
|
15
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
17
|
+
Copyright (c) 2013-2018, Matthew Kerwin <matthew@kerwin.net.au>
|
16
18
|
|
17
19
|
Permission to use, copy, modify, and/or distribute this software for any
|
18
20
|
purpose with or without fee is hereby granted, provided that the above
|
data/test/test-iterator-for.rb
CHANGED
@@ -5,14 +5,14 @@ require_relative '../lib/mug/iterator/for'
|
|
5
5
|
class IterForTest < Test::Unit::TestCase
|
6
6
|
def test_iter_for
|
7
7
|
# Test on Integer#next => Integer
|
8
|
-
assert_equal([
|
8
|
+
assert_equal([1, 2, 3, 4, 5], 0.iter_for(:next).take(5))
|
9
9
|
# Test on String#succ => String
|
10
|
-
assert_equal(%w[
|
10
|
+
assert_equal(%w[b c d e f g h i j k], 'a'.iter_for(:succ).take(10))
|
11
11
|
# Test on Integer#inspect => String#inspect => String
|
12
|
-
assert_equal([
|
12
|
+
assert_equal(["1", "\"1\""], 1.iter_for(:inspect).take(2))
|
13
13
|
end
|
14
14
|
def test_iter_for_args
|
15
15
|
# Test with a parameter
|
16
|
-
assert_equal([
|
16
|
+
assert_equal([2, 4, 6, 8, 10], 0.iter_for(:+, 2).take(5))
|
17
17
|
end
|
18
18
|
end
|
@@ -5,14 +5,14 @@ require_relative '../lib/mug/iterator/method'
|
|
5
5
|
class MethodToIterTest < Test::Unit::TestCase
|
6
6
|
def test_to_iter
|
7
7
|
# Test on Integer#next => Integer
|
8
|
-
assert_equal([
|
8
|
+
assert_equal([1, 2, 3, 4, 5], 0.method(:next).to_iter.take(5))
|
9
9
|
# Test on String#succ => String
|
10
|
-
assert_equal(%w[
|
10
|
+
assert_equal(%w[b c d e f g h i j k], 'a'.method(:succ).to_iter.take(10))
|
11
11
|
# Test on Integer#inspect => String#inspect => String
|
12
|
-
assert_equal([
|
12
|
+
assert_equal(["1", "\"1\""], 1.method(:inspect).to_iter.take(2))
|
13
13
|
end
|
14
14
|
def test_iter_for_args
|
15
15
|
# Test with a parameter
|
16
|
-
assert_equal([
|
16
|
+
assert_equal([2, 4, 6, 8, 10], 0.method(:+).to_iter(2).take(5))
|
17
17
|
end
|
18
18
|
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test/unit'
|
2
|
+
$VERBOSE = true
|
3
|
+
|
4
|
+
require_relative '../lib/mug/iterator'
|
5
|
+
class IteratorTest < Test::Unit::TestCase
|
6
|
+
def test_iterator
|
7
|
+
iter = Iterator.new(0, 1) do |o, x|
|
8
|
+
o + x
|
9
|
+
end
|
10
|
+
assert_equal([1,2,3,4,5], iter.take(5))
|
11
|
+
end
|
12
|
+
def test_iterator2
|
13
|
+
# TODO: assert that this calls 'warn'?
|
14
|
+
iter = Iterator.new(0, :+, 1)
|
15
|
+
assert_equal([1,2,3,4,5], iter.take(5))
|
16
|
+
end
|
17
|
+
end
|
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.
|
4
|
+
version: 1.0.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Matthew Kerwin
|
@@ -53,7 +53,6 @@ files:
|
|
53
53
|
- lib/mug/iterator.rb
|
54
54
|
- lib/mug/iterator/for.rb
|
55
55
|
- lib/mug/iterator/method.rb
|
56
|
-
- lib/mug/iterator_c.rb
|
57
56
|
- lib/mug/loop-with.rb
|
58
57
|
- lib/mug/main.rb
|
59
58
|
- lib/mug/matchdata.rb
|
@@ -91,6 +90,7 @@ files:
|
|
91
90
|
- test/test-hashwhen.rb
|
92
91
|
- test/test-iterator-for.rb
|
93
92
|
- test/test-iterator-method.rb
|
93
|
+
- test/test-iterator.rb
|
94
94
|
- test/test-loop-with.rb
|
95
95
|
- test/test-matchdata_each.rb
|
96
96
|
- test/test-matchdata_hash.rb
|
@@ -127,36 +127,37 @@ signing_key:
|
|
127
127
|
specification_version: 4
|
128
128
|
summary: 'MUG: Matty''s Ultimate Gem'
|
129
129
|
test_files:
|
130
|
-
- test/test-
|
131
|
-
- test/test-
|
132
|
-
- test/test-
|
130
|
+
- test/test-fragile-method-chain.rb
|
131
|
+
- test/test-maybe.rb
|
132
|
+
- test/test-hashop.rb
|
133
|
+
- test/test-not.rb
|
134
|
+
- test/test-loop-with.rb
|
135
|
+
- test/test-counts.rb
|
136
|
+
- test/test-array-minus.rb
|
137
|
+
- test/test-affix.rb
|
138
|
+
- test/test-bool.rb
|
139
|
+
- test/test-alias.rb
|
133
140
|
- test/test-rexproc.rb
|
141
|
+
- test/test-hashmerge.rb
|
142
|
+
- test/test-array-samples.rb
|
134
143
|
- test/test-tau.rb
|
135
|
-
- test/test-
|
136
|
-
- test/test-
|
144
|
+
- test/test-apply.rb
|
145
|
+
- test/test-any-and-all.rb
|
146
|
+
- test/test-clamp.rb
|
147
|
+
- test/test-iterator-for.rb
|
137
148
|
- test/test-bittest.rb
|
138
149
|
- test/test-hashwhen.rb
|
150
|
+
- test/test-negativity.rb
|
151
|
+
- test/test-array-delete_all.rb
|
152
|
+
- test/test-iterator.rb
|
139
153
|
- test/test-matchdata_hash.rb
|
140
|
-
- test/test-not.rb
|
141
|
-
- test/test-array-extend.rb
|
142
|
-
- test/test-iterator-for.rb
|
143
|
-
- test/test-top.rb
|
144
|
-
- test/test-and-or.rb
|
145
|
-
- test/test-array-to_proc.rb
|
146
|
-
- test/test-affix.rb
|
147
|
-
- test/test-hashmerge.rb
|
148
|
-
- test/test-any-and-all.rb
|
149
154
|
- test/test-self.rb
|
150
|
-
- test/test-
|
151
|
-
- test/test-counts.rb
|
152
|
-
- test/test-negativity.rb
|
153
|
-
- test/test-bool.rb
|
155
|
+
- test/test-and-or.rb
|
154
156
|
- test/test-hashmap.rb
|
155
|
-
- test/test-
|
156
|
-
- test/test-
|
157
|
-
- test/test-
|
158
|
-
- test/test-hashop.rb
|
157
|
+
- test/test-enumerable-hash-like.rb
|
158
|
+
- test/test-time.rb
|
159
|
+
- test/test-matchdata_each.rb
|
159
160
|
- test/test-iterator-method.rb
|
160
|
-
- test/test-
|
161
|
-
- test/test-
|
162
|
-
- test/test-
|
161
|
+
- test/test-array-extend.rb
|
162
|
+
- test/test-top.rb
|
163
|
+
- test/test-array-to_proc.rb
|
data/lib/mug/iterator_c.rb
DELETED
@@ -1,44 +0,0 @@
|
|
1
|
-
|
2
|
-
#
|
3
|
-
# A special class of Enumerator that repeatedly invokes a method.
|
4
|
-
#
|
5
|
-
# Initially the method is send to the given +obj+, but subsequent
|
6
|
-
# invocations are sent to the result of the previous invocation.
|
7
|
-
#
|
8
|
-
# Example:
|
9
|
-
#
|
10
|
-
# 0.iter_for(:next).take(5) #=> [0,1,2,3,4]
|
11
|
-
#
|
12
|
-
class Iterator < Enumerator
|
13
|
-
#
|
14
|
-
# Creates a new Iterator for method +meth+, to be
|
15
|
-
# called initially on object +obj+.
|
16
|
-
#
|
17
|
-
# All method calls will have +args+ as parameters.
|
18
|
-
#
|
19
|
-
def initialize obj, meth, *args
|
20
|
-
super() do |y|
|
21
|
-
loop do
|
22
|
-
y << obj
|
23
|
-
obj = obj.send(meth, *args)
|
24
|
-
end
|
25
|
-
end
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
=begin
|
30
|
-
Copyright (c) 2013, Matthew Kerwin <matthew@kerwin.net.au>
|
31
|
-
|
32
|
-
Permission to use, copy, modify, and/or distribute this software for any
|
33
|
-
purpose with or without fee is hereby granted, provided that the above
|
34
|
-
copyright notice and this permission notice appear in all copies.
|
35
|
-
|
36
|
-
THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
37
|
-
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
38
|
-
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
39
|
-
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
40
|
-
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
41
|
-
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
42
|
-
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
43
|
-
=end
|
44
|
-
|