diff --git a/either/either.go b/either/either.go index 24498ad..cdced1f 100644 --- a/either/either.go +++ b/either/either.go @@ -92,11 +92,11 @@ func MonadChainTo[E, A, B any](ma Either[E, A], mb Either[E, B]) Either[E, B] { } func MonadChainOptionK[A, B, E any](onNone func() E, ma Either[E, A], f func(A) O.Option[B]) Either[E, B] { - return MonadChain(ma, F.Flow2(f, FromOption[E, B](onNone))) + return MonadChain(ma, F.Flow2(f, FromOption[B](onNone))) } func ChainOptionK[A, B, E any](onNone func() E) func(func(A) O.Option[B]) func(Either[E, A]) Either[E, B] { - from := FromOption[E, B](onNone) + from := FromOption[B](onNone) return func(f func(A) O.Option[B]) func(Either[E, A]) Either[E, B] { return Chain(F.Flow2(f, from)) } @@ -142,7 +142,7 @@ func Sequence3[E, T1, T2, T3, R any](f func(T1, T2, T3) Either[E, R]) func(Eithe } } -func FromOption[E, A any](onNone func() E) func(O.Option[A]) Either[E, A] { +func FromOption[A, E any](onNone func() E) func(O.Option[A]) Either[E, A] { return O.Fold(F.Nullary2(onNone, Left[A, E]), Right[E, A]) } diff --git a/either/either_test.go b/either/either_test.go index 1b18801..85e6755 100644 --- a/either/either_test.go +++ b/either/either_test.go @@ -112,6 +112,6 @@ func TestChainOptionK(t *testing.T) { } func TestFromOption(t *testing.T) { - assert.Equal(t, Left[int]("none"), FromOption[string, int](F.Constant("none"))(O.None[int]())) - assert.Equal(t, Right[string](1), FromOption[string, int](F.Constant("none"))(O.Some(1))) + assert.Equal(t, Left[int]("none"), FromOption[int](F.Constant("none"))(O.None[int]())) + assert.Equal(t, Right[string](1), FromOption[int](F.Constant("none"))(O.Some(1))) } diff --git a/http/utils.go b/http/utils.go index 74580ec..b6226b3 100644 --- a/http/utils.go +++ b/http/utils.go @@ -57,7 +57,7 @@ var ( GetHeader, R.Lookup[H.Header](HeaderContentType), O.Chain(A.First[string]), - E.FromOption[error, string](errors.OnNone("unable to access the [%s] header", HeaderContentType)), + E.FromOption[string](errors.OnNone("unable to access the [%s] header", HeaderContentType)), E.ChainFirst(validateJsonContentTypeString), ))) ) diff --git a/internal/fromeither/either.go b/internal/fromeither/either.go index 7ebe9ea..124386c 100644 --- a/internal/fromeither/either.go +++ b/internal/fromeither/either.go @@ -22,8 +22,8 @@ import ( O "github.com/IBM/fp-go/option" ) -func FromOption[E, A, HKTEA any](fromEither func(ET.Either[E, A]) HKTEA, onNone func() E) func(ma O.Option[A]) HKTEA { - return F.Flow2(ET.FromOption[E, A](onNone), fromEither) +func FromOption[A, HKTEA, E any](fromEither func(ET.Either[E, A]) HKTEA, onNone func() E) func(ma O.Option[A]) HKTEA { + return F.Flow2(ET.FromOption[A](onNone), fromEither) } func FromPredicate[E, A, HKTEA any](fromEither func(ET.Either[E, A]) HKTEA, pred func(A) bool, onFalse func(A) E) func(A) HKTEA { @@ -45,7 +45,7 @@ func MonadFromOption[E, A, HKTEA any]( ) } -func FromOptionK[E, A, B, HKTEB any]( +func FromOptionK[A, E, B, HKTEB any]( fromEither func(ET.Either[E, B]) HKTEB, onNone func() E) func(f func(A) O.Option[B]) func(A) HKTEB { // helper @@ -65,7 +65,7 @@ func ChainOptionK[A, E, B, HKTEA, HKTEB any]( fromEither func(ET.Either[E, B]) HKTEB, onNone func() E, ) func(f func(A) O.Option[B]) func(ma HKTEA) HKTEB { - return F.Flow2(FromOptionK[E, A](fromEither, onNone), F.Bind1st(F.Bind2nd[HKTEA, func(A) HKTEB, HKTEB], mchain)) + return F.Flow2(FromOptionK[A](fromEither, onNone), F.Bind1st(F.Bind2nd[HKTEA, func(A) HKTEB, HKTEB], mchain)) } func MonadChainFirstEitherK[A, E, B, HKTEA, HKTEB any]( diff --git a/samples/match/examples_match_test.go b/samples/match/examples_match_test.go new file mode 100644 index 0000000..12f4389 --- /dev/null +++ b/samples/match/examples_match_test.go @@ -0,0 +1,65 @@ +// Copyright (c) 2023 IBM Corp. +// All rights reserved. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package match + +import ( + "fmt" + + E "github.com/IBM/fp-go/either" + "github.com/IBM/fp-go/errors" + F "github.com/IBM/fp-go/function" + O "github.com/IBM/fp-go/option" + S "github.com/IBM/fp-go/string" +) + +type Thing struct { + Name string +} + +func (t Thing) GetName() string { + return t.Name +} + +var ( + // func(Thing) Either[error, string] + getName = F.Flow2( + Thing.GetName, + E.FromPredicate(S.IsNonEmpty, errors.OnSome[string]("value [%s] is empty")), + ) + + // func(option.Option[Thing]) Either[error, string] + GetName = F.Flow2( + E.FromOption[Thing](errors.OnNone("value is none")), + E.Chain(getName), + ) +) + +func ExampleEither_match() { + + oThing := O.Of(Thing{"Carsten"}) + + res := F.Pipe2( + oThing, + GetName, + E.Fold(S.Format[error]("failed with error %v"), S.Format[string]("get value %s")), + ) + + fmt.Println(res) + + // Output: + // get value Carsten + +}