array_utils 0.0.1 → 0.0.3
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/lib/array_utils.rb +58 -0
- metadata +8 -11
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9328e017c9d5987494e2d6de4ef7c2fdc626c0b351ea7cddaa9a55e6482ec1bc
|
4
|
+
data.tar.gz: 06dd470410326a939e0ee7f5cafe7e7c8e6c8985df17334a65a0875b03c36662
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 4af0682ffe381b43b60d824a1e45ddfdb0c0990707c3c638c3eb5ce79972332e2bbef39d6b610d80dd100e679a329535795dcfc09da1be1e0e622efefbcb3fd4
|
7
|
+
data.tar.gz: ae6be72e010f74407247aceadfb17ae93b1b2bf23c560da8be5d65ef2800130b9f4a37b8faf0e0ae6a5df0e3e403626e816b12d400df32de53126396468e5762
|
data/lib/array_utils.rb
CHANGED
@@ -25,4 +25,62 @@ class Array
|
|
25
25
|
|
26
26
|
partition(&:odd?)
|
27
27
|
end
|
28
|
+
|
29
|
+
# Determines whether an array is "sparse," meaning it contains a significant
|
30
|
+
# proportion
|
31
|
+
# of `nil` values compared to its size.
|
32
|
+
#
|
33
|
+
# @param threshold [Float] The threshold ratio (default: 0.5).
|
34
|
+
# If the proportion of `nil` values to the array's size exceeds this
|
35
|
+
# threshold, the array is considered sparse.
|
36
|
+
#
|
37
|
+
# @return [Boolean] `true` if the array is sparse, `false` otherwise.
|
38
|
+
#
|
39
|
+
# @example Basic usage:
|
40
|
+
# [1, nil, nil, 4, nil].sparse? # => true (more than 50% nil values)
|
41
|
+
#
|
42
|
+
# @example Custom threshold:
|
43
|
+
# [1, nil, nil, 4, 5].sparse?(0.8) # => false (less than 80% nil values)
|
44
|
+
#
|
45
|
+
# @example Non-sparse array:
|
46
|
+
# [1, 2, 3, 4, 5].sparse? # => false (no nil values)
|
47
|
+
def sparse?(threshold = 0.5)
|
48
|
+
flatten.count(nil).to_f / flatten.size > threshold
|
49
|
+
end
|
50
|
+
|
51
|
+
# Returns a new array where all `nil` values are removed.
|
52
|
+
#
|
53
|
+
# If an element is an array, it is compacted (removing `nil` values)
|
54
|
+
# before being added. This makes it more effective than the default `#compact`
|
55
|
+
# method which only works with 1D arrays.
|
56
|
+
#
|
57
|
+
# @return [Array] A new array with `nil` values removed.
|
58
|
+
def densify
|
59
|
+
densified_array = []
|
60
|
+
|
61
|
+
each do |elem|
|
62
|
+
next unless elem
|
63
|
+
|
64
|
+
if elem.is_a? Array
|
65
|
+
nested = elem.densify
|
66
|
+
# skip empty arrays after removing nil values
|
67
|
+
densified_array << nested unless nested.empty?
|
68
|
+
else
|
69
|
+
densified_array << elem
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
densified_array
|
74
|
+
end
|
75
|
+
|
76
|
+
# Modifies the array in place by removing all `nil` values.
|
77
|
+
#
|
78
|
+
# If an element is an array, it is compacted (removing `nil` values)
|
79
|
+
# before being added. This makes it more effective than the default `#compact`
|
80
|
+
# method which only works with 1D arrays.
|
81
|
+
#
|
82
|
+
# @return [self] The modified array.
|
83
|
+
def densify!
|
84
|
+
replace(densify)
|
85
|
+
end
|
28
86
|
end
|
metadata
CHANGED
@@ -1,24 +1,21 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: array_utils
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Maxwell Nana Forson
|
8
8
|
bindir: bin
|
9
9
|
cert_chain: []
|
10
|
-
date: 2025-03-
|
10
|
+
date: 2025-03-26 00:00:00.000000000 Z
|
11
11
|
dependencies: []
|
12
|
-
description: |
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
appending to the end of the array when such action is performed.
|
12
|
+
description: |2
|
13
|
+
A simple Ruby gem that handles the issue with Ruby’s Array class when
|
14
|
+
inserting an element at an index greater than the current array size.
|
15
|
+
It handles this by intelligently appending the result to the end of the
|
16
|
+
array instead of creating a sparse array.
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
There are other useful methods added on top of the Array class for common tasks.
|
18
|
+
It also includes some other useful Array methods for common uses.
|
22
19
|
email: maxwellnanaforson@gmail.com
|
23
20
|
executables: []
|
24
21
|
extensions: []
|