non_empty_array 1.1.0 → 1.1.1

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: c54d118eef824c4fffa241971f7c95b927f24597eab6eddc019a434be045160b
4
- data.tar.gz: '053186e3e45956d92ee38c4a218228daa881c89c73de2e6b2172762b05283a8c'
3
+ metadata.gz: ac28ce826c76708775a75a8d582ecb0d9fb704a021f7a7de1e3723144e1203f0
4
+ data.tar.gz: fd08cdeaf999b788888c79486bd98d95c2b674fb5d3ea406d3d284cea6c06578
5
5
  SHA512:
6
- metadata.gz: b8ebda3c33e1bf55e37f2b62bf62a08b1dd7d542fd7017244085084b5c2dc70df2a6cbb79af5f4885ab7a09bdb21637cce6f42e8ee1702be3df07356337e7fef
7
- data.tar.gz: 6331e7a5f3e9d9bd9c1210c38e538e457ecd7a228dc2676fd53dff4ab2290425ea9eec7e0723721f77838b9cd8ddacd47170a77b1234b3f7c5820471d7c922be
6
+ metadata.gz: b9d39b0aa03cb003f34e108fe6bf0ef2dbd65a1bba04880e78482959b15f8e24807184b961ee171c5e57bcc461b2937cc97fb8e4dd02e681213bc2f8f6901532
7
+ data.tar.gz: 3c944d6220ee3bb008b25ce66a0c8cd407a4fecbe54e4c1df98abbc5351bbb989349839a917c03d6c4b9e85ccbef68c07818b5a2f9e0adc041e156fe5c6a6090
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- non_empty_array (1.1.0)
4
+ non_empty_array (1.1.1)
5
5
  sorbet-runtime (>= 0.5.5890)
6
6
 
7
7
  GEM
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 not be empty. E.g., `#first`
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
- Additionally, three methods which give access:
8
+ These four methods give non-empty-aware access:
9
9
 
10
- * `#tail`
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(100, [200, 300])
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.last() # => 300 Always succeeds - never returns a "no element" error.
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).
@@ -23,7 +23,9 @@ class NonEmptyArray
23
23
 
24
24
  sig { returns(T.untyped) }
25
25
  def last
26
- @tail[-1] || @head
26
+ return @head if @tail.empty?
27
+
28
+ @tail[-1]
27
29
  end
28
30
 
29
31
  sig { returns(T::Array[T.untyped]) }
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'non_empty_array'
5
- spec.version = '1.1.0'
5
+ spec.version = '1.1.1'
6
6
  spec.authors = ['Robb Shecter']
7
7
  spec.email = ['robb@public.law']
8
8
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: non_empty_array
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter