Skip to content

Commit

Permalink
Merge pull request #41 from IBM/cleue-add-missing-functions
Browse files Browse the repository at this point in the history
fix: order of parameters in optics
  • Loading branch information
CarstenLeue authored Sep 10, 2023
2 parents 3812076 + ff1b6fa commit 7da9de6
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 14 deletions.
8 changes: 8 additions & 0 deletions array/generic/array.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
6 changes: 3 additions & 3 deletions optics/iso/iso.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions optics/lens/lens.go
Original file line number Diff line number Diff line change
Expand Up @@ -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]))
Expand Down Expand Up @@ -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]()
Expand Down
4 changes: 2 additions & 2 deletions optics/lens/lens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions optics/lens/record/generic/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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))
}
4 changes: 2 additions & 2 deletions optics/lens/record/record.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
2 changes: 1 addition & 1 deletion optics/lens/record/record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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}))
Expand Down
4 changes: 2 additions & 2 deletions optics/lens/testing/laws_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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),
)

Expand Down

0 comments on commit 7da9de6

Please sign in to comment.