-
Notifications
You must be signed in to change notification settings - Fork 135
X NonEmptyList
An immutable, shared memory, List which is guaranteed to have at least one value.
NonEmptyList<Integer> oneOrMore = NonEmptyList.of(1,2,3);
NonEmptyList allows developers to offer more safety and certainty in their APIs - use it where you need to ensure that clients provide at least one value (this can be a useful tool in your own applications to ensure illegal states are unrepresentable).
Example : Update the addresses associated with a customer (where at least one address must be present). With JDK list
public void updateAddresses(User customer, List<Address> addresses){
if(address.size()==0){
throw NoSuchElementException("No addresses supplied");
}
}
or with NonEmptyList
public void updateAddresses(User customer, NonEmptyList<Address> addresses){
//no need to throw any RuntimeException, the compiler makes sure we have at least one address
}
Unlike other ImmutableLists in Cyclops such as Seq, LazySeq and Vector - we can safely directly access the first (and only the first) value on a NonEmptyList.
Integer first = nonEmptyList.head();
The head() method is not available directly on other ImmutableList types (and can be accessed only indirectly and safely via a fold).
oops - my bad