List

scalus.prelude.List
See theList companion object
enum List[+A]

Attributes

Companion
object
Graph
Supertypes
trait Enum
trait Serializable
trait Product
trait Equals
class Object
trait Matchable
class Any
Show all
Known subtypes
class Cons[A]

Members list

Type members

Enum entries

final case class Cons[+A](head: A, tail: List[A]) extends List[A]
case Nil extends List[Nothing]

Value members

Concrete methods

inline def !!(idx: BigInt): A
Extension method from List
inline def ++[B >: A](other: List[B]): List[B]
Extension method from List
inline def ++:[B >: A](other: List[B]): List[B]
Extension method from List
inline def +:[B >: A](elem: B): List[B]
Extension method from List
inline def :+[B >: A](elem: B): List[B]
Extension method from List
inline def :++[B >: A](other: List[B]): List[B]
Extension method from List
def appended[B >: A](elem: B): List[B]
Extension method from List
def appendedAll[B >: A](other: List[B]): List[B]
Extension method from List
def asScala: Seq[A]
Extension method from List

Converts to a scala.Seq

Converts to a scala.Seq

Attributes

def at(index: BigInt): A
Extension method from List
inline def concat[B >: A](other: List[B]): List[B]
Extension method from List
def contains[B >: A](elem: B)(using eq: () => B): Boolean
Extension method from List
def count(p: A => Boolean): BigInt
Extension method from List
def drop(skip: BigInt): List[A]
Extension method from List
def dropRight(skip: BigInt): List[A]
Extension method from List
def exists(predicate: A => Boolean): Boolean
Extension method from List
def filter(predicate: A => Boolean): List[A]
Extension method from List

Filters the elements of the list based on a predicate.

Filters the elements of the list based on a predicate.

Value parameters

predicate

A function that takes an element of type A and returns true if the element should be included in the resulting list, or false otherwise.

Attributes

Returns

A new list containing only the elements that satisfy the predicate.

Example
 List.empty[BigInt].filter(_ % 2 == 1) === Nil
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 val filtered = list.filter(_ % 2 == 1)
 filtered === Cons(1, Cons(3, Nil))
def filterMap[B](predicate: A => Option[B]): List[B]
Extension method from List
def find(predicate: A => Boolean): Option[A]
Extension method from List

Finds the first element in the list that satisfies the given predicate.

Finds the first element in the list that satisfies the given predicate.

Value parameters

predicate

A function that takes an element of type A and returns true if the element matches the condition.

Attributes

Returns

An Option containing the first element that satisfies the predicate, or None if no such element exists.

Example
 List.empty[BigInt].find(_ > 1) === None
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.find(_ > 1) === Some(2)
 list.find(_ > 3) === None
def flatMap[B](mapper: A => List[B]): List[B]
Extension method from List
def foldLeft[B](init: B)(combiner: (B, A) => B): B
Extension method from List

Performs a left fold on the list.

Performs a left fold on the list.

Type parameters

B

The type of the accumulated value.

Value parameters

combiner

A function that combines the accumulated value and the current element.

init

The initial value to start the fold with.

Attributes

Returns

The result of applying the combiner function to all elements of the list, starting with the initial value.

Example
 List.empty[BigInt].foldLeft(BigInt(0))(_ + _) === BigInt(0)
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 val sum = list.foldLeft(BigInt(0))(_ + _)
 sum === BigInt(6)
def foldRight[B](init: B)(combiner: (A, B) => B): B
Extension method from List
def forall(predicate: A => Boolean): Boolean
Extension method from List
def foreach(f: A => Unit): Unit
Extension method from List

Applies the given function to each element of the list.

Applies the given function to each element of the list.

Value parameters

f

The function to apply to each element.

Attributes

Example
 var sum = BigInt(0)
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.foreach(elem => sum += elem)
 sum === BigInt(6)
def get(index: BigInt): Option[A]
Extension method from List

Retrieves the element at the specified index in the list.

Retrieves the element at the specified index in the list.

Value parameters

index

The zero-based index of the element to retrieve.

Attributes

Returns

An Option containing the element at the specified index, or None if the index is out of bounds.

Example
 List.empty[BigInt].get(0) === None
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.get(1) === Some(2)
 list.get(3) === None
 list.get(-1) === None
def groupBy[K : Eq](keyExtractor: A => K): AssocMap[K, List[A]]
Extension method from List

Groups the elements of this list by the keys returned by the specified function.

Groups the elements of this list by the keys returned by the specified function.

Type parameters

K

The type of the keys.

Value parameters

keyExtractor

A function that extracts the key from each element.

Attributes

Returns

An AssocMap mapping each key to a list of elements that have that key.

Example
 List.empty[BigInt].groupBy(_ % 2) === AssocMap.empty
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Cons(4, Nil))))
 list.groupBy(_ % 2) === AssocMap.from((BigInt(1), Cons(1, Cons(3, Nil))), (BigInt(0), Cons(2, Cons(4, Nil))))
def groupMap[K : Eq, B](keyExtractor: A => K)(valueExtractor: A => B): AssocMap[K, List[B]]
Extension method from List

Groups the elements of this list by the keys returned by the key extractor function and transforms each element using the value extractor function.

Groups the elements of this list by the keys returned by the key extractor function and transforms each element using the value extractor function.

Type parameters

B

The type of the transformed elements.

K

The type of the keys.

Value parameters

keyExtractor

A function that extracts the key from each element.

valueExtractor

A function that transforms each element before collecting into groups.

Attributes

Returns

An AssocMap mapping each key to a list of transformed elements that have that key.

Example
 List.empty[BigInt].groupMap(_ % 2)(_ * 2) === AssocMap.empty
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Cons(4, Nil))))
 list.groupMap(_ % 2)(_ * 2) === AssocMap.from((BigInt(1), Cons(2, Cons(6, Nil))), (BigInt(0), Cons(4, Cons(8, Nil))))
def groupMapReduce[K : Eq, B](keyExtractor: A => K)(valueExtractor: A => B)(reducer: (B, B) => B): AssocMap[K, B]
Extension method from List

Groups elements by the keys returned by the key extractor function, transforms each element using the value extractor function, and combines values with the same key using the reducer function.

Groups elements by the keys returned by the key extractor function, transforms each element using the value extractor function, and combines values with the same key using the reducer function.

Type parameters

B

The type of the transformed elements.

K

The type of the keys.

Value parameters

keyExtractor

A function that extracts the key from each element.

reducer

A function that combines two values with the same key.

valueExtractor

A function that transforms each element before reduction.

Attributes

Returns

An AssocMap mapping each key to the reduced value of all elements with that key.

Example
 List.empty[BigInt].groupMapReduce(_ % 2)(identity)(_ + _) === AssocMap.empty
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Cons(4, Nil))))
 list.groupMapReduce(_ % 2)(identity)(_ + _) === AssocMap.from((BigInt(1), BigInt(4)), (BigInt(0), BigInt(6)))
def head: A
Extension method from List

Returns the first element of the list.

Returns the first element of the list.

Attributes

Returns

The first element of the list.

Throws
NoSuchElementException

If the list is empty.

Example
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.head === BigInt(1)
 // List.empty[BigInt].head would throw NoSuchElementException
def headOption: Option[A]
Extension method from List

Returns the first element of the list as an Option.

Returns the first element of the list as an Option.

Attributes

Returns

An Option containing the first element of the list, or None if the list is empty.

Example
 List.empty[BigInt].headOption === None
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.headOption === Some(1)
def indexOf[B >: A](elem: B)(using eq: () => B): BigInt
Extension method from List
def indexOfOption[B >: A](elem: B)(using eq: () => B): Option[BigInt]
Extension method from List

Finds the index of the first occurrence of the specified element in the list.

Finds the index of the first occurrence of the specified element in the list.

Type parameters

B

The type of the element being searched for, which must be a supertype of A.

Value parameters

elem

The element to search for in the list.

Attributes

Returns

An Option containing the index of the first occurrence of the element, or None if the element is not found.

Example
 List.empty[BigInt].indexOfOption(BigInt(2)) === None
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.indexOfOption(BigInt(2)) === Some(BigInt(1))
 list.indexOfOption(BigInt(4)) === None
inline def init: List[A]
Extension method from List
def isDefinedAt(index: BigInt): Boolean
Extension method from List
def isEmpty: Boolean
Extension method from List

Checks if the list is empty.

Checks if the list is empty.

Attributes

Returns

true if the list is empty, false otherwise.

Example
 List.empty[BigInt].isEmpty === true
 Cons(1, Nil).isEmpty       === false
def last: A
Extension method from List
def lastOption: Option[A]
Extension method from List

Returns the last element of the list.

Returns the last element of the list.

Attributes

Returns

An Option containing the last element of the list, or None if the list is empty.

Example
 List.empty[BigInt].lastOption === None
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.lastOption === Some(3)
def length: BigInt
Extension method from List

Returns the number of elements in the list.

Returns the number of elements in the list.

Attributes

Returns

The number of elements in the list as a BigInt.

Example
 List.empty[BigInt].length === BigInt(0)
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.length === BigInt(3)
def map[B](mapper: A => B): List[B]
Extension method from List

Applies a function to each element of the list, producing a new list with the results.

Applies a function to each element of the list, producing a new list with the results.

Value parameters

mapper

A function that takes an element of type A and returns a value of type B.

Attributes

Returns

A new list where each element is the result of applying mapper to the corresponding element of the original list.

Example
 List.empty[BigInt].map(_ * 2) === Nil
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 val result = list.map(_ * 2)
 result === Cons(2, Cons(4, .Cons(6, Nil)))
inline def nonEmpty: Boolean
Extension method from List

Checks if the list is not empty.

Checks if the list is not empty.

Attributes

Returns

true if the list contains at least one element, false otherwise.

Example
 List.empty[BigInt].nonEmpty === false
 Cons(1, Nil).nonEmpty       === true
inline def prepended[B >: A](elem: B): List[B]
Extension method from List

Adds an element at the beginning of this list

Adds an element at the beginning of this list

Attributes

inline def prependedAll[B >: A](other: List[B]): List[B]
Extension method from List
def reverse: List[A]
Extension method from List

Returns a new list with elements in reverse order.

Returns a new list with elements in reverse order.

Attributes

Returns

A new list containing the same elements but in reverse order.

Example
 List.empty[BigInt].reverse === Nil
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.reverse === Cons(3, Cons(2, Cons(1, Nil)))
inline def size: BigInt
Extension method from List

Alias for length.

Alias for length.

Attributes

Returns

The number of elements in the list as a BigInt.

def tail: List[A]
Extension method from List

Returns a list consisting of all elements except the first.

Returns a list consisting of all elements except the first.

Attributes

Returns

A list containing all elements except the first.

Throws
NoSuchElementException

If the list is empty.

Example
 val list: List[BigInt] = Cons(1, Cons(2, Cons(3, Nil)))
 list.tail === Cons(2, Cons(3, Nil))
 // List.empty[BigInt].tail would throw NoSuchElementException
def zip[B](other: List[B]): List[(A, B)]
Extension method from List