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

fix: add match example

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-09-24 22:15:01 +02:00
parent 7b82e87bf3
commit 7ef6eb524b
5 changed files with 75 additions and 10 deletions

View File

@@ -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])
}

View File

@@ -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)))
}

View File

@@ -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),
)))
)

View File

@@ -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](

View File

@@ -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
}