Skip to content

Commit

Permalink
Merge branch 'main' into test/added-cache4k-testcases
Browse files Browse the repository at this point in the history
  • Loading branch information
serras authored Nov 4, 2024
2 parents 02a144a + 20f29ff commit 2db52f1
Show file tree
Hide file tree
Showing 14 changed files with 267 additions and 188 deletions.
10 changes: 8 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,14 @@ 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 mapValuesOrAccumulate (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Larrow/core/Either;
public static final fun mapValuesOrAccumulate (Ljava/util/Map;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Larrow/core/Either;
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 Expand Up @@ -972,8 +974,10 @@ public class arrow/core/raise/RaiseAccumulate : arrow/core/raise/Raise {
public fun invoke (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public fun invoke (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public final fun mapOrAccumulate (Ljava/lang/Iterable;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public final fun mapOrAccumulate (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public final fun mapOrAccumulate-FrVv2gs (Ljava/util/List;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public final fun mapOrAccumulate-nfMsDo0 (Ljava/util/Set;Lkotlin/jvm/functions/Function2;)Ljava/util/Set;
public final fun mapValuesOrAccumulate (Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Ljava/util/Map;
public fun raise (Ljava/lang/Object;)Ljava/lang/Void;
public final fun raiseErrors ()Ljava/lang/Void;
public final fun withNel (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
Expand Down Expand Up @@ -1049,6 +1053,8 @@ public final class arrow/core/raise/RaiseKt {
public static final fun mapOrAccumulate (Larrow/core/raise/Raise;Lkotlin/sequences/Sequence;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public static final fun mapOrAccumulate--YW8gn4 (Larrow/core/raise/Raise;Ljava/util/Set;Lkotlin/jvm/functions/Function2;)Ljava/util/Set;
public static final fun mapOrAccumulate-l8IBlHg (Larrow/core/raise/Raise;Ljava/util/List;Lkotlin/jvm/functions/Function2;)Ljava/util/List;
public static final fun mapValuesOrAccumulate (Larrow/core/raise/Raise;Ljava/util/Map;Lkotlin/jvm/functions/Function2;)Ljava/util/Map;
public static final fun mapValuesOrAccumulate (Larrow/core/raise/Raise;Ljava/util/Map;Lkotlin/jvm/functions/Function2;Lkotlin/jvm/functions/Function2;)Ljava/util/Map;
public static final fun merge (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
public static final fun merge (Lkotlin/jvm/functions/Function2;Lkotlin/coroutines/Continuation;)Ljava/lang/Object;
public static final fun nullable (Lkotlin/jvm/functions/Function1;)Ljava/lang/Object;
Expand Down
10 changes: 8 additions & 2 deletions arrow-libs/core/arrow-core/api/arrow-core.klib.api

Large diffs are not rendered by default.

32 changes: 25 additions & 7 deletions arrow-libs/core/arrow-core/src/commonMain/kotlin/arrow/core/map.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package arrow.core
import arrow.core.raise.either
import arrow.core.raise.mapOrAccumulate
import arrow.core.raise.RaiseAccumulate
import arrow.core.raise.mapValuesOrAccumulate
import kotlin.experimental.ExperimentalTypeInference

/**
Expand Down Expand Up @@ -244,32 +245,49 @@ 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)
}
}
}

@Deprecated(
message = "Deprecated to allow for future alignment with stdlib Map#map returning List",
replaceWith = ReplaceWith("mapValuesOrAccumulate(combine, transform)"),
)
public inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(
combine: (E, E) -> E,
@BuilderInference transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B
): Either<E, Map<K, B>> = mapValuesOrAccumulate(combine, transform)

@Deprecated(
message = "Deprecated to allow for future alignment with stdlib Map#map returning List",
replaceWith = ReplaceWith("mapValuesOrAccumulate(transform)"),
)
public inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(
@BuilderInference transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B
): Either<NonEmptyList<E>, Map<K, B>> = mapValuesOrAccumulate(transform)

public inline fun <K, E, A, B> Map<K, A>.mapValuesOrAccumulate(
combine: (E, E) -> E,
@BuilderInference transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B
): Either<E, Map<K, B>> = either {
mapOrAccumulate(this@mapOrAccumulate, combine, transform)
mapValuesOrAccumulate(this@mapValuesOrAccumulate, combine, transform)
}

public inline fun <K, E, A, B> Map<K, A>.mapOrAccumulate(
public inline fun <K, E, A, B> Map<K, A>.mapValuesOrAccumulate(
@BuilderInference transform: RaiseAccumulate<E>.(Map.Entry<K, A>) -> B
): Either<NonEmptyList<E>, Map<K, B>> = either {
mapOrAccumulate(this@mapOrAccumulate, transform)
mapValuesOrAccumulate(this@mapValuesOrAccumulate, 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 @@ -651,18 +651,39 @@ public inline fun <Error, A, B> Raise<NonEmptyList<Error>>.mapOrAccumulate(
}.toNonEmptySetOrNull()!!

@RaiseDSL
@Deprecated(
message = "Deprecated to allow for future alignment with stdlib Map#map returning List",
replaceWith = ReplaceWith("mapValuesOrAccumulate(map, combine, transform)"),
)
public inline fun <K, Error, A, B> Raise<Error>.mapOrAccumulate(
map: Map<K, A>,
combine: (Error, Error) -> Error,
@BuilderInference transform: RaiseAccumulate<Error>.(Map.Entry<K, A>) -> B
): Map<K, B> = mapValuesOrAccumulate(map, combine, transform)

@RaiseDSL
@Deprecated(
message = "Deprecated to allow for future alignment with stdlib Map#map returning List",
replaceWith = ReplaceWith("mapValuesOrAccumulate(map, transform)")
)
public inline fun <K, Error, A, B> Raise<NonEmptyList<Error>>.mapOrAccumulate(
map: Map<K, A>,
@BuilderInference transform: RaiseAccumulate<Error>.(Map.Entry<K, A>) -> B
): Map<K, B> = mapValuesOrAccumulate(map, transform)

@RaiseDSL
public inline fun <K, Error, A, B> Raise<Error>.mapValuesOrAccumulate(
map: Map<K, A>,
combine: (Error, Error) -> Error,
@BuilderInference transform: RaiseAccumulate<Error>.(Map.Entry<K, A>) -> B
): Map<K, B> = buildMap(map.size) {
forEachAccumulatingImpl(map.entries.iterator(), combine) { item, hasErrors ->
transform(item).also { if (!hasErrors) put(item.key, it) }
}
}

@RaiseDSL
public inline fun <K, Error, A, B> Raise<NonEmptyList<Error>>.mapOrAccumulate(
public inline fun <K, Error, A, B> Raise<NonEmptyList<Error>>.mapValuesOrAccumulate(
map: Map<K, A>,
@BuilderInference transform: RaiseAccumulate<Error>.(Map.Entry<K, A>) -> B
): Map<K, B> = buildMap(map.size) {
Expand Down Expand Up @@ -712,7 +733,7 @@ public open class RaiseAccumulate<Error>(
raise.raise((errors + r).toNonEmptyListOrNull()!!)

public override fun <K, A> Map<K, Either<Error, A>>.bindAll(): Map<K, A> =
raise.mapOrAccumulate(this) { it.value.bind() }
raise.mapValuesOrAccumulate(this) { it.value.bind() }

@RaiseDSL
public inline fun <A, B> Iterable<A>.mapOrAccumulate(
Expand All @@ -729,6 +750,16 @@ public open class RaiseAccumulate<Error>(
transform: RaiseAccumulate<Error>.(A) -> B
): NonEmptySet<B> = raise.mapOrAccumulate(this, transform)

@RaiseDSL
public inline fun <K, A, B> Map<K, A>.mapOrAccumulate(
transform: RaiseAccumulate<Error>.(Map.Entry<K, A>) -> B
): List<B> = raise.mapOrAccumulate(entries, transform)

@RaiseDSL
public inline fun <K, A, B> Map<K, A>.mapValuesOrAccumulate(
transform: RaiseAccumulate<Error>.(Map.Entry<K, A>) -> B
): Map<K, B> = raise.mapValuesOrAccumulate(this, transform)

@RaiseDSL
@JvmName("_mapOrAccumulate")
public inline fun <A, B> mapOrAccumulate(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import kotlin.test.Test
class ListKTest {

@Test fun monoidLaws() =
testLaws(MonoidLaws("List", emptyList(), List<Int>::plus, Arb.list(Arb.int())))
testLaws(MonoidLaws("List", emptyList(), { x: List<Int>, y: List<Int> -> x + y }, Arb.list(Arb.int())))

@Test fun mapNotNullOk() = runTest {
checkAll(Arb.list(Arb.int())) { listk ->
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,33 +678,33 @@ 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()
result shouldBe expected
}
}

@Test fun mapOrAccumulateEmpty() = runTest {
val result: Either<NonEmptyList<String>, Map<Int, String>> = emptyMap<Int, Int>().mapOrAccumulate {
@Test fun mapValuesOrAccumulateEmpty() = runTest {
val result: Either<NonEmptyList<String>, Map<Int, String>> = emptyMap<Int, Int>().mapValuesOrAccumulate {
it.value.toString()
}

result.shouldBeInstanceOf<Either.Right<Map<Int, String>>>()
.value.shouldBeEmpty()
}

@Test fun mapOrAccumulateMaps() = runTest {
@Test fun mapValuesOrAccumulateMaps() = runTest {
checkAll(
Arb.map(Arb.int(), Arb.int(), maxSize = 30)
) { xs ->

val result: Either<NonEmptyList<String>, Map<Int, String>> = xs.mapOrAccumulate {
val result: Either<NonEmptyList<String>, Map<Int, String>> = xs.mapValuesOrAccumulate {
it.value.toString()
}

Expand All @@ -714,22 +714,22 @@ class MapKTest {
}
}

@Test fun mapOrAccumulateAccumulates() = runTest {
@Test fun mapValuesOrAccumulateAccumulates() = runTest {
checkAll(
Arb.map(Arb.int(), Arb.int(), minSize = 1, maxSize = 30)
) { xs ->
xs.mapOrAccumulate {
raise(it.value)
}.shouldBeInstanceOf<Either.Left<NonEmptyList<Int>>>()
xs.mapValuesOrAccumulate {
raise(it.value)
}.shouldBeInstanceOf<Either.Left<NonEmptyList<Int>>>()
.value.all.shouldContainAll(xs.values)
}
}

@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
4 changes: 1 addition & 3 deletions arrow-libs/optics/arrow-optics-compose/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import org.jetbrains.kotlin.gradle.ExperimentalKotlinGradlePluginApi
import org.jetbrains.kotlin.gradle.dsl.KotlinVersion
import org.jetbrains.kotlin.gradle.targets.js.dsl.ExperimentalWasmDsl


repositories {
google()
Expand Down Expand Up @@ -37,7 +35,7 @@ kotlin {
browser()
nodejs()
}
@OptIn(ExperimentalWasmDsl::class) wasmJs()
@OptIn(org.jetbrains.kotlin.gradle.ExperimentalWasmDsl::class) wasmJs()
androidTarget()
// Native: https://kotlinlang.org/docs/native-target-support.html
// -- Tier 1 --
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
@file:JvmName("ComposeFlowKt")
@file:OptIn(ExperimentalCoroutinesApi::class, ExperimentalForInheritanceCoroutinesApi::class)

package arrow.optics

import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.ExperimentalForInheritanceCoroutinesApi
import kotlinx.coroutines.flow.FlowCollector
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
Expand Down Expand Up @@ -66,7 +68,6 @@ public fun <T, A> MutableStateFlow<T>.optic(lens: Lens<T, A>): MutableStateFlow<
override val replayCache: List<A>
get() = this@optic.replayCache.map { lens.get(it) }

@ExperimentalCoroutinesApi
override fun resetReplayCache() {
this@optic.resetReplayCache()
}
Expand Down
6 changes: 6 additions & 0 deletions arrow-libs/optics/arrow-optics-ksp-plugin/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@ kotlin {
}
}

java {
toolchain {
languageVersion.set(JavaLanguageVersion.of(11))
}
}

apply(from = property("ANIMALSNIFFER_MPP"))

dependencies {
Expand Down
Loading

0 comments on commit 2db52f1

Please sign in to comment.