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

commonMain.dev.inmo.micro_utils.common.JoinTo.kt Maven / Gradle / Ivy

package dev.inmo.micro_utils.common

inline fun  Iterable.joinTo(
    separatorFun: (I) -> R?,
    prefix: R? = null,
    postfix: R? = null,
    transform: (I) -> R?
): List {
    val result = mutableListOf()
    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  Iterable.joinTo(
    separator: R? = null,
    prefix: R? = null,
    postfix: R? = null,
    transform: (I) -> R?
): List = joinTo({ separator }, prefix, postfix, transform)

inline fun  Iterable.joinTo(
    separatorFun: (I) -> I?,
    prefix: I? = null,
    postfix: I? = null
): List = joinTo(separatorFun, prefix, postfix) { it }

inline fun  Iterable.joinTo(
    separator: I? = null,
    prefix: I? = null,
    postfix: I? = null
): List = joinTo({ separator }, prefix, postfix)

inline fun  Array.joinTo(
    separatorFun: (I) -> R?,
    prefix: R? = null,
    postfix: R? = null,
    transform: (I) -> R?
): Array = asIterable().joinTo(separatorFun, prefix, postfix, transform).toTypedArray()

inline fun  Array.joinTo(
    separator: R? = null,
    prefix: R? = null,
    postfix: R? = null,
    transform: (I) -> R?
): Array = asIterable().joinTo(separator, prefix, postfix, transform).toTypedArray()