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

com.lightningkite.khrysalis.util.forEachBetween.kt Maven / Gradle / Ivy

The newest version!
package com.lightningkite.khrysalis.util

import java.util.concurrent.*

inline fun  Iterable.forEachBetween(
    forItem: (T) -> Unit,
    between: () -> Unit
) {
    var hasDoneFirst = false
    forEach {
        if (hasDoneFirst) {
            between()
        } else {
            hasDoneFirst = true
        }
        forItem(it)
    }
}


inline fun  Sequence.forEachBetween(
    forItem: (T) -> Unit,
    between: () -> Unit
) {
    var hasDoneFirst = false
    forEach {
        if (hasDoneFirst) {
            between()
        } else {
            hasDoneFirst = true
        }
        forItem(it)
    }
}

inline fun  Iterable.forEachBetweenIndexed(
    forItem: (Int, T) -> Unit,
    between: () -> Unit
) {
    var hasDoneFirst = false
    forEachIndexed { index, it ->
        if (hasDoneFirst) {
            between()
        } else {
            hasDoneFirst = true
        }
        forItem(index, it)
    }
}


inline fun  Sequence.forEachBetweenIndexed(
    forItem: (Int, T) -> Unit,
    between: () -> Unit
) {
    var hasDoneFirst = false
    forEachIndexed { index, it ->
        if (hasDoneFirst) {
            between()
        } else {
            hasDoneFirst = true
        }
        forItem(index, it)
    }
}

fun  Collection.forEachMultithreaded(action: (T)->Unit) {
    this.parallelStream().forEach(action)
}

fun  Sequence.forEachMultithreaded(action: (T)->Unit) {
//    this.asStream().parallel().forEach(action)
    this.forEach(action)
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy