xenum 0.1.0 → 0.1.2
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/Gemfile.lock +1 -1
- data/README.md +38 -3
- data/lib/xenum/version.rb +1 -1
- data/lib/xenum.rb +73 -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
@@ -14,14 +14,49 @@ If bundler is not being used to manage dependencies, install the gem by executin
|
|
14
14
|
|
15
15
|
## Usage
|
16
16
|
|
17
|
-
|
17
|
+
### lazy_flatten
|
18
|
+
|
19
|
+
works like `Array#flatten`, but for all `Enumerable`, and it is lazy
|
18
20
|
|
19
21
|
```ruby
|
20
|
-
|
22
|
+
[
|
23
|
+
3.times,
|
24
|
+
3,
|
25
|
+
4,
|
26
|
+
[5, [6,7]],
|
27
|
+
(8..10),
|
28
|
+
Enumerator.new{|e| n = 10; loop{e << n+=1}}
|
29
|
+
].lazy_flatten.first(15)
|
30
|
+
# [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14]
|
31
|
+
```
|
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
|
+
|
45
|
+
### lazy_product
|
46
|
+
|
47
|
+
works like `Array#product`, but not limited to `Array`
|
48
|
+
|
49
|
+
```ruby
|
50
|
+
3.times.lazy_product(
|
51
|
+
(3..5),
|
52
|
+
[6,7,8]
|
53
|
+
).take(6)
|
21
54
|
# [[0, 3, 6], [0, 3, 7], [0, 3, 8], [0, 4, 6], [0, 4, 7], [0, 4, 8]]
|
22
55
|
```
|
23
56
|
|
24
|
-
|
57
|
+
### merge_sort
|
58
|
+
|
59
|
+
lazily merge sort two sorted `Enumerable`
|
25
60
|
|
26
61
|
```ruby
|
27
62
|
e = (6..8).merge_sort([3,4,5]).merge_sort(3.times)
|
data/lib/xenum/version.rb
CHANGED
data/lib/xenum.rb
CHANGED
@@ -7,6 +7,35 @@ module Xenum
|
|
7
7
|
end
|
8
8
|
|
9
9
|
module Enumerable
|
10
|
+
def lazy_flatten(level = nil)
|
11
|
+
these = self
|
12
|
+
level = -1 if level.nil?
|
13
|
+
|
14
|
+
Enumerator.new do |yielder|
|
15
|
+
level.nil?
|
16
|
+
these.each do |this|
|
17
|
+
if Enumerable === this && level != 0
|
18
|
+
this.lazy_flatten(level - 1).each do |sub|
|
19
|
+
yielder.yield(sub)
|
20
|
+
end
|
21
|
+
next
|
22
|
+
end
|
23
|
+
|
24
|
+
yielder.yield(this)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
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
|
+
|
10
39
|
def lazy_product(*enums)
|
11
40
|
if enums.empty?
|
12
41
|
return Enumerator.new do |yielder|
|
@@ -73,4 +102,48 @@ module Enumerable
|
|
73
102
|
end
|
74
103
|
end
|
75
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
|
76
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:
|