non_empty_array 1.0.0 → 1.1.0

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: 5649876842392722eeb4b0764cf1f7a75045836547b988cf8d56c929e11220f9
4
- data.tar.gz: 178a65237cbac3a32620c4d9eb07021588a47d236dcb23bbe3eb51dba3b67ddf
3
+ metadata.gz: c54d118eef824c4fffa241971f7c95b927f24597eab6eddc019a434be045160b
4
+ data.tar.gz: '053186e3e45956d92ee38c4a218228daa881c89c73de2e6b2172762b05283a8c'
5
5
  SHA512:
6
- metadata.gz: 3dee92ab346f77a421739796263bef093e90f6b00cd9a40bfcae2daac74a36ee31c6fb0e50eb321a7b2be0928f1c731050757a240cd8fc6efb9e96ba370cd92a
7
- data.tar.gz: '0196289c3deb5a81607cb1b9e4f2fba68d3e00e404a3ba2fe9d8b6e02d18245142891eafb9e25caaf8aaf3f61f53f796510a1f5b9cada3c6b0d3d8c0ef60fb1a'
6
+ metadata.gz: b8ebda3c33e1bf55e37f2b62bf62a08b1dd7d542fd7017244085084b5c2dc70df2a6cbb79af5f4885ab7a09bdb21637cce6f42e8ee1702be3df07356337e7fef
7
+ data.tar.gz: 6331e7a5f3e9d9bd9c1210c38e538e457ecd7a228dc2676fd53dff4ab2290425ea9eec7e0723721f77838b9cd8ddacd47170a77b1234b3f7c5820471d7c922be
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- non_empty_array (1.0.0)
4
+ non_empty_array (1.1.0)
5
5
  sorbet-runtime (>= 0.5.5890)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,12 +1,54 @@
1
+ [![Gem Version](https://badge.fury.io/rb/non_empty_array.svg)](https://badge.fury.io/rb/non_empty_array) [![Build Status](https://travis-ci.com/dogweather/non_empty_array.svg?branch=master)](https://travis-ci.com/dogweather/non_empty_array)
2
+
1
3
  # NonEmptyArray
2
4
 
3
- An enumerable which is guaranteed to not be empty.
5
+ An [enumerable](https://ruby-doc.org/core-2.7.1/Enumerable.html) which is guaranteed to not be empty. E.g., `#first`
6
+ will never fail.
4
7
 
5
- Additionally, two methods which give access:
8
+ Additionally, three methods which give access:
6
9
 
7
- * `last()`
8
- * `all_but_last()`
10
+ * `#tail`
11
+ * `#last`
12
+ * `#all_but_last`
9
13
 
10
14
  And one method for mutating the list:
11
15
 
12
- * `push()`
16
+ * `#push`
17
+
18
+ ## Why is this useful?
19
+
20
+ Sometimes I know that an Array isn't empty. In fact, it should never be empty, because
21
+ otherwise, it means the object was set up incorrectly. The usual way to handle this is
22
+ to check the array's length, or check for nil, and throw an exception if, for some
23
+ reason, the Array _is_ empty.
24
+
25
+ This `NonEmptyArray` approach saves this unnecessary work by moving the non-emptyness
26
+ into the type system, letting Ruby check and prevent misuse. I.e., this class is
27
+ designed so that it's impossible for it to be empty. And it has accessors like `#last`
28
+ which always returns an element - it can never fail:
29
+
30
+ ```ruby
31
+ require 'non_empty_array'
32
+
33
+ a = NonEmptyArray.new() # => Ruby error - missing parameter
34
+ ```
35
+
36
+ ```ruby
37
+ require 'non_empty_array'
38
+
39
+ a = NonEmptyArray.new(100, [200, 300])
40
+
41
+ # Methods from Enumerable
42
+ a.count() # => 3
43
+ a.max() # => 300
44
+ a.to_a() # => [100, 200, 300]
45
+
46
+ # Methods specific to NonEmptyArray
47
+ a.last() # => 300 Always succeeds - never returns a "no element" error.
48
+ a.all_but_last() # => [100, 200] A normal array, which may indeed be empty.
49
+ a.push('400')
50
+ a.all_but_last() # => [100, 200, 300]
51
+ a.tail() # => [200, 300, 400]
52
+ ```
53
+
54
+ Influenced by [Haskell's NonEmpty List](https://hackage.haskell.org/package/base-4.14.0.0/docs/Data-List-NonEmpty.html).
@@ -18,7 +18,7 @@ class NonEmptyArray
18
18
 
19
19
  sig { override.params(block: T.untyped).void }
20
20
  def each(&block)
21
- _all.each(&block)
21
+ all_elements.each(&block)
22
22
  end
23
23
 
24
24
  sig { returns(T.untyped) }
@@ -28,9 +28,12 @@ class NonEmptyArray
28
28
 
29
29
  sig { returns(T::Array[T.untyped]) }
30
30
  def all_but_last
31
- T.must(_all.slice(0..-2))
31
+ T.must(all_elements.slice(0..-2))
32
32
  end
33
33
 
34
+ sig { returns(T::Array[T.untyped]) }
35
+ attr_reader :tail
36
+
34
37
  sig { params(element: T.untyped).returns(NonEmptyArray) }
35
38
  def push(element)
36
39
  @tail.push(element)
@@ -40,7 +43,7 @@ class NonEmptyArray
40
43
  private
41
44
 
42
45
  sig { returns(T::Array[T.untyped]) }
43
- def _all
46
+ def all_elements
44
47
  [@head] + @tail
45
48
  end
46
49
  end
@@ -2,12 +2,12 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = 'non_empty_array'
5
- spec.version = '1.0.0'
5
+ spec.version = '1.1.0'
6
6
  spec.authors = ['Robb Shecter']
7
7
  spec.email = ['robb@public.law']
8
8
 
9
9
  spec.summary = 'An ordered list guaranteed to have at least one element.'
10
- spec.description = "Inspired by Haskell's NonEmptyList"
10
+ spec.description = "Inspired by Haskell's NonEmpty list"
11
11
  spec.homepage = 'https://github.com/dogweather/non_empty_array'
12
12
  spec.license = 'MIT'
13
13
  spec.required_ruby_version = Gem::Requirement.new('>= 2.6.0')
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.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Robb Shecter
@@ -38,7 +38,7 @@ dependencies:
38
38
  - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: 0.5.5890
41
- description: Inspired by Haskell's NonEmptyList
41
+ description: Inspired by Haskell's NonEmpty list
42
42
  email:
43
43
  - robb@public.law
44
44
  executables: []