xenum 0.1.1 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/README.md +16 -0
- data/lib/xenum/version.rb +1 -1
- data/lib/xenum.rb +70 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6702efc29813e62dabd6f74d9c7bc8067b191a79f3b416f6a1336ba20e6497d4
|
4
|
+
data.tar.gz: 94676ca2107d67c7680d2e6b963e67c5e19f7ab8386c9808e96c751dcafe34ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f82eac1237b52b16d7950bcaee008811a71ad905e966141893a13a51d04aa8ea9590d47590099f2eb204ec5d2df1a3b75a26db1e3ad5d0a6c159c56bf84776f2
|
7
|
+
data.tar.gz: 71df4dec9373651f55a6b5f9abbd4b74f0778b6be2726de4a69735886c0b5528c4e47a46f9d65fb7119b4c3ffa0ee655ce26b092273dfd1ec4b9eedb2bd23b81
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -30,6 +30,18 @@ works like `Array#flatten`, but for all `Enumerable`, and it is lazy
|
|
30
30
|
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
|
31
31
|
```
|
32
32
|
|
33
|
+
### lazy_insert
|
34
|
+
|
35
|
+
works like `Array#insert`, but not limited to `Array`
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
10.times.lazy_insert(3, 'a', 'b').to_a
|
39
|
+
# [0, 1, 2, "a", "b", 3, 4, 5, 6, 7, 8, 9]
|
40
|
+
|
41
|
+
10.times.lazy_insert(-3, 'a', 'b').to_a
|
42
|
+
# [0, 1, 2, 3, 4, 5, 6, 7, "a", "b", 8, 9]
|
43
|
+
```
|
44
|
+
|
33
45
|
### lazy_product
|
34
46
|
|
35
47
|
works like `Array#product`, but not limited to `Array`
|
@@ -55,4 +67,8 @@ e.take(5)
|
|
55
67
|
|
56
68
|
[(6..8), [3,4,5], 3.times].reduce(&:merge_sort).to_a
|
57
69
|
# [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
70
|
+
|
71
|
+
# same result but faster
|
72
|
+
(6..8).merge_sort([3,4,5], 3.times).to_a
|
73
|
+
# [0, 1, 2, 3, 4, 5, 6, 7, 8]
|
58
74
|
```
|
data/lib/xenum/version.rb
CHANGED
data/lib/xenum.rb
CHANGED
@@ -26,6 +26,16 @@ module Enumerable
|
|
26
26
|
end
|
27
27
|
end
|
28
28
|
|
29
|
+
def lazy_insert(index, *objs)
|
30
|
+
return self if objs.empty?
|
31
|
+
|
32
|
+
if index >= 0
|
33
|
+
lazy_insert_pos(index, objs)
|
34
|
+
else
|
35
|
+
lazy_insert_neg(index, objs)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
|
29
39
|
def lazy_product(*enums)
|
30
40
|
if enums.empty?
|
31
41
|
return Enumerator.new do |yielder|
|
@@ -45,7 +55,24 @@ module Enumerable
|
|
45
55
|
end
|
46
56
|
end
|
47
57
|
|
48
|
-
def merge_sort(
|
58
|
+
def merge_sort(*enums)
|
59
|
+
case enums.size
|
60
|
+
when 0
|
61
|
+
self
|
62
|
+
when 1
|
63
|
+
_merge_sort(enums[0])
|
64
|
+
else
|
65
|
+
enums2 = enums.pop((enums.size / 2) - 1)
|
66
|
+
enum2 = enums.pop
|
67
|
+
e2 = enum2.merge_sort(*enums2)
|
68
|
+
e1 = merge_sort(*enums)
|
69
|
+
e1.merge_sort(e2)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def _merge_sort(that)
|
49
76
|
this = Enumerator === self ? self : self.to_enum
|
50
77
|
that = Enumerator === that ? that : that.to_enum
|
51
78
|
|
@@ -92,4 +119,46 @@ module Enumerable
|
|
92
119
|
end
|
93
120
|
end
|
94
121
|
end
|
122
|
+
|
123
|
+
def lazy_insert_neg(index, objs)
|
124
|
+
these = Enumerator === self ? self : self.to_enum
|
125
|
+
queue = []
|
126
|
+
|
127
|
+
(index.abs - 1).times do
|
128
|
+
begin
|
129
|
+
queue << these.next
|
130
|
+
rescue StopIteration
|
131
|
+
raise IndexError, "index #{index} too small; minimum: #{-(queue.size + 1)}"
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
Enumerator.new do |yielder|
|
136
|
+
loop do
|
137
|
+
begin
|
138
|
+
queue << these.next
|
139
|
+
yielder << queue.shift
|
140
|
+
rescue StopIteration
|
141
|
+
break
|
142
|
+
end
|
143
|
+
end
|
144
|
+
objs.each{ |obj| yielder << obj }
|
145
|
+
queue.each{ |this| yielder << this }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
|
149
|
+
def lazy_insert_pos(index, objs)
|
150
|
+
these = self
|
151
|
+
Enumerator.new do |yielder|
|
152
|
+
current_index = -1
|
153
|
+
these.each do |this|
|
154
|
+
current_index += 1
|
155
|
+
objs.each{ |obj| yielder << obj } if current_index == index
|
156
|
+
yielder << this
|
157
|
+
end
|
158
|
+
if current_index < index
|
159
|
+
((current_index + 1)...index).each{ yielder << nil }
|
160
|
+
objs.each{ |obj| yielder << obj }
|
161
|
+
end
|
162
|
+
end
|
163
|
+
end
|
95
164
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: xenum
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- ken
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-03-
|
11
|
+
date: 2024-03-19 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|