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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6bbf9d98e7579cf718f304fdde5a13459d5c3b225383d48d20fba38bc25d6f59
4
- data.tar.gz: 5d448b96d97c4438fbb491ebfe89e8aee222977c0df42edb568972ef41cb1ccb
3
+ metadata.gz: 6702efc29813e62dabd6f74d9c7bc8067b191a79f3b416f6a1336ba20e6497d4
4
+ data.tar.gz: 94676ca2107d67c7680d2e6b963e67c5e19f7ab8386c9808e96c751dcafe34ee
5
5
  SHA512:
6
- metadata.gz: be579d94a4ce9c11aac9e2be34c2542e92e6c6fef2d7624e662cbfcc5f8f277262d58094bb5b245d44d6e6949a85811faf081f1ffed2592dd3bdc8e62d907565
7
- data.tar.gz: 6bdabc0c8e78e6dd35ee33d7af734cc15b86bf66e6d40e26b14804900d211dae1ddbd53c8cc86904cd2c90e723bafb55a3d36083551c84154f5feb4f379a497e
6
+ metadata.gz: f82eac1237b52b16d7950bcaee008811a71ad905e966141893a13a51d04aa8ea9590d47590099f2eb204ec5d2df1a3b75a26db1e3ad5d0a6c159c56bf84776f2
7
+ data.tar.gz: 71df4dec9373651f55a6b5f9abbd4b74f0778b6be2726de4a69735886c0b5528c4e47a46f9d65fb7119b4c3ffa0ee655ce26b092273dfd1ec4b9eedb2bd23b81
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.3)
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`
@@ -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
@@ -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.3"
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|
@@ -45,7 +55,24 @@ module Enumerable
45
55
  end
46
56
  end
47
57
 
48
- def merge_sort(that)
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.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-15 00:00:00.000000000 Z
11
+ date: 2024-03-19 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: