All Downloads are FREE. Search and download functionalities are using the official Maven repository.

walkmc.extensions.collections.Iterables.kt Maven / Gradle / Ivy

@file:Suppress("NOTHING_TO_INLINE")

package walkmc.extensions.collections

import com.google.common.collect.*
import walkmc.extensions.strings.*
import walkmc.placeholder.*

/**
 * Singularizes this iterable with windoweds. This preserves the last windows.
 * This functions is equals to:
 * ```kt
 * windowed(size, 1, true)
 * ```
 */
inline fun  Iterable.singulared(size: Int) = windowed(size, 1, true)

/**
 * Returns a [FluentIterable] from this iterable object.
 */
inline fun  Iterable.asFluent(): FluentIterable = FluentIterable.from(this)

/**
 * Returns a list containing first [size] elements.
 */
inline fun  Iterable.limit(size: Int) = take(size)

/**
 * Returns a [FluentIterable] from this iterable object skiped by the size.
 */
inline fun  Iterable.skip(size: Int): FluentIterable = asFluent().skip(size)

/**
 * Filters this iterable by the specified equals value, this is, this will filter all
 * values from this iterable to the specified value if them is equals:
 *
 * ```kt
 * val list = listOf(1, 2, 3, 3, 4, 5)
 * println(list.filterEquals(3)) // prints [3, 3]
 * ```
 */
fun  Iterable.filterEquals(value: T): List = filter { it == value }

/**
 * Filters this iterable by the specified *not* equals value, this is, this will filter all
 * values from this iterable to the specified value if them is *not* equals:
 *
 * ```kt
 * val list = listOf(1, 2, 3, 3, 4, 5)
 * println(list.filterNotEquals(3)) // prints [1, 2, 4, 5]
 * ```
 */
fun  Iterable.filterNotEquals(value: T): List = filter { it != value }

/**
 * Filters this iterable by the specified values in range.
 */
fun Iterable.filterRange(range: IntRange): List = filter { it in range }

/**
 * Filters this iterable by the specified values in range.
 */
@JvmName("filterRangeByte")
fun Iterable.filterRange(range: IntRange): List = filter { it in range }

/**
 * Filters this iterable by the specified values in range.
 */
@JvmName("filterRangeShort")
fun Iterable.filterRange(range: IntRange): List = filter { it in range }

/**
 * Filters this iterable by the specified values in range.
 */
fun Iterable.filterRange(range: LongRange): List = filter { it in range }

/**
 * Filters this iterable by the specified values in range.
 */
fun Iterable.filterRange(range: DoubleRange): List = filter { it in range }

/**
 * Filters this iterable by the specified values in range.
 */
@JvmName("filterRangeFloat")
fun Iterable.filterRange(range: FloatRange): List = filter { it in range }

/**
 * Filters this iterable by the specified values in range.
 */
fun Iterable.filterRange(range: CharRange): List = filter { it in range }

/**
 * Returns a [Map] from this iterable object specified from the builder.
 */
inline fun  Iterable.asMap(builder: K.() -> V): Map = associateWith(builder)

/**
 * Process this string with the specified placeholder and value.
 */
inline fun  Iterable.process(placeholder: Placeholder, value: T): List =
	placeholder.process(this, value)

/**
 * Process only specified placeholder in this iterable object.
 */
fun Iterable.process(value: Pair): List = map {
	it.process(value)
}

/**
 * Process all specifieds placeholders in this iterable object.
 */
fun Iterable.process(vararg values: Pair): List = map {
	it.process(*values)
}

/**
 * Process all specifieds placeholders in this iterable object.
 */
fun Iterable.process(values: Map): List = map {
	it.process(values)
}

/**
 * Rotates this list by the specified [distance]. Negative distance is allowed to represents thats
 * the rotation will be in reverse direction.
 */
fun  Iterable.rotate(distance: Int): MutableList {
	return if (this is MutableList) this.rotate(distance) else toMutableList().rotate(distance)
}

/**
 * Creates a [Triple] with the given [third] element with the first and second element of this pair.
 */
infix fun  Pair.and(third: C) = Triple(first, second, third)

/**
 * Groups this iterable applying the given [keySelector] and the aggregator [aggregation].
 */
inline fun  Iterable.groupApply(
	crossinline keySelector: (E) -> K,
	crossinline aggregation: (Iterable) -> V,
): Map {
	val map = mutableMapOf>()
	for (item in this) {
		val key = keySelector(item)
		val list = map.computeIfAbsent(key) { mutableListOf() }
		list += item
	}
	
	val aggregatedMap = mutableMapOf()
	for ((key, value) in map) {
		aggregatedMap[key] = aggregation(value)
	}
	
	return aggregatedMap
}

/**
 * Groups this iterable applying the given [keySelector] with [valueSelector] and the aggregator [aggregation].
 */
inline fun  Iterable.groupApply(
	crossinline keySelector: (E) -> K,
	crossinline valueSelector: (E) -> V,
	crossinline aggregation: (Iterable) -> R,
): Map {
	val map = mutableMapOf>()
	for (item in this) {
		val key = keySelector(item)
		val list = map.computeIfAbsent(key) { mutableListOf() }
		list += valueSelector(item)
	}
	
	val aggregatedMap = mutableMapOf()
	for ((key, value) in map) {
		aggregatedMap[key] = aggregation(value)
	}
	
	return aggregatedMap
}

/**
 * Groups each distinct value with the number counts it appeared
 */
fun  Iterable.countBy() = groupApply({ it }, { it.count() })




© 2015 - 2025 Weber Informatics LLC | Privacy Policy