Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: rename Map.flatMap and Map.mapNotNull #3512

Merged
merged 2 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions arrow-libs/core/arrow-core/api/arrow-core.api
Original file line number Diff line number Diff line change
Expand Up @@ -309,12 +309,12 @@ public final class arrow/core/MapKt {
public static final fun align (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
public static final fun combine (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Ljava/util/Map;
public static final fun filterOption (Ljava/util/Map;)Ljava/util/Map;
public static final fun flatMap (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
public static final fun flatMapValues (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
public static final fun fold (Ljava/util/Map;Ljava/lang/Object;Lkotlin/jvm/functions/Function2;)Ljava/lang/Object;
public static final fun getOrNone (Ljava/util/Map;Ljava/lang/Object;)Larrow/core/Option;
public static final fun mapNotNull (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
public static final fun mapOrAccumulate (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Larrow/core/Either;
public static final fun mapOrAccumulate (Ljava/util/Map;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Larrow/core/Either;
public static final fun mapValuesNotNull (Ljava/util/Map;Lkotlin/jvm/functions/Function1;)Ljava/util/Map;
public static final fun padZip (Ljava/util/Map;Ljava/util/Map;)Ljava/util/Map;
public static final fun padZip (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function3;)Ljava/util/Map;
public static final fun padZip (Ljava/util/Map;Ljava/util/Map;Lkotlin/jvm/functions/Function3;)Ljava/util/Map;
Expand Down
4 changes: 2 additions & 2 deletions arrow-libs/core/arrow-core/api/arrow-core.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -729,8 +729,7 @@ final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/Either
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (arrow.core/NonEmptyList<#C>).arrow.core/unzip(kotlin/Function1<#C, kotlin/Pair<#A, #B>>): kotlin/Pair<arrow.core/NonEmptyList<#A>, arrow.core/NonEmptyList<#B>> // arrow.core/unzip|[email protected]<0:2>(kotlin.Function1<0:2,kotlin.Pair<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Iterable<#A>).arrow.core/crosswalkMap(kotlin/Function1<#A, kotlin.collections/Map<#B, #C>>): kotlin.collections/Map<#B, kotlin.collections/List<#C>> // arrow.core/crosswalkMap|[email protected]<0:0>(kotlin.Function1<0:0,kotlin.collections.Map<0:1,0:2>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/align(kotlin.collections/Map<#A, #C>): kotlin.collections/Map<#A, arrow.core/Ior<#B, #C>> // arrow.core/align|[email protected]<0:0,0:1>(kotlin.collections.Map<0:0,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/flatMap(kotlin/Function1<kotlin.collections/Map.Entry<#A, #B>, kotlin.collections/Map<#A, #C>>): kotlin.collections/Map<#A, #C> // arrow.core/flatMap|[email protected]<0:0,0:1>(kotlin.Function1<kotlin.collections.Map.Entry<0:0,0:1>,kotlin.collections.Map<0:0,0:2>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/mapNotNull(kotlin/Function1<kotlin.collections/Map.Entry<#A, #B>, #C?>): kotlin.collections/Map<#A, #C> // arrow.core/mapNotNull|[email protected]<0:0,0:1>(kotlin.Function1<kotlin.collections.Map.Entry<0:0,0:1>,0:2?>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/flatMapValues(kotlin/Function1<kotlin.collections/Map.Entry<#A, #B>, kotlin.collections/Map<#A, #C>>): kotlin.collections/Map<#A, #C> // arrow.core/flatMapValues|[email protected]<0:0,0:1>(kotlin.Function1<kotlin.collections.Map.Entry<0:0,0:1>,kotlin.collections.Map<0:0,0:2>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/padZip(kotlin.collections/Map<#A, #C>): kotlin.collections/Map<#A, kotlin/Pair<#B?, #C?>> // arrow.core/padZip|[email protected]<0:0,0:1>(kotlin.collections.Map<0:0,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/zip(kotlin.collections/Map<#A, #C>): kotlin.collections/Map<#A, kotlin/Pair<#B, #C>> // arrow.core/zip|[email protected]<0:0,0:1>(kotlin.collections.Map<0:0,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, arrow.core/Ior<#B, #C>>).arrow.core/unalign(): kotlin/Pair<kotlin.collections/Map<#A, #B>, kotlin.collections/Map<#A, #C>> // arrow.core/unalign|[email protected]<0:0,arrow.core.Ior<0:1,0:2>>(){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
Expand Down Expand Up @@ -934,6 +933,7 @@ final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.col
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Iterable<#B>).arrow.core/mapOrAccumulate(kotlin/Function2<arrow.core.raise/RaiseAccumulate<#A>, #B, #C>): arrow.core/Either<arrow.core/NonEmptyList<#A>, kotlin.collections/List<#C>> // arrow.core/mapOrAccumulate|[email protected]<0:1>(kotlin.Function2<arrow.core.raise.RaiseAccumulate<0:0>,0:1,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Iterable<#C>).arrow.core/unalign(kotlin/Function1<#C, arrow.core/Ior<#A, #B>>): kotlin/Pair<kotlin.collections/List<#A?>, kotlin.collections/List<#B?>> // arrow.core/unalign|[email protected]<0:2>(kotlin.Function1<0:2,arrow.core.Ior<0:0,0:1>>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/fold(#C, kotlin/Function2<#C, kotlin.collections/Map.Entry<#A, #B>, #C>): #C // arrow.core/fold|[email protected]<0:0,0:1>(0:2;kotlin.Function2<0:2,kotlin.collections.Map.Entry<0:0,0:1>,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.collections/Map<#A, #B>).arrow.core/mapValuesNotNull(kotlin/Function1<kotlin.collections/Map.Entry<#A, #B>, #C?>): kotlin.collections/Map<#A, #C> // arrow.core/mapValuesNotNull|[email protected]<0:0,0:1>(kotlin.Function1<kotlin.collections.Map.Entry<0:0,0:1>,0:2?>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin/Function1<arrow.core.raise/Raise<#A>, #B>).arrow.core.raise/fold(kotlin/Function1<#A, #C>, kotlin/Function1<#B, #C>): #C // arrow.core.raise/fold|[email protected]<arrow.core.raise.Raise<0:0>,0:1>(kotlin.Function1<0:0,0:2>;kotlin.Function1<0:1,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin/Function1<arrow.core.raise/Raise<#A>, #B>).arrow.core.raise/fold(kotlin/Function1<kotlin/Throwable, #C>, kotlin/Function1<#A, #C>, kotlin/Function1<#B, #C>): #C // arrow.core.raise/fold|[email protected]<arrow.core.raise.Raise<0:0>,0:1>(kotlin.Function1<kotlin.Throwable,0:2>;kotlin.Function1<0:0,0:2>;kotlin.Function1<0:1,0:2>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> arrow.core.raise/accumulate(kotlin/Function1<kotlin/Function1<arrow.core.raise/Raise<arrow.core/NonEmptyList<#A>>, #B>, #C>, crossinline kotlin/Function1<arrow.core.raise/RaiseAccumulate<#A>, #B>): #C // arrow.core.raise/accumulate|accumulate(kotlin.Function1<kotlin.Function1<arrow.core.raise.Raise<arrow.core.NonEmptyList<0:0>>,0:1>,0:2>;kotlin.Function1<arrow.core.raise.RaiseAccumulate<0:0>,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>;2§<kotlin.Any?>}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,9 +244,9 @@ public inline fun <Key, B, C, D, E, F, G, H, I, J, K, L> Map<Key, B>.zip(
* only keeping the [Map.Entry] of the transformed map that match the input [Map.Entry].
*/
@Suppress("UNCHECKED_CAST")
public fun <K, A, B> Map<K, A>.flatMap(f: (Map.Entry<K, A>) -> Map<K, B>): Map<K, B> =
public fun <K, A, B> Map<K, A>.flatMapValues(f: (Map.Entry<K, A>) -> Map<K, B>): Map<K, B> =
buildMap {
this@flatMap.forEach { entry ->
this@flatMapValues.forEach { entry ->
val nestedMap = f(entry)
if (nestedMap.containsKey(entry.key)) {
put(entry.key, nestedMap[entry.key] as B)
Expand All @@ -267,9 +267,9 @@ public inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(
mapOrAccumulate(this@mapOrAccumulate, transform)
}

public fun <K, A, B> Map<K, A>.mapNotNull(transform: (Map.Entry<K, A>) -> B?): Map<K, B> =
public inline fun <K, A, B> Map<K, A>.mapValuesNotNull(transform: (Map.Entry<K, A>) -> B?): Map<K, B> =
buildMap {
this@mapNotNull.forEach { entry ->
this@mapValuesNotNull.forEach { entry ->
transform(entry)?.let { put(entry.key, it) }
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -409,11 +409,11 @@ class MapKTest {
}
}

@Test fun mapNotNullOk() = runTest {
@Test fun mapValuesNotNullOk() = runTest {
checkAll(
Arb.map(Arb.int(), Arb.boolean(), maxSize = 30)
) { xs ->
val rs = xs.mapNotNull { (_, pred) -> if(pred) true else null }
val rs = xs.mapValuesNotNull { (_, pred) -> if(pred) true else null }

xs.forAll {
if (it.value)
Expand Down Expand Up @@ -678,11 +678,11 @@ class MapKTest {
}
}

@Test fun flatMapOk() = runTest {
@Test fun flatMapValuesOk() = runTest {
checkAll(
Arb.map2(Arb.int(), Arb.int(), Arb.int())
) { (a, b) ->
val result = a.flatMap { b }
val result = a.flatMapValues { b }
val expected = a.filter { (k, _) -> b.containsKey(k) }
.map { (k, _) -> Pair(k, b[k]!!) }
.toMap()
Expand Down Expand Up @@ -725,11 +725,11 @@ class MapKTest {
}
}

@Test fun flatMapNull() = runTest {
@Test fun flatMapValuesNull() = runTest {
checkAll(
Arb.map2(Arb.int(), Arb.int(), Arb.int().orNull())
) { (mapA, mapB) ->
val result = mapA.flatMap { mapB }
val result = mapA.flatMapValues { mapB }
val expected = mapA.filter { (k, _) -> mapB.containsKey(k) }
.map { (k, _) -> Pair(k, mapB[k]) }
.toMap()
Expand Down