scoop:scoop.std:0.1.0
src/collections/grouping.scoop
Selected production source · src/collections/grouping.scoop
1
package scoop.collections3
/**4
* Appends values into the destination's per-key mutable lists and returns that destination.5
*/6
public fun <T, K, V> Iterable<T>.groupByTo(7
destination: MutableMap<K, MutableList<V>>, keySelector: (T) -> K, valueTransform: (T) -> V8
): MutableMap<K, MutableList<V>> {9
for (element in toList<T>()) {10
val key = keySelector(element)11
val value = valueTransform(element)12
val group = when (destination[key]) {13
case Some(val existing) -> existing14
case None -> {15
val created: MutableList<V> = ArrayList<V>()16
destination[key] = created17
created18
}19
}20
group.add(value)21
}22
return destination23
}25
/**26
* Appends values into the destination's per-key mutable lists and returns that destination.27
*/28
public fun <T, K> Iterable<T>.groupByTo(29
destination: MutableMap<K, MutableList<T>>, keySelector: (T) -> K30
): MutableMap<K, MutableList<T>> = groupByTo<T, K, T>(destination, keySelector) { value -> value }32
/**33
* Groups values into lists by the selected key, retaining encounter order within each group.34
*/35
public fun <T, K, V> Iterable<T>.groupBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, List<V>>36
where K : Equals<K>, K : Hash {37
val groups = LinkedHashMap<K, MutableList<V>>()38
groupByTo<T, K, V>(groups, keySelector, valueTransform)39
val result = LinkedHashMap<K, List<V>>()40
for ((key, values) in groups) result[key] = values.toList()41
return MapSnapshot<K, List<V>>(result)42
}44
/**45
* Groups values into lists by the selected key, retaining encounter order within each group.46
*/47
public fun <T, K> Iterable<T>.groupBy(keySelector: (T) -> K): Map<K, List<T>>48
where K : Equals<K>, K : Hash = groupBy<T, K, T>(keySelector) { value -> value }50
/**51
* Builds a map from transformed key-value pairs; later values replace earlier values for equal52
* keys.53
*/54
public fun <T, K, V> Iterable<T>.associate(transform: (T) -> (K, V)): Map<K, V>55
where K : Equals<K>, K : Hash {56
val result = LinkedHashMap<K, V>()57
for (element in this) {58
val (key, value) = transform(element)59
result[key] = value60
}61
return MapSnapshot<K, V>(result)62
}64
/**65
* Builds a map using selected keys and optional value transformation; the last value for each key66
* wins.67
*/68
public fun <T, K, V> Iterable<T>.associateBy(keySelector: (T) -> K, valueTransform: (T) -> V): Map<K, V>69
where K : Equals<K>, K : Hash = associate<T, K, V> { element -> (keySelector(element), valueTransform(element)) }71
/**72
* Builds a map using selected keys and optional value transformation; the last value for each key73
* wins.74
*/75
public fun <T, K> Iterable<T>.associateBy(keySelector: (T) -> K): Map<K, T>76
where K : Equals<K>, K : Hash = associateBy<T, K, T>(keySelector) { value -> value }78
/**79
* Associates each element with its selected value; later equal elements replace the stored value.80
*/81
public fun <T, V> Iterable<T>.associateWith(valueSelector: (T) -> V): Map<T, V>82
where T : Equals<T>, T : Hash = associate<T, T, V> { value -> (value, valueSelector(value)) }84
/**85
* Retains the first element for each selected key in a new list, preserving encounter order.86
*/87
public fun <T, K> Iterable<T>.distinctBy(selector: (T) -> K): List<T>88
where K : Equals<K>, K : Hash {89
val seen = LinkedHashSet<K>()90
val result = ArrayList<T>()91
for (element in this) { if (seen.add(selector(element))) result.add(element) }92
return ListSnapshot<T>(result.toArray())93
}95
/**96
* Retains the first occurrence of each equal element in a new list.97
*/98
public fun <T> Iterable<T>.distinct(): List<T> where T : Equals<T>, T : Hash =99
distinctBy<T, T> { value -> value }