1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-10 22:31:32 +02:00

fix: order of parameters in optics

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-09-10 21:49:44 +02:00
parent 38120764e7
commit ff1b6faf84
8 changed files with 22 additions and 14 deletions

View File

@@ -67,7 +67,7 @@ func Modify[S, A any](f func(A) A) func(Iso[S, A]) func(S) S {
}
// Wrap wraps the value
func Unwrap[S, A any](s S) func(Iso[S, A]) A {
func Unwrap[A, S any](s S) func(Iso[S, A]) A {
return func(sa Iso[S, A]) A {
return sa.Get(s)
}
@@ -81,8 +81,8 @@ func Wrap[S, A any](a A) func(Iso[S, A]) S {
}
// From wraps the value
func To[S, A any](s S) func(Iso[S, A]) A {
return Unwrap[S, A](s)
func To[A, S any](s S) func(Iso[S, A]) A {
return Unwrap[A, S](s)
}
// To unwraps the value

View File

@@ -121,7 +121,7 @@ func Compose[S, A, B any](ab Lens[A, B]) func(Lens[S, A]) Lens[S, B] {
// the getter returns an `Option[B]` because the container `A` could already be an option
// if the setter is invoked with `Some[B]` then the value of `B` will be set, potentially on a default value of `A` if `A` did not exist
// if the setter is invoked with `None[B]` then the container `A` is reset to `None[A]` because this is the only way to remove `B`
func ComposeOption[S, A, B any](defaultA A) func(ab Lens[A, B]) func(Lens[S, O.Option[A]]) Lens[S, O.Option[B]] {
func ComposeOption[S, B, A any](defaultA A) func(ab Lens[A, B]) func(Lens[S, O.Option[A]]) Lens[S, O.Option[B]] {
defa := F.Constant(defaultA)
return func(ab Lens[A, B]) func(Lens[S, O.Option[A]]) Lens[S, O.Option[B]] {
foldab := O.Fold(O.None[B], F.Flow2(ab.Get, O.Some[B]))
@@ -172,7 +172,7 @@ func ComposeOption[S, A, B any](defaultA A) func(ab Lens[A, B]) func(Lens[S, O.O
// if the setter is called with `Some[B]` and `A` does not exist, the default of 'A' is updated with `B`
// if the setter is called with `None[B]` and `A` does not exist this is the identity operation on 'S'
// if the setter is called with `None[B]` and `A` does exist, 'B' is removed from 'A'
func ComposeOptions[S, A, B any](defaultA A) func(ab Lens[A, O.Option[B]]) func(Lens[S, O.Option[A]]) Lens[S, O.Option[B]] {
func ComposeOptions[S, B, A any](defaultA A) func(ab Lens[A, O.Option[B]]) func(Lens[S, O.Option[A]]) Lens[S, O.Option[B]] {
defa := F.Constant(defaultA)
noops := F.Constant(F.Identity[S])
noneb := O.None[B]()

View File

@@ -209,7 +209,7 @@ func TestComposeOption(t *testing.T) {
// compose lenses
lens := F.Pipe1(
inner,
ComposeOption[Outer, *Inner, int](defaultInner)(value),
ComposeOption[Outer, int](defaultInner)(value),
)
outer1 := Outer{inner: &Inner{Value: 1, Foo: "a"}}
// the checks
@@ -235,7 +235,7 @@ func TestComposeOptions(t *testing.T) {
// compose lenses
lens := F.Pipe1(
inner,
ComposeOptions[OuterOpt, *InnerOpt, *int](defaultInner)(value),
ComposeOptions[OuterOpt, *int](defaultInner)(value),
)
// additional settings
defaultValue2 := 2

View File

@@ -24,7 +24,7 @@ import (
)
// AtRecord returns a lens that focusses on a value in a record
func AtRecord[M ~map[K]V, K comparable, V any](key K) L.Lens[M, O.Option[V]] {
func AtRecord[M ~map[K]V, V any, K comparable](key K) L.Lens[M, O.Option[V]] {
addKey := F.Bind1of2(RR.UpsertAt[M, K, V])(key)
delKey := F.Bind1of1(RR.DeleteAt[M, K, V])(key)
fold := O.Fold(
@@ -44,6 +44,6 @@ func AtRecord[M ~map[K]V, K comparable, V any](key K) L.Lens[M, O.Option[V]] {
}
// AtKey returns a `Lens` focused on a required key of a `ReadonlyRecord`
func AtKey[M ~map[K]V, S any, K comparable, V any](key K) func(sa L.Lens[S, M]) L.Lens[S, O.Option[V]] {
func AtKey[M ~map[K]V, S any, V any, K comparable](key K) func(sa L.Lens[S, M]) L.Lens[S, O.Option[V]] {
return L.Compose[S](AtRecord[M](key))
}

View File

@@ -22,11 +22,11 @@ import (
)
// AtRecord returns a lens that focusses on a value in a record
func AtRecord[K comparable, V any](key K) L.Lens[map[K]V, O.Option[V]] {
func AtRecord[V any, K comparable](key K) L.Lens[map[K]V, O.Option[V]] {
return G.AtRecord[map[K]V](key)
}
// AtKey returns a `Lens` focused on a required key of a `ReadonlyRecord`
func AtKey[S any, K comparable, V any](key K) func(sa L.Lens[S, map[K]V]) L.Lens[S, O.Option[V]] {
func AtKey[S any, V any, K comparable](key K) func(sa L.Lens[S, map[K]V]) L.Lens[S, O.Option[V]] {
return G.AtKey[map[K]V, S](key)
}

View File

@@ -31,7 +31,7 @@ type (
func TestAtKey(t *testing.T) {
sa := F.Pipe1(
L.Id[S](),
AtKey[S, string, int]("a"),
AtKey[S, int]("a"),
)
assert.Equal(t, O.Some(1), sa.Get(S{"a": 1}))

View File

@@ -198,7 +198,7 @@ func TestOuterLensLaws(t *testing.T) {
eqValue := EQT.Eq[int]()
eqOptValue := O.Eq(eqValue)
// lens to access a value from outer
valueFromOuter := L.ComposeOption[*Outer, *Inner, int](&defaultInner)(valueLens)(outerLens)
valueFromOuter := L.ComposeOption[*Outer, int](&defaultInner)(valueLens)(outerLens)
// try to access the value, this should get an option
assert.True(t, eqOptValue.Equals(valueFromOuter.Get(&emptyOuter), O.None[int]()))
// update the object
@@ -234,7 +234,7 @@ func TestOuterOptLensLaws(t *testing.T) {
valueFromOuter := F.Pipe3(
valueOptLens,
LI.Compose[*InnerOpt](intIso),
L.ComposeOptions[*OuterOpt, *InnerOpt, int](&defaultInnerOpt),
L.ComposeOptions[*OuterOpt, int](&defaultInnerOpt),
I.Ap[L.Lens[*OuterOpt, O.Option[int]]](outerOptLens),
)