mirror of
https://github.com/IBM/fp-go.git
synced 2025-11-27 22:28:29 +02:00
fix: order of parameters in optics
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@@ -187,6 +187,14 @@ func IsEmpty[AS ~[]A, A any](as AS) bool {
|
|||||||
return array.IsEmpty(as)
|
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 {
|
func Match[AS ~[]A, A, B any](onEmpty func() B, onNonEmpty func(AS) B) func(AS) B {
|
||||||
return func(as AS) B {
|
return func(as AS) B {
|
||||||
if IsEmpty(as) {
|
if IsEmpty(as) {
|
||||||
|
|||||||
@@ -67,7 +67,7 @@ func Modify[S, A any](f func(A) A) func(Iso[S, A]) func(S) S {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Wrap wraps the value
|
// 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 func(sa Iso[S, A]) A {
|
||||||
return sa.Get(s)
|
return sa.Get(s)
|
||||||
}
|
}
|
||||||
@@ -81,8 +81,8 @@ func Wrap[S, A any](a A) func(Iso[S, A]) S {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// From wraps the value
|
// From wraps the value
|
||||||
func To[S, A any](s S) func(Iso[S, A]) A {
|
func To[A, S any](s S) func(Iso[S, A]) A {
|
||||||
return Unwrap[S, A](s)
|
return Unwrap[A, S](s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// To unwraps the value
|
// To unwraps the value
|
||||||
|
|||||||
@@ -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
|
// 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 `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`
|
// 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)
|
defa := F.Constant(defaultA)
|
||||||
return func(ab Lens[A, B]) func(Lens[S, O.Option[A]]) Lens[S, O.Option[B]] {
|
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]))
|
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 `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 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'
|
// 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)
|
defa := F.Constant(defaultA)
|
||||||
noops := F.Constant(F.Identity[S])
|
noops := F.Constant(F.Identity[S])
|
||||||
noneb := O.None[B]()
|
noneb := O.None[B]()
|
||||||
|
|||||||
@@ -209,7 +209,7 @@ func TestComposeOption(t *testing.T) {
|
|||||||
// compose lenses
|
// compose lenses
|
||||||
lens := F.Pipe1(
|
lens := F.Pipe1(
|
||||||
inner,
|
inner,
|
||||||
ComposeOption[Outer, *Inner, int](defaultInner)(value),
|
ComposeOption[Outer, int](defaultInner)(value),
|
||||||
)
|
)
|
||||||
outer1 := Outer{inner: &Inner{Value: 1, Foo: "a"}}
|
outer1 := Outer{inner: &Inner{Value: 1, Foo: "a"}}
|
||||||
// the checks
|
// the checks
|
||||||
@@ -235,7 +235,7 @@ func TestComposeOptions(t *testing.T) {
|
|||||||
// compose lenses
|
// compose lenses
|
||||||
lens := F.Pipe1(
|
lens := F.Pipe1(
|
||||||
inner,
|
inner,
|
||||||
ComposeOptions[OuterOpt, *InnerOpt, *int](defaultInner)(value),
|
ComposeOptions[OuterOpt, *int](defaultInner)(value),
|
||||||
)
|
)
|
||||||
// additional settings
|
// additional settings
|
||||||
defaultValue2 := 2
|
defaultValue2 := 2
|
||||||
|
|||||||
@@ -24,7 +24,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// AtRecord returns a lens that focusses on a value in a record
|
// 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)
|
addKey := F.Bind1of2(RR.UpsertAt[M, K, V])(key)
|
||||||
delKey := F.Bind1of1(RR.DeleteAt[M, K, V])(key)
|
delKey := F.Bind1of1(RR.DeleteAt[M, K, V])(key)
|
||||||
fold := O.Fold(
|
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`
|
// 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))
|
return L.Compose[S](AtRecord[M](key))
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -22,11 +22,11 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// AtRecord returns a lens that focusses on a value in a record
|
// 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)
|
return G.AtRecord[map[K]V](key)
|
||||||
}
|
}
|
||||||
|
|
||||||
// AtKey returns a `Lens` focused on a required key of a `ReadonlyRecord`
|
// 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)
|
return G.AtKey[map[K]V, S](key)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ type (
|
|||||||
func TestAtKey(t *testing.T) {
|
func TestAtKey(t *testing.T) {
|
||||||
sa := F.Pipe1(
|
sa := F.Pipe1(
|
||||||
L.Id[S](),
|
L.Id[S](),
|
||||||
AtKey[S, string, int]("a"),
|
AtKey[S, int]("a"),
|
||||||
)
|
)
|
||||||
|
|
||||||
assert.Equal(t, O.Some(1), sa.Get(S{"a": 1}))
|
assert.Equal(t, O.Some(1), sa.Get(S{"a": 1}))
|
||||||
|
|||||||
@@ -198,7 +198,7 @@ func TestOuterLensLaws(t *testing.T) {
|
|||||||
eqValue := EQT.Eq[int]()
|
eqValue := EQT.Eq[int]()
|
||||||
eqOptValue := O.Eq(eqValue)
|
eqOptValue := O.Eq(eqValue)
|
||||||
// lens to access a value from outer
|
// 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
|
// try to access the value, this should get an option
|
||||||
assert.True(t, eqOptValue.Equals(valueFromOuter.Get(&emptyOuter), O.None[int]()))
|
assert.True(t, eqOptValue.Equals(valueFromOuter.Get(&emptyOuter), O.None[int]()))
|
||||||
// update the object
|
// update the object
|
||||||
@@ -234,7 +234,7 @@ func TestOuterOptLensLaws(t *testing.T) {
|
|||||||
valueFromOuter := F.Pipe3(
|
valueFromOuter := F.Pipe3(
|
||||||
valueOptLens,
|
valueOptLens,
|
||||||
LI.Compose[*InnerOpt](intIso),
|
LI.Compose[*InnerOpt](intIso),
|
||||||
L.ComposeOptions[*OuterOpt, *InnerOpt, int](&defaultInnerOpt),
|
L.ComposeOptions[*OuterOpt, int](&defaultInnerOpt),
|
||||||
I.Ap[L.Lens[*OuterOpt, O.Option[int]]](outerOptLens),
|
I.Ap[L.Lens[*OuterOpt, O.Option[int]]](outerOptLens),
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user