A data type which represents a non empty list of A, with single element (head) and optional structure (tail).
A data type which represents a non empty list of A, with single element (head) and optional structure (tail).
- Companion
- object
Value members
Concrete methods
Alias for append
Alias for append
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3)
scala> nel :+ 4
res0: cats.data.NonEmptyList[Int] = NonEmptyList(1, 2, 3, 4)
Alias for concatNel
Alias for concatNel
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3)
scala> nel ::: NonEmptyList.of(4, 5)
res0: cats.data.NonEmptyList[Int] = NonEmptyList(1, 2, 3, 4, 5)
Builds a new List
by applying a partial function to
all the elements from this NonEmptyList
on which the function is defined
Builds a new List
by applying a partial function to
all the elements from this NonEmptyList
on which the function is defined
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.collect { case v if v < 3 => v }
res0: scala.collection.immutable.List[Int] = List(1, 2)
scala> nel.collect {
| case v if v % 2 == 0 => "even"
| case _ => "odd"
| }
res1: scala.collection.immutable.List[String] = List(odd, even, odd, even, odd)
Remove duplicates. Duplicates are checked using Order[_]
instance.
Remove duplicates. Duplicates are checked using Order[_]
instance.
Check whether at least one element satisfies the predicate
Check whether at least one element satisfies the predicate
Remove elements not matching the predicate
Remove elements not matching the predicate
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.filter(_ < 3)
res0: scala.collection.immutable.List[Int] = List(1, 2)
Remove elements matching the predicate
Remove elements matching the predicate
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.filterNot(_ < 3)
res0: scala.collection.immutable.List[Int] = List(3, 4, 5)
Find the first element matching the predicate, if one exists
Find the first element matching the predicate, if one exists
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given mapping function.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given mapping function.
scala> import scala.collection.immutable.SortedMap
scala> import cats.data.NonEmptyList
scala> import cats.implicits._
scala> val nel = NonEmptyList.of(12, -2, 3, -5)
scala> val expectedResult = SortedMap(false -> NonEmptyList.of(-2, -5), true -> NonEmptyList.of(12, 3))
scala> val result = nel.groupBy(_ >= 0)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given mapping function.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given mapping function.
scala> import cats.data.{NonEmptyList, NonEmptyMap}
scala> import cats.implicits._
scala> val nel = NonEmptyList.of(12, -2, 3, -5)
scala> val expectedResult = NonEmptyMap.of(false -> NonEmptyList.of(-2, -5), true -> NonEmptyList.of(12, 3))
scala> val result = nel.groupByNem(_ >= 0)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
And each element in a group is transformed into a value of type B
using the mapping function.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
And each element in a group is transformed into a value of type B
using the mapping function.
scala> import scala.collection.immutable.SortedMap
scala> import cats.data.NonEmptyList
scala> import cats.implicits._
scala> val nel = NonEmptyList.of(12, -2, 3, -5)
scala> val expectedResult = SortedMap(false -> NonEmptyList.of("-2", "-5"), true -> NonEmptyList.of("12", "3"))
scala> val result = nel.groupMap(_ >= 0)(_.toString)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
And each element in a group is transformed into a value of type B
using the mapping function.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
And each element in a group is transformed into a value of type B
using the mapping function.
scala> import cats.data.{NonEmptyList, NonEmptyMap}
scala> import cats.implicits._
scala> val nel = NonEmptyList.of(12, -2, 3, -5)
scala> val expectedResult = NonEmptyMap.of(false -> NonEmptyList.of("-2", "-5"), true -> NonEmptyList.of("12", "3"))
scala> val result = nel.groupMapNem(_ >= 0)(_.toString)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using their Semigroup
.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using their Semigroup
.
scala> import scala.collection.immutable.SortedMap
scala> import cats.data.NonEmptyList
scala> import cats.implicits._
scala> val nel = NonEmptyList.of("Hello", "World", "Goodbye", "World")
scala> val expectedResult = SortedMap("goodbye" -> 1, "hello" -> 1, "world" -> 2)
scala> val result = nel.groupMapReduce(_.trim.toLowerCase)(_ => 1)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using their Semigroup
.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using their Semigroup
.
scala> import cats.data.{NonEmptyList, NonEmptyMap}
scala> import cats.implicits._
scala> val nel = NonEmptyList.of("Hello", "World", "Goodbye", "World")
scala> val expectedResult = NonEmptyMap.of("goodbye" -> 1, "hello" -> 1, "world" -> 2)
scala> val result = nel.groupMapReduceNem(_.trim.toLowerCase)(_ => 1)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using the provided combine function.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using the provided combine function.
scala> import scala.collection.immutable.SortedMap
scala> import cats.data.NonEmptyList
scala> import cats.implicits._
scala> val nel = NonEmptyList.of("Hello", "World", "Goodbye", "World")
scala> val expectedResult = SortedMap("goodbye" -> 1, "hello" -> 1, "world" -> 2)
scala> val result = nel.groupMapReduceWith(_.trim.toLowerCase)(_ => 1)(_ + _)
scala> result === expectedResult
res0: Boolean = true
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using the provided combine function.
Groups elements inside this NonEmptyList
according to the Order
of the keys produced by the given key function.
Then each element in a group is transformed into a value of type B
using the mapping function.
And finally they are all reduced into a single value
using the provided combine function.
scala> import cats.data.{NonEmptyList, NonEmptyMap}
scala> import cats.implicits._
scala> val nel = NonEmptyList.of("Hello", "World", "Goodbye", "World")
scala> val expectedResult = NonEmptyMap.of("goodbye" -> 1, "hello" -> 1, "world" -> 2)
scala> val result = nel.groupMapReduceWithNem(_.trim.toLowerCase)(_ => 1)(_ + _)
scala> result === expectedResult
res0: Boolean = true
Partitions elements in fixed size NonEmptyList
s.
Partitions elements in fixed size NonEmptyList
s.
scala> import cats.data.NonEmptyList
scala> import cats.implicits._
scala> val nel = NonEmptyList.of(12, -2, 3, -5)
scala> val expectedResult = List(NonEmptyList.of(12, -2), NonEmptyList.of(3, -5))
scala> val result = nel.grouped(2)
scala> result.toList === expectedResult
res0: Boolean = true
Selects all elements except the last
Selects all elements except the last
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.init
res0: scala.collection.immutable.List[Int] = List(1, 2, 3, 4)
Selects the last element
Selects the last element
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.last
res0: Int = 5
Reverse this NonEmptyList
.
Reverse this NonEmptyList
.
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3)
scala> nel.reverse
res0: cats.data.NonEmptyList[Int] = NonEmptyList(3, 2, 1)
The size of this NonEmptyList
The size of this NonEmptyList
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.size
res0: Int = 5
Sorts this NonEmptyList
according to an Order
on transformed B
from A
Sorts this NonEmptyList
according to an Order
on transformed B
from A
scala> import cats.data.NonEmptyList
scala> import cats.instances.int._
scala> val nel = NonEmptyList.of(('a', 4), ('z', 1), ('e', 22))
scala> nel.sortBy(_._2)
res0: cats.data.NonEmptyList[(Char, Int)] = NonEmptyList((z,1), (a,4), (e,22))
Sorts this NonEmptyList
according to an Order
Sorts this NonEmptyList
according to an Order
scala> import cats.data.NonEmptyList
scala> import cats.instances.int._
scala> val nel = NonEmptyList.of(12, 4, 3, 9)
scala> nel.sorted
res0: cats.data.NonEmptyList[Int] = NonEmptyList(3, 4, 9, 12)
Returns the first n elements of this NonEmptyList as a List.
Returns the first n elements of this NonEmptyList as a List.
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.take(3)
res0: scala.collection.immutable.List[Int] = List(1, 2, 3)
Return the head and tail into a single list
Return the head and tail into a single list
scala> import cats.data.NonEmptyList
scala> val nel = NonEmptyList.of(1, 2, 3, 4, 5)
scala> nel.toList
res0: scala.collection.immutable.List[Int] = List(1, 2, 3, 4, 5)
Creates new NonEmptyMap
, similarly to List#toMap from scala standard library.
Creates new NonEmptyMap
, similarly to List#toMap from scala standard library.
scala> import cats.data.{NonEmptyList, NonEmptyMap}
scala> import cats.implicits._
scala> val nel = NonEmptyList((0, "a"), List((1, "b"),(0, "c"), (2, "d")))
scala> val expectedResult = NonEmptyMap.of(0 -> "c", 1 -> "b", 2 -> "d")
scala> val result = nel.toNem
scala> result === expectedResult
res0: Boolean = true
Creates new NonEmptySet
, similarly to List#toSet from scala standard library.
Creates new NonEmptySet
, similarly to List#toSet from scala standard library.
scala> import cats.data._
scala> import cats.instances.int._
scala> val nel = NonEmptyList(1, List(2,2,3,4))
scala> nel.toNes
res0: cats.data.NonEmptySet[Int] = TreeSet(1, 2, 3, 4)
Creates new NonEmptyVector
, similarly to List#toVector from scala standard library.
Creates new NonEmptyVector
, similarly to List#toVector from scala standard library.
scala> import cats.data._
scala> import cats.instances.int._
scala> val nel = NonEmptyList(1, List(2,3,4))
scala> val expectedResult = NonEmptyVector.fromVectorUnsafe(Vector(1,2,3,4))
scala> val result = nel.toNev
scala> result === expectedResult
res0: Boolean = true
Zips this NonEmptyList
with another NonEmptyList
and returns the pairs of elements.
Zips this NonEmptyList
with another NonEmptyList
and returns the pairs of elements.
scala> import cats.data.NonEmptyList
scala> val as = NonEmptyList.of(1, 2, 3)
scala> val bs = NonEmptyList.of("A", "B", "C")
scala> as.zip(bs)
res0: cats.data.NonEmptyList[(Int, String)] = NonEmptyList((1,A), (2,B), (3,C))
Zips this NonEmptyList
with another NonEmptyList
and applies a function for each pair of elements.
Zips this NonEmptyList
with another NonEmptyList
and applies a function for each pair of elements.
scala> import cats.data.NonEmptyList
scala> val as = NonEmptyList.of(1, 2, 3)
scala> val bs = NonEmptyList.of("A", "B", "C")
scala> as.zipWith(bs)(_.toString + _)
res0: cats.data.NonEmptyList[String] = NonEmptyList(1A, 2B, 3C)