non_empty_array 1.1.0 → 1.1.1
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 +22 -6
- data/lib/non_empty_array.rb +3 -1
- data/non_empty_array.gemspec +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ac28ce826c76708775a75a8d582ecb0d9fb704a021f7a7de1e3723144e1203f0
|
4
|
+
data.tar.gz: fd08cdeaf999b788888c79486bd98d95c2b674fb5d3ea406d3d284cea6c06578
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b9d39b0aa03cb003f34e108fe6bf0ef2dbd65a1bba04880e78482959b15f8e24807184b961ee171c5e57bcc461b2937cc97fb8e4dd02e681213bc2f8f6901532
|
7
|
+
data.tar.gz: 3c944d6220ee3bb008b25ce66a0c8cd407a4fecbe54e4c1df98abbc5351bbb989349839a917c03d6c4b9e85ccbef68c07818b5a2f9e0adc041e156fe5c6a6090
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -2,13 +2,19 @@
|
|
2
2
|
|
3
3
|
# NonEmptyArray
|
4
4
|
|
5
|
-
An [enumerable](https://ruby-doc.org/core-2.7.1/Enumerable.html) which is guaranteed to
|
5
|
+
An [enumerable](https://ruby-doc.org/core-2.7.1/Enumerable.html) which is guaranteed to have at least one element. E.g., `#first`
|
6
6
|
will never fail.
|
7
7
|
|
8
|
-
|
8
|
+
These four methods give non-empty-aware access:
|
9
9
|
|
10
|
-
|
10
|
+
### Always succeed
|
11
|
+
|
12
|
+
* `#first`
|
11
13
|
* `#last`
|
14
|
+
|
15
|
+
### May return an empty Array
|
16
|
+
|
17
|
+
* `#tail`
|
12
18
|
* `#all_but_last`
|
13
19
|
|
14
20
|
And one method for mutating the list:
|
@@ -36,7 +42,16 @@ a = NonEmptyArray.new() # => Ruby error - missing parameter
|
|
36
42
|
```ruby
|
37
43
|
require 'non_empty_array'
|
38
44
|
|
39
|
-
a = NonEmptyArray.new(
|
45
|
+
a = NonEmptyArray.new('1000') # Simplest way to create one
|
46
|
+
a.count() # => 1
|
47
|
+
a.push('2000')
|
48
|
+
a.count() # => 2
|
49
|
+
```
|
50
|
+
|
51
|
+
```ruby
|
52
|
+
require 'non_empty_array'
|
53
|
+
|
54
|
+
a = NonEmptyArray.new(100, [200, 300]). # Creating from both the head and tail
|
40
55
|
|
41
56
|
# Methods from Enumerable
|
42
57
|
a.count() # => 3
|
@@ -44,11 +59,12 @@ a.max() # => 300
|
|
44
59
|
a.to_a() # => [100, 200, 300]
|
45
60
|
|
46
61
|
# Methods specific to NonEmptyArray
|
47
|
-
a.
|
62
|
+
a.first() # => 100 Always succeeds - never returns a "no element" error.
|
63
|
+
a.last() # => 300 Always succeeds
|
48
64
|
a.all_but_last() # => [100, 200] A normal array, which may indeed be empty.
|
49
65
|
a.push('400')
|
50
66
|
a.all_but_last() # => [100, 200, 300]
|
51
67
|
a.tail() # => [200, 300, 400]
|
52
68
|
```
|
53
69
|
|
54
|
-
Influenced by [Haskell's NonEmpty List](https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-List-NonEmpty.html).
|
70
|
+
Influenced by [Haskell's NonEmpty List](https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-List-NonEmpty.html).
|
data/lib/non_empty_array.rb
CHANGED
data/non_empty_array.gemspec
CHANGED