org.jetbrains.kotlin.utils.setUtils.kt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of kotlin-compiler-embeddable Show documentation
Show all versions of kotlin-compiler-embeddable Show documentation
the Kotlin compiler embeddable
/*
* Copyright 2010-2023 JetBrains s.r.o. and Kotlin Programming Language contributors.
* Use of this source code is governed by the Apache 2.0 license that can be found in the license/LICENSE.txt file.
*/
package org.jetbrains.kotlin.utils
/**
* Works almost as regular flatMap, but returns a set and returns null if any lambda call returned null
*/
inline fun Iterable.flatMapToNullableSet(transform: (T) -> Iterable?): Set? =
flatMapTo(mutableSetOf()) { transform(it) ?: return null }.ifEmpty { emptySet() }
/**
* Maps all elements of this non-empty collection with the given [transform] function to a new mutable set, or returns [emptySet] if this
* collection is empty.
*
* [mapToSetOrEmpty] should be preferred over `collection.mapTo(mutableSetOf()) { ... }` when `collection` may be empty and the resulting
* set may be cached, because [mapToSetOrEmpty] saves memory by avoiding the creation of an empty mutable set.
*/
inline fun Collection.mapToSetOrEmpty(transform: (T) -> R): Set =
if (isNotEmpty()) mapTo(mutableSetOf(), transform) else emptySet()
inline fun Collection.filterToSetOrEmpty(predicate: (T) -> Boolean): Set =
filterTo(mutableSetOf(), predicate).ifEmpty { emptySet() }