diff --git a/array/generic/array.go b/array/generic/array.go index 3460020..a0fb83b 100644 --- a/array/generic/array.go +++ b/array/generic/array.go @@ -187,6 +187,14 @@ func IsEmpty[AS ~[]A, A any](as AS) bool { return array.IsEmpty(as) } +func IsNil[GA ~[]A, A any](as GA) bool { + return array.IsNil(as) +} + +func IsNonNil[GA ~[]A, A any](as GA) bool { + return array.IsNonNil(as) +} + func Match[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(AS) B) func(AS) B { return func(as AS) B { if IsEmpty(as) { diff --git a/optics/iso/iso.go b/optics/iso/iso.go index f7029d7..7d5f548 100644 --- a/optics/iso/iso.go +++ b/optics/iso/iso.go @@ -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 diff --git a/optics/lens/lens.go b/optics/lens/lens.go index 2df8296..807fd3d 100644 --- a/optics/lens/lens.go +++ b/optics/lens/lens.go @@ -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]() diff --git a/optics/lens/lens_test.go b/optics/lens/lens_test.go index a228350..ccbbf90 100644 --- a/optics/lens/lens_test.go +++ b/optics/lens/lens_test.go @@ -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 diff --git a/optics/lens/record/generic/record.go b/optics/lens/record/generic/record.go index 260ec12..9d240e6 100644 --- a/optics/lens/record/generic/record.go +++ b/optics/lens/record/generic/record.go @@ -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)) } diff --git a/optics/lens/record/record.go b/optics/lens/record/record.go index 3970a09..cefe725 100644 --- a/optics/lens/record/record.go +++ b/optics/lens/record/record.go @@ -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) } diff --git a/optics/lens/record/record_test.go b/optics/lens/record/record_test.go index d492d98..0019d01 100644 --- a/optics/lens/record/record_test.go +++ b/optics/lens/record/record_test.go @@ -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})) diff --git a/optics/lens/testing/laws_test.go b/optics/lens/testing/laws_test.go index b5049de..382b69e 100644 --- a/optics/lens/testing/laws_test.go +++ b/optics/lens/testing/laws_test.go @@ -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), )