[FIR] Refactoring: Move set utility functions to setUtils.kt

This commit is contained in:
Marco Pennekamp
2023-10-19 19:41:27 +02:00
committed by Space Team
parent 8d2bf8828c
commit 38ebe60e9f
6 changed files with 16 additions and 18 deletions
@@ -1,19 +0,0 @@
/*
* 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.analysis.utils.collections
/**
* 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.
*/
public inline fun <T, R> Collection<T>.mapToSetOrEmpty(transform: (T) -> R): Set<R> =
if (isNotEmpty()) mapTo(mutableSetOf(), transform) else emptySet()
public inline fun <T> Collection<T>.filterToSetOrEmpty(predicate: (T) -> Boolean): Set<T> =
filterTo(mutableSetOf(), predicate).ifEmpty { emptySet() }