NonEmptyList

final case class NonEmptyList[A](head: A, tail: List[A])

An ordered list-type object that requires there to always be at least one element present, ruling out the possibility of unsafely accessing the head element.

Type Params
A

The type of element to be stored in the list.

Companion
object
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any

Value members

Concrete methods

def ++(other: NonEmptyList[A]): NonEmptyList[A]

Concatenate two NonEmptyList's together

Concatenate two NonEmptyList's together

Value Params
other

A second NonEmptyList of the same type

Returns

A new NonEmptyList containing the elements of both lists

def ++(other: List[A]): NonEmptyList[A]

Concatenate a NonEmptyList with a List

Concatenate a NonEmptyList with a List

Value Params
other

A List of the same type

Returns

A new NonEmptyList containing the elements of both lists

def :+(next: A): NonEmptyList[A]

Append an element

Append an element

Value Params
next

The next element of the same type

Returns

NonEmptyList[A]

def ::(first: A): NonEmptyList[A]

Prepend an element

Prepend an element

Value Params
first

The new head element of the same type

Returns

NonEmptyList[A]

def exists(p: A => Boolean): Boolean

List find, but only returns a Boolean indicating if an element matching the predicate was found.

List find, but only returns a Boolean indicating if an element matching the predicate was found.

Value Params
p

Predicate function

Returns

Boolean

def find(p: A => Boolean): Option[A]

Search the NonEmptyList using a predicate and return the first element that matches

Search the NonEmptyList using a predicate and return the first element that matches

Value Params
p

Predicate, returns the first elements for which this predicate holds true

Returns

Optional A, if no match can be found None is returned.

def first: A

Alias for head

Alias for head

Returns

A

def flatMap[B](f: A => NonEmptyList[B]): NonEmptyList[B]

Apply a function f to each element of the list to produce a new list. Differs from map because f produces another NonEmptyList, which is then flattened. Useful in monadic comprehensions.

Apply a function f to each element of the list to produce a new list. Differs from map because f produces another NonEmptyList, which is then flattened. Useful in monadic comprehensions.

Type Params
B

Resultant type of the new NonEmptyList

Value Params
f

function to apply to each element

Returns

A NonEmptyList of a potentially different type

Example

NonEmptyList(1, 2, 3).flatMap(i => NonEmptyList(i * 10)) results in NonEmptyList(10, 20, 30)

def foldLeft[Z](acc: Z)(f: (Z, A) => Z): Z

foldLeft differs from reduce it two important ways:

foldLeft differs from reduce it two important ways:

  1. It has an initial value onto which all other values are applied
  2. It does not require the result type to be the same as the list type.
Type Params
Z

The accumulator type

Value Params
acc

The initial accumulator value to accumulate against

f

A function for combining the accumulator and the next value

Returns

the final accumulated value

Example

NonEmptyList(1, 2, 3)("")((a, b) => a + b) results in "123"

def forall(p: A => Boolean): Boolean

Checks that a predicate holds for all elements

Checks that a predicate holds for all elements

Value Params
p

Predicate function

Returns

Boolean

def last: A

Returns the last element in the list

Returns the last element in the list

Returns

A

def length: Int

A count of the elements in the list

A count of the elements in the list

Returns

Int

def map[B](f: A => B): NonEmptyList[B]

Apply a function f to each element of the list to produce a new list.

Apply a function f to each element of the list to produce a new list.

Type Params
B

Resultant type of the new NonEmptyList

Value Params
f

function to apply to each element

Returns

A NonEmptyList of a potentially different type

Example

NonEmptyList(1, 2, 3).map(_ * 10) results in NonEmptyList(10, 20, 30)

def mkString: String

Delegates to mkString(separator: String): String

Delegates to mkString(separator: String): String

Returns

String

def mkString(separator: String): String

Converts the list into a String

Converts the list into a String

Value Params
separator

A string to add between the elements

Returns

String

def reduce(f: (A, A) => A): A
Value Params
f

a function for combining to A's into a single A

Returns

The final A value

Example

NonEmptyList(1, 2, 3)((a, b) => a + b) results in 6

Reverse the order of the list

Reverse the order of the list

Returns

NonEmptyList[A]

def toList: List[A]

Converts the NonEmptyList back to a regular List.

Converts the NonEmptyList back to a regular List.

override def toString: String
Definition Classes
Any
def zip[B](other: NonEmptyList[B]): NonEmptyList[(A, B)]

Takes two NonEmptyLists and creates a new NonEmptyList of the elements of both inputs tupled together.

Takes two NonEmptyLists and creates a new NonEmptyList of the elements of both inputs tupled together.

Type Params
B

The type of the second NonEmptyList

Value Params
other

The second NonEmptyList to zip with.

Returns

NonEmptyList[(A, B)]

Example

NonEmptyList("a", "b", "c").zip(NonEmptyList(1, 2, 3)) results in NonEmptyList(("a", 1), ("b", 2), ("c", 3))

def zipWithIndex: NonEmptyList[(A, Int)]
Example

NonEmptyList("a", "b", "c").zipWithIndex results in NonEmptyList(("a", 0), ("b", 1), ("c",2))

Inherited methods

def productElementNames: Iterator[String]
Inherited from
Product
def productIterator: Iterator[Any]
Inherited from
Product