Scoop API

scoop:scoop.std:0.1.0

src/sequences/sorting.scoop

Selected production source · src/sequences/sorting.scoop

1package scoop.sequences
3import scoop.collections.sortedWith
4import scoop.collections.sortedBy
5import scoop.comparisons.Comparator
6import scoop.comparisons.naturalOrder
7import scoop.comparisons.reverseOrder
9/**
10 * Evaluation is deferred until iteration. Creates a lazy sequence that materializes a stable
11 * sorted copy using the supplied comparator. Each traversal materializes the complete input before
12 * yielding results.
13 */
14public fun <T> Sequence<T>.sortedWith(comparator: Comparator<T>): Sequence<T> =
15 FactorySequence<T> { terminalInput<T>().sortedWith<T>(comparator).iterator() }
17/**
18 * Evaluation is deferred until iteration. Creates a lazy sequence that materializes a stable copy
19 * in natural ascending order. Each traversal materializes the complete input before yielding
20 * results.
21 */
22public fun <T : Compare<T>> Sequence<T>.sorted(): Sequence<T> = sortedWith<T>(naturalOrder<T>())
23/**
24 * Evaluation is deferred until iteration. Creates a lazy sequence that materializes a stable copy
25 * in natural descending order. Each traversal materializes the complete input before yielding
26 * results.
27 */
28public fun <T : Compare<T>> Sequence<T>.sortedDescending(): Sequence<T> = sortedWith<T>(reverseOrder<T>())
30/**
31 * Evaluation is deferred until iteration. Creates a lazy sequence that materializes a stable copy
32 * ordered by the selector's natural key order. Each traversal materializes the complete input
33 * before yielding results.
34 */
35public fun <T, K : Compare<K>> Sequence<T>.sortedBy(selector: (T) -> K): Sequence<T> =
36 FactorySequence<T> { terminalInput<T>().sortedBy<T, K>(selector).iterator() }