xenum 0.1.1 → 0.1.2
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 +12 -0
- data/lib/xenum/version.rb +1 -1
- data/lib/xenum.rb +54 -0
- 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: a22147054180684998dfbea79223da8a588abf7e0f923e3a1189b76a52d4335e
|
4
|
+
data.tar.gz: bd0b21403fa4d114cbb5139f1c514226648f8de71406da76b6bfb086375346fb
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 94efb08dd1112f2bd67b3380b2e104d48392ff0467f39b28e9830b686de9778d5956a43db1f353b31b371e77585ea836a260b9951c89d5ba91d705ad0617f6e4
|
7
|
+
data.tar.gz: fdbaafde2073b72fbb450dcab5a134d7002cdefab5a89428a3977caaee0aac7eaaf1ef5dd9e6cf4ae82417032ddcc723931579f7a271507ab326634e88fb7329
|
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`
|
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|
|
@@ -92,4 +102,48 @@ module Enumerable
|
|
92
102
|
end
|
93
103
|
end
|
94
104
|
end
|
105
|
+
|
106
|
+
private
|
107
|
+
|
108
|
+
def lazy_insert_neg(index, objs)
|
109
|
+
these = Enumerator === self ? self : self.to_enum
|
110
|
+
queue = []
|
111
|
+
|
112
|
+
(index.abs - 1).times do
|
113
|
+
begin
|
114
|
+
queue << these.next
|
115
|
+
rescue StopIteration
|
116
|
+
raise IndexError, "index #{index} too small; minimum: #{-(queue.size + 1)}"
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
Enumerator.new do |yielder|
|
121
|
+
loop do
|
122
|
+
begin
|
123
|
+
queue << these.next
|
124
|
+
yielder << queue.shift
|
125
|
+
rescue StopIteration
|
126
|
+
break
|
127
|
+
end
|
128
|
+
end
|
129
|
+
objs.each{ |obj| yielder << obj }
|
130
|
+
queue.each{ |this| yielder << this }
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
def lazy_insert_pos(index, objs)
|
135
|
+
these = self
|
136
|
+
Enumerator.new do |yielder|
|
137
|
+
current_index = -1
|
138
|
+
these.each do |this|
|
139
|
+
current_index += 1
|
140
|
+
objs.each{ |obj| yielder << obj } if current_index == index
|
141
|
+
yielder << this
|
142
|
+
end
|
143
|
+
if current_index < index
|
144
|
+
((current_index + 1)...index).each{ yielder << nil }
|
145
|
+
objs.each{ |obj| yielder << obj }
|
146
|
+
end
|
147
|
+
end
|
148
|
+
end
|
95
149
|
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.2
|
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-18 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description:
|
14
14
|
email:
|