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

com.abelhu.utils._Collections.kt Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package com.abelhu.utils

/**
 * Returns a list containing all elements satisfying the given [predicate]
 */
public inline fun  Iterable.takeAll(predicate: (T) -> Boolean): List {
    val list = ArrayList()
    for (item in this) {
        if (!predicate(item))
            continue
        list.add(item)
    }
    return list
}

/**
 * Returns one element satisfying the given [predicate]
 */
public inline fun  Iterable.takeOnly(predicate: (T) -> Boolean): T? {
    forEach { item -> if (predicate(item)) return item }
    return null
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy