xenum 0.1.1 → 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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bbf9d98e7579cf718f304fdde5a13459d5c3b225383d48d20fba38bc25d6f59
4
- data.tar.gz: 5d448b96d97c4438fbb491ebfe89e8aee222977c0df42edb568972ef41cb1ccb
3
+ metadata.gz: a22147054180684998dfbea79223da8a588abf7e0f923e3a1189b76a52d4335e
4
+ data.tar.gz: bd0b21403fa4d114cbb5139f1c514226648f8de71406da76b6bfb086375346fb
5
5
  SHA512:
6
- metadata.gz: be579d94a4ce9c11aac9e2be34c2542e92e6c6fef2d7624e662cbfcc5f8f277262d58094bb5b245d44d6e6949a85811faf081f1ffed2592dd3bdc8e62d907565
7
- data.tar.gz: 6bdabc0c8e78e6dd35ee33d7af734cc15b86bf66e6d40e26b14804900d211dae1ddbd53c8cc86904cd2c90e723bafb55a3d36083551c84154f5feb4f379a497e
6
+ metadata.gz: 94efb08dd1112f2bd67b3380b2e104d48392ff0467f39b28e9830b686de9778d5956a43db1f353b31b371e77585ea836a260b9951c89d5ba91d705ad0617f6e4
7
+ data.tar.gz: fdbaafde2073b72fbb450dcab5a134d7002cdefab5a89428a3977caaee0aac7eaaf1ef5dd9e6cf4ae82417032ddcc723931579f7a271507ab326634e88fb7329
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- xenum (0.1.0)
4
+ xenum (0.1.1)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Xenum
4
- VERSION = "0.1.1"
4
+ VERSION = "0.1.2"
5
5
  end
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.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-15 00:00:00.000000000 Z
11
+ date: 2024-03-18 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: