mirror of
https://github.com/InsanusMokrassar/MicroUtils.git
synced 2025-10-08 17:00:30 +00:00
60 lines
1.6 KiB
Kotlin
60 lines
1.6 KiB
Kotlin
package dev.inmo.micro_utils.common
|
|
|
|
inline fun <I, R> Iterable<I>.joinTo(
|
|
crossinline separatorFun: (I) -> R?,
|
|
prefix: R? = null,
|
|
postfix: R? = null,
|
|
crossinline transform: (I) -> R?
|
|
): List<R> {
|
|
val result = mutableListOf<R>()
|
|
val iterator = iterator()
|
|
|
|
prefix ?.let(result::add)
|
|
|
|
while (iterator.hasNext()) {
|
|
val element = iterator.next()
|
|
result.add(transform(element) ?: continue)
|
|
|
|
if (iterator.hasNext()) {
|
|
result.add(separatorFun(element) ?: continue)
|
|
}
|
|
}
|
|
|
|
postfix ?.let(result::add)
|
|
|
|
return result
|
|
}
|
|
|
|
inline fun <I, R> Iterable<I>.joinTo(
|
|
separator: R? = null,
|
|
prefix: R? = null,
|
|
postfix: R? = null,
|
|
crossinline transform: (I) -> R?
|
|
): List<R> = joinTo({ separator }, prefix, postfix, transform)
|
|
|
|
inline fun <I> Iterable<I>.joinTo(
|
|
crossinline separatorFun: (I) -> I?,
|
|
prefix: I? = null,
|
|
postfix: I? = null
|
|
): List<I> = joinTo<I, I>(separatorFun, prefix, postfix) { it }
|
|
|
|
inline fun <I> Iterable<I>.joinTo(
|
|
separator: I? = null,
|
|
prefix: I? = null,
|
|
postfix: I? = null
|
|
): List<I> = joinTo<I>({ separator }, prefix, postfix)
|
|
|
|
inline fun <I, reified R> Array<I>.joinTo(
|
|
crossinline separatorFun: (I) -> R?,
|
|
prefix: R? = null,
|
|
postfix: R? = null,
|
|
crossinline transform: (I) -> R?
|
|
): Array<R> = asIterable().joinTo(separatorFun, prefix, postfix, transform).toTypedArray()
|
|
|
|
inline fun <I, reified R> Array<I>.joinTo(
|
|
separator: R? = null,
|
|
prefix: R? = null,
|
|
postfix: R? = null,
|
|
crossinline transform: (I) -> R?
|
|
): Array<R> = asIterable().joinTo(separator, prefix, postfix, transform).toTypedArray()
|