Skip to content

Commit

Permalink
Suggestions by @nomisRev
Browse files Browse the repository at this point in the history
  • Loading branch information
serras committed Oct 2, 2024
1 parent 3c83496 commit 3615561
Show file tree
Hide file tree
Showing 11 changed files with 45 additions and 33 deletions.
11 changes: 6 additions & 5 deletions arrow-libs/optics/arrow-match/api/arrow-match.api
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
public final class arrow/match/BuildersKt {
public static final fun getNotEmpty (Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun identity ()Lkotlin/reflect/KProperty1;
public static final fun isNotEmpty (Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun of (Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun of (Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun of (Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun of (Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun of (Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;Lkotlin/reflect/KProperty1;)Lkotlin/reflect/KProperty1;
public static final fun self ()Lkotlin/reflect/KProperty1;
public static final fun suchThat (Lkotlin/reflect/KProperty1;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lkotlin/reflect/KProperty1;
public static synthetic fun suchThat$default (Lkotlin/reflect/KProperty1;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlin/reflect/KProperty1;
public static final fun takeIf (Lkotlin/reflect/KProperty1;Ljava/lang/String;Lkotlin/jvm/functions/Function1;)Lkotlin/reflect/KProperty1;
public static synthetic fun takeIf$default (Lkotlin/reflect/KProperty1;Ljava/lang/String;Lkotlin/jvm/functions/Function1;ILjava/lang/Object;)Lkotlin/reflect/KProperty1;
}

public final class arrow/match/DoesNotMatch : java/lang/Throwable {
public static final field INSTANCE Larrow/match/DoesNotMatch;
public fun <init> ()V
public fun fillInStackTrace ()Ljava/lang/Throwable;
}

public final class arrow/match/MatchKt {
Expand Down
14 changes: 8 additions & 6 deletions arrow-libs/optics/arrow-match/api/arrow-match.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ abstract class <#A: kotlin/Any?, #B: kotlin/Any?> arrow.match/MatchScope { // ar
final val it // arrow.match/MatchScope.it|{}it[0]
final fun <get-it>(): kotlin.reflect/KProperty1<#A, #A> // arrow.match/MatchScope.it.<get-it>|<get-it>(){}[0]
}
final class arrow.match/DoesNotMatch : kotlin/Throwable { // arrow.match/DoesNotMatch|null[0]
constructor <init>() // arrow.match/DoesNotMatch.<init>|<init>(){}[0]
}
final class arrow.match/MatchNotFound : kotlin/Throwable { // arrow.match/MatchNotFound|null[0]
constructor <init>(kotlin/Any?) // arrow.match/MatchNotFound.<init>|<init>(kotlin.Any?){}[0]
final val value // arrow.match/MatchNotFound.value|{}value[0]
Expand All @@ -32,11 +35,10 @@ final fun <#A: kotlin/Any?, #B: kotlin/Any?, #C: kotlin/Any?> (kotlin.reflect/KP
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (#A).arrow.match/matchOrElse(kotlin/Function0<#B>, kotlin/Function1<arrow.match/MatchScope<#A, #B>, kotlin/Unit>): #B // arrow.match/matchOrElse|matchOrElse@0:0(kotlin.Function0<0:1>;kotlin.Function1<arrow.match.MatchScope<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (#A).arrow.match/matchOrThrow(kotlin/Function0<kotlin/Throwable> = ..., kotlin/Function1<arrow.match/MatchScope<#A, #B>, kotlin/Unit>): #B // arrow.match/matchOrThrow|matchOrThrow@0:0(kotlin.Function0<kotlin.Throwable>;kotlin.Function1<arrow.match.MatchScope<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.core.raise/Raise<arrow.match/MatchNotFound>).arrow.match/matchOrRaise(#A, kotlin/Function1<arrow.match/MatchScope<#A, #B>, kotlin/Unit>): #B // arrow.match/matchOrRaise|matchOrRaise@arrow.core.raise.Raise<arrow.match.MatchNotFound>(0:0;kotlin.Function1<arrow.match.MatchScope<0:0,0:1>,kotlin.Unit>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.reflect/KProperty1<#A, #B>).arrow.match/suchThat(kotlin/String? = ..., kotlin/Function1<#B, kotlin/Boolean>): kotlin.reflect/KProperty1<#A, #B> // arrow.match/suchThat|suchThat@kotlin.reflect.KProperty1<0:0,0:1>(kotlin.String?;kotlin.Function1<0:1,kotlin.Boolean>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (kotlin.reflect/KProperty1<#A, #B>).arrow.match/takeIf(kotlin/String? = ..., kotlin/Function1<#B, kotlin/Boolean>): kotlin.reflect/KProperty1<#A, #B> // arrow.match/takeIf|takeIf@kotlin.reflect.KProperty1<0:0,0:1>(kotlin.String?;kotlin.Function1<0:1,kotlin.Boolean>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> arrow.match/Matcher(kotlin/String, kotlin/Function1<#A, #B>): kotlin.reflect/KProperty1<#A, #B> // arrow.match/Matcher|Matcher(kotlin.String;kotlin.Function1<0:0,0:1>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> (#A).arrow.match/matchUnit(kotlin/Function1<arrow.match/MatchScope<#A, kotlin/Unit>, kotlin/Unit>) // arrow.match/matchUnit|matchUnit@0:0(kotlin.Function1<arrow.match.MatchScope<0:0,kotlin.Unit>,kotlin.Unit>){0§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> arrow.match/self(): kotlin.reflect/KProperty1<#A, #A> // arrow.match/self|self(){0§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: reified #A> arrow.match/it(): kotlin.reflect/KProperty1<#A, #B> // arrow.match/it|it(){0§<kotlin.Any?>;1§<0:0>}[0]
final object arrow.match/DoesNotMatch : kotlin/Throwable // arrow.match/DoesNotMatch|null[0]
final val arrow.match/notEmpty // arrow.match/notEmpty|@kotlin.reflect.KProperty1<0:0,kotlin.collections.Collection<0:1>>{0§<kotlin.Any?>;1§<kotlin.Any?>}notEmpty[0]
final fun <#A1: kotlin/Any?, #B1: kotlin/Any?> (kotlin.reflect/KProperty1<#A1, kotlin.collections/Collection<#B1>>).<get-notEmpty>(): kotlin.reflect/KProperty1<#A1, kotlin.collections/Collection<#B1>> // arrow.match/notEmpty.<get-notEmpty>|<get-notEmpty>@kotlin.reflect.KProperty1<0:0,kotlin.collections.Collection<0:1>>(){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?> arrow.match/identity(): kotlin.reflect/KProperty1<#A, #A> // arrow.match/identity|identity(){0§<kotlin.Any?>}[0]
final inline fun <#A: kotlin/Any?, #B: reified #A> arrow.match/instanceOf(): kotlin.reflect/KProperty1<#A, #B> // arrow.match/instanceOf|instanceOf(){0§<kotlin.Any?>;1§<0:0>}[0]
final val arrow.match/isNotEmpty // arrow.match/isNotEmpty|@kotlin.reflect.KProperty1<0:0,kotlin.collections.Collection<0:1>>{0§<kotlin.Any?>;1§<kotlin.Any?>}isNotEmpty[0]
final fun <#A1: kotlin/Any?, #B1: kotlin/Any?> (kotlin.reflect/KProperty1<#A1, kotlin.collections/Collection<#B1>>).<get-isNotEmpty>(): kotlin.reflect/KProperty1<#A1, kotlin.collections/Collection<#B1>> // arrow.match/isNotEmpty.<get-isNotEmpty>|<get-isNotEmpty>@kotlin.reflect.KProperty1<0:0,kotlin.collections.Collection<0:1>>(){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,25 @@ public expect fun <S, A> Matcher(
get: (S) -> A
): Matcher<S, A>

public object DoesNotMatch: Throwable()
public expect class DoesNotMatch(): Throwable

public fun <S> self(): Matcher<S, S> = Matcher("self") { it }
public fun <S> identity(): Matcher<S, S> = Matcher("identity") { it }

public inline fun <S, reified A: S> it(): Matcher<S, A> =
Matcher("is<${A::class.simpleName}>") {
if (it is A) it else throw DoesNotMatch
public inline fun <S, reified A: S> instanceOf(): Matcher<S, A> =
Matcher("instanceOf<${A::class.simpleName}>") {
if (it is A) it else throw DoesNotMatch()
}

public fun <S, A> Matcher<S, A>.suchThat(
public fun <S, A> Matcher<S, A>.takeIf(
description: String? = null,
predicate: (A) -> Boolean
): Matcher<S, A> = Matcher("${this.name}.${description ?: "suchThat"}") {
val value = this.get(it)
if (predicate(value)) value else throw DoesNotMatch
if (predicate(value)) value else throw DoesNotMatch()
}

public val <S, A> Matcher<S, Collection<A>>.notEmpty: Matcher<S, Collection<A>>
get() = this.suchThat("notEmpty") { it.isNotEmpty() }
public val <S, A> Matcher<S, Collection<A>>.isNotEmpty: Matcher<S, Collection<A>>
get() = this.takeIf("isNotEmpty") { it.isNotEmpty() }

public fun <S, A, B> Matcher<S, A>.of(
field: Matcher<A, B>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,33 +14,33 @@ public abstract class MatchScope<S, R> {

public inline fun <reified A: S & Any, B> KClass<A>.of(
field: Matcher<A, B>
): Matcher<S, B> = it<S, A>().of(field)
): Matcher<S, B> = instanceOf<S, A>().of(field)

public inline fun <reified A: S & Any, B, C> KClass<A>.of(
field1: Matcher<A, B>,
field2: Matcher<A, C>
): Matcher<S, Pair<B, C>> = it<S, A>().of(field1, field2)
): Matcher<S, Pair<B, C>> = instanceOf<S, A>().of(field1, field2)

public inline fun <reified A: S & Any, B, C, D> KClass<A>.of(
field1: Matcher<A, B>,
field2: Matcher<A, C>,
field3: Matcher<A, D>
): Matcher<S, Triple<B, C, D>> = it<S, A>().of(field1, field2, field3)
): Matcher<S, Triple<B, C, D>> = instanceOf<S, A>().of(field1, field2, field3)

public inline fun <reified A: S & Any, B, C, D, E> KClass<A>.of(
field1: Matcher<A, B>,
field2: Matcher<A, C>,
field3: Matcher<A, D>,
field4: Matcher<A, E>
): Matcher<S, Tuple4<B, C, D, E>> = it<S, A>().of(field1, field2, field3, field4)
): Matcher<S, Tuple4<B, C, D, E>> = instanceOf<S, A>().of(field1, field2, field3, field4)

public inline fun <reified A: S & Any, B, C, D, E, F> KClass<A>.of(
field1: Matcher<A, B>,
field2: Matcher<A, C>,
field3: Matcher<A, D>,
field4: Matcher<A, E>,
field5: Matcher<A, F>
): Matcher<S, Tuple5<B, C, D, E, F>> = it<S, A>().of(field1, field2, field3, field4, field5)
): Matcher<S, Tuple5<B, C, D, E, F>> = instanceOf<S, A>().of(field1, field2, field3, field4, field5)
}

private class MatchScopeImpl<S, R>(val subject: S): MatchScope<S, R>() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package arrow.match

public actual class DoesNotMatch: Throwable() {
// disable stacktrace creation
override fun fillInStackTrace(): Throwable = this
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ data class Company(
): User

val User.shownName: String get() = this.matchOrThrow {
Person::class.of(Person::name.of(Name::firstName), Person::age.suchThat { it < 18 }) then { (fn, _) -> fn }
Person::class.of(Person::name.of(Name::firstName), Person::age.takeIf { it < 18 }) then { (fn, _) -> fn }
Person::class.of(Person::name.of(Name::firstName, Name::lastName)) then { (fn, ln) -> "Sir/Madam $fn $ln" }
Company::class.of(Company::name, Company::director.of(Name::lastName)) then { (nm, d) -> "$nm, att. $d" }
}

val Person.shownNameForPerson: String get() = this.matchOrThrow {
it.of(Person::name.of(Name::firstName), Person::age.suchThat { it < 18 }) then { (fn, _) -> fn }
it.of(Person::name.of(Name::firstName), Person::age.takeIf { it < 18 }) then { (fn, _) -> fn }
it.of(Person::name.of(Name::firstName, Name::lastName)) then { (fn, ln) -> "Sir/Madam $fn $ln" }
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package arrow.match

public actual class DoesNotMatch: Throwable()
2 changes: 1 addition & 1 deletion arrow-libs/optics/arrow-optics/api/arrow-optics.api
Original file line number Diff line number Diff line change
Expand Up @@ -535,7 +535,7 @@ public final class arrow/optics/match/CombinatorsKt {
public static final fun it (Larrow/optics/POptional;Larrow/optics/POptional;Larrow/optics/POptional;Larrow/optics/POptional;)Larrow/optics/POptional;
public static final fun it (Larrow/optics/POptional;Larrow/optics/POptional;Larrow/optics/POptional;Larrow/optics/POptional;Larrow/optics/POptional;)Larrow/optics/POptional;
public static final fun predicate (Lkotlin/jvm/functions/Function1;)Larrow/optics/POptional;
public static final fun suchThat (Larrow/optics/POptional;Lkotlin/jvm/functions/Function1;)Larrow/optics/POptional;
public static final fun takeIf (Larrow/optics/POptional;Lkotlin/jvm/functions/Function1;)Larrow/optics/POptional;
}

public final class arrow/optics/match/MatchKt {
Expand Down
2 changes: 1 addition & 1 deletion arrow-libs/optics/arrow-optics/api/arrow-optics.klib.api
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.core.raise/Raise<arrow.optic
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/PLens<#A, #A, kotlin.collections/Set<#B>, kotlin.collections/Set<#B>>).arrow.optics.dsl/at(#B): arrow.optics/PLens<#A, #A, kotlin/Boolean, kotlin/Boolean> // arrow.optics.dsl/at|at@arrow.optics.PLens<0:0,0:0,kotlin.collections.Set<0:1>,kotlin.collections.Set<0:1>>(0:1){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/PLens<#B, #B, kotlin.collections/List<#A>, kotlin.collections/List<#A>>).arrow.optics/get(kotlin/Int): arrow.optics/POptional<#B, #B, #A, #A> // arrow.optics/get|get@arrow.optics.PLens<0:1,0:1,kotlin.collections.List<0:0>,kotlin.collections.List<0:0>>(kotlin.Int){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/POptional<#A, #A, #B, #B>).arrow.optics.match/ifEquals(#B): arrow.optics/POptional<#A, #A, #B, #B> // arrow.optics.match/ifEquals|ifEquals@arrow.optics.POptional<0:0,0:0,0:1,0:1>(0:1){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/POptional<#A, #A, #B, #B>).arrow.optics.match/suchThat(kotlin/Function1<#B, kotlin/Boolean>): arrow.optics/POptional<#A, #A, #B, #B> // arrow.optics.match/suchThat|suchThat@arrow.optics.POptional<0:0,0:0,0:1,0:1>(kotlin.Function1<0:1,kotlin.Boolean>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/POptional<#A, #A, #B, #B>).arrow.optics.match/takeIf(kotlin/Function1<#B, kotlin/Boolean>): arrow.optics/POptional<#A, #A, #B, #B> // arrow.optics.match/takeIf|takeIf@arrow.optics.POptional<0:0,0:0,0:1,0:1>(kotlin.Function1<0:1,kotlin.Boolean>){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/POptional<#A, #A, arrow.core/NonEmptyList<#B>, arrow.core/NonEmptyList<#B>>).arrow.optics.dsl/index(kotlin/Int): arrow.optics/POptional<#A, #A, #B, #B> // arrow.optics.dsl/index|index@arrow.optics.POptional<0:0,0:0,arrow.core.NonEmptyList<0:1>,arrow.core.NonEmptyList<0:1>>(kotlin.Int){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/POptional<#A, #A, kotlin.collections/List<#B>, kotlin.collections/List<#B>>).arrow.optics.dsl/index(kotlin/Int): arrow.optics/POptional<#A, #A, #B, #B> // arrow.optics.dsl/index|index@arrow.optics.POptional<0:0,0:0,kotlin.collections.List<0:1>,kotlin.collections.List<0:1>>(kotlin.Int){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
final fun <#A: kotlin/Any?, #B: kotlin/Any?> (arrow.optics/POptional<#A, #A, kotlin.collections/Set<#B>, kotlin.collections/Set<#B>>).arrow.optics.dsl/at(#B): arrow.optics/POptional<#A, #A, kotlin/Boolean, kotlin/Boolean> // arrow.optics.dsl/at|at@arrow.optics.POptional<0:0,0:0,kotlin.collections.Set<0:1>,kotlin.collections.Set<0:1>>(0:1){0§<kotlin.Any?>;1§<kotlin.Any?>}[0]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import arrow.core.raise.ensure
import arrow.optics.Optional
import arrow.optics.Prism

public fun <S, A> Optional<S, A>.suchThat(
public fun <S, A> Optional<S, A>.takeIf(
predicate: (A) -> Boolean,
): Optional<S, A> = this.compose(predicate(predicate))

Expand All @@ -24,7 +24,7 @@ public fun <A> predicate(
set = { x, _ -> x }
)

public fun <S, A> Optional<S, A>.ifEquals(value: A): Optional<S, A> = this.suchThat { it == value }
public fun <S, A> Optional<S, A>.ifEquals(value: A): Optional<S, A> = this.takeIf { it == value }

public fun <A> equalsTo(value: A): Optional<A, A> = predicate { it == value }

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ data class Company(
}

val User.shownName: String get() = this.matchOrThrow {
User.person(Person.name(Name.firstName), Person.age.suchThat { it < 18 }) then { (fn, _) -> fn }
User.person(Person.name(Name.firstName), Person.age.takeIf { it < 18 }) then { (fn, _) -> fn }
User.person(Person.name(Name.firstName, Name.lastName)) then { (fn, ln) -> "Sir/Madam $fn $ln" }
User.company(Company.name, Company.director(Name.lastName)) then { (nm, d) -> "$nm, att. $d" }
}

val Person.shownNameForPerson: String get() = this.matchOrThrow {
it(Person.name(Name.firstName), Person.age.suchThat { it < 18 }) then { (fn, _) -> fn }
it(Person.name(Name.firstName), Person.age.takeIf { it < 18 }) then { (fn, _) -> fn }
it(Person.name(Name.firstName, Name.lastName)) then { (fn, ln) -> "Sir/Madam $fn $ln" }
}

Expand Down

0 comments on commit 3615561

Please sign in to comment.