O1Library Docs
Fall 2026
Docs API

# collection

o1.util.nice.collection
object collection

Provides nice extensions to the interface of `scala.Iterable`, for convenience.

## Attributes

Graph
Supertypes
  class Object
  trait Matchable
  class Any
Self type
  collection.type

## Members list

### Extensions

extension [Element](self: Iterable[Element])
def cycle: LazyList[Element]

Returns an infinite `LazyList` that repeatedly cycles over the collection’s elements. This is equivalent to `LazyList.continually(collection).flatten`.

Returns an infinite `LazyList` that repeatedly cycles over the collection’s elements. This is equivalent to `LazyList.continually(collection).flatten`.

## Attributes

def firstWhere(predicate: Element => Boolean): Element

Returns the first element in the collection that satisfies the given predicate. This is essentially equivalent to calling `dropWhile(predicate).head` or `find(predicate).get`. This method assumes that such an element exists and throws `NoSuchElementException` if not.

Returns the first element in the collection that satisfies the given predicate. This is essentially equivalent to calling `dropWhile(predicate).head` or `find(predicate).get`. This method assumes that such an element exists and throws `NoSuchElementException` if not.

## Attributes

def firstWhereNot(predicate: Element => Boolean): Element

Returns the first element in the collection that does not satisfy the given predicate. This is essentially equivalent to calling `dropWhile( elem => !predicate(elem) ).head` or `find(predicate).get`. This method assumes that such an element exists and throws `NoSuchElementException` if not.

Returns the first element in the collection that does not satisfy the given predicate. This is essentially equivalent to calling `dropWhile( elem => !predicate(elem) ).head` or `find(predicate).get`. This method assumes that such an element exists and throws `NoSuchElementException` if not.

## Attributes

def frequencies: Map[Element, Int]

Constructs and returns a new map with the collection’s elements as keys and each element’s occurrence counts as the corresponding values. This is equivalent to `mapGroups(identity)( _.size )`.

Constructs and returns a new map with the collection’s elements as keys and each element’s occurrence counts as the corresponding values. This is equivalent to `mapGroups(identity)( _.size )`.

## Attributes

inline def ifNonEmpty[Result](compute: Iterable[Element] => Result): Option[Result]

Applies the given function to the collection, assuming there’s at least one element there. Returns `None` if the collection is empty.

Applies the given function to the collection, assuming there’s at least one element there. Returns `None` if the collection is empty.

## Attributes

def log(format: Iterable[Element] => String = ...): Iterable[Element]

Prints out the collection, then returns the unmodified collection.

Prints out the collection, then returns the unmodified collection.

## Parameters

format

  a function from the collection to the desired printout; defaults to `_.toString`

## Attributes

def mapFromID[ID](formID: Element => ID): Map[ID, Element]

Constructs and returns a new map by applying an ID-generating function to each element of the collection and using the collection’s elements as values. This is equivalent to calling `mapify(formID)(identity)`.

Constructs and returns a new map by applying an ID-generating function to each element of the collection and using the collection’s elements as values. This is equivalent to calling `mapify(formID)(identity)`.

## Parameters

formID

  a function called on each element of the collection to obtain the keys

## Attributes

def mapGroups[Key, Value](formKey: Element => Key)(transformGroup: Iterable[Element] => Value): Map[Key, Value]

Constructs and returns a new map by using one given function to group the elements of the collection (as per `groupBy`) and then transforming each of the groups using another given function (as per `mapValues`).

Constructs and returns a new map by using one given function to group the elements of the collection (as per `groupBy`) and then transforming each of the groups using another given function (as per `mapValues`).

## Parameters

formKey

  a function called on each element of the collection to generate groups of values

transformGroup

  a function called on each group to obtain the values for the resulting collection

## Attributes

def mapTo[Value](formValue: Element => Value): Map[Element, Value]

Constructs and returns a new map by applying a value-generating function to each element of the collection and using the collection’s elements as keys. This is equivalent to calling `mapify(identity)(formValue)`.

Constructs and returns a new map by applying a value-generating function to each element of the collection and using the collection’s elements as keys. This is equivalent to calling `mapify(identity)(formValue)`.

## Parameters

formValue

  a function called on each element of the collection to obtain the keys

## Attributes

def mapify[Key, Value](formKey: Element => Key)(formValue: Element => Value): Map[Key, Value]

Constructs and returns a new map by applying a key-generating function as well as a value-generating function to each element of the collection. The respective outputs of the functions are paired to form the key—value pairs of the `Map`.

Constructs and returns a new map by applying a key-generating function as well as a value-generating function to each element of the collection. The respective outputs of the functions are paired to form the key—value pairs of the `Map`.

## Parameters

formKey

  a function called on each element of the collection to obtain the keys

formValue

  a function called on each element of the collection to obtain the values

## Attributes

def slidingPairs: Iterator[(Element, Element)]

Returns an iterator of pairs (tuples) that “slide across” the collection as per `sliding`.

Returns an iterator of pairs (tuples) that “slide across” the collection as per `sliding`.

## Attributes

def tap(effect: Element => Unit): Iterable[Element]

Performs a given side effect at each element of the collection. Returns the unmodified collection.

Performs a given side effect at each element of the collection. Returns the unmodified collection.

## Attributes

def toLazy: LazyList[Element]

Returns a `LazyList` containing the collection’s elements. This is equivalent to `to(LazyList)`.

Returns a `LazyList` containing the collection’s elements. This is equivalent to `to(LazyList)`.

## Attributes

extension [Element, Constructor[_], Collection](self: IterableOps[Element, Constructor, Collection])
def except(excluded: Element)(using CanEqual[Element, Element]): Collection

Filters out a specific element. (All instances of it, in case there are many.)

Filters out a specific element. (All instances of it, in case there are many.)

## Attributes