mirror of
https://github.com/IBM/fp-go.git
synced 2025-07-17 01:32:23 +02:00
fix: next small step towards StateReaderIOEither
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
@ -50,9 +50,7 @@ func MonadMap[
|
|||||||
|
|
||||||
return F.Flow2(
|
return F.Flow2(
|
||||||
fa,
|
fa,
|
||||||
F.Bind2nd(fmap, func(a P.Pair[A, S]) P.Pair[B, S] {
|
F.Bind2nd(fmap, P.Map[S](f)),
|
||||||
return P.MakePair(f(P.Head(a)), P.Tail(a))
|
|
||||||
}),
|
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -67,9 +65,7 @@ func Map[
|
|||||||
|
|
||||||
f func(A) B,
|
f func(A) B,
|
||||||
) func(HKTSA) HKTSB {
|
) func(HKTSA) HKTSB {
|
||||||
mp := fmap(func(a P.Pair[A, S]) P.Pair[B, S] {
|
mp := fmap(P.Map[S](f))
|
||||||
return P.MakePair(f(P.Head(a)), P.Tail(a))
|
|
||||||
})
|
|
||||||
|
|
||||||
return func(fa HKTSA) HKTSB {
|
return func(fa HKTSA) HKTSB {
|
||||||
return F.Flow2(
|
return F.Flow2(
|
||||||
@ -121,3 +117,51 @@ func Chain[
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MonadAp[
|
||||||
|
HKTSA ~func(S) HKTA,
|
||||||
|
HKTSB ~func(S) HKTB,
|
||||||
|
HKTSAB ~func(S) HKTAB,
|
||||||
|
HKTA,
|
||||||
|
HKTB,
|
||||||
|
HKTAB,
|
||||||
|
|
||||||
|
S, A, B any,
|
||||||
|
](
|
||||||
|
fmap func(HKTA, func(P.Pair[A, S]) P.Pair[B, S]) HKTB,
|
||||||
|
fchain func(HKTAB, func(P.Pair[func(A) B, S]) HKTB) HKTB,
|
||||||
|
|
||||||
|
fab HKTSAB,
|
||||||
|
fa HKTSA,
|
||||||
|
) HKTSB {
|
||||||
|
return func(s S) HKTB {
|
||||||
|
return fchain(fab(s), func(ab P.Pair[func(A) B, S]) HKTB {
|
||||||
|
return fmap(fa(P.Tail(ab)), P.Map[S](P.Head(ab)))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func Ap[
|
||||||
|
HKTSA ~func(S) HKTA,
|
||||||
|
HKTSB ~func(S) HKTB,
|
||||||
|
HKTSAB ~func(S) HKTAB,
|
||||||
|
HKTA,
|
||||||
|
HKTB,
|
||||||
|
HKTAB,
|
||||||
|
|
||||||
|
S, A, B any,
|
||||||
|
](
|
||||||
|
fmap func(func(P.Pair[A, S]) P.Pair[B, S]) func(HKTA) HKTB,
|
||||||
|
fchain func(func(P.Pair[func(A) B, S]) HKTB) func(HKTAB) HKTB,
|
||||||
|
|
||||||
|
fa HKTSA,
|
||||||
|
) func(HKTSAB) HKTSB {
|
||||||
|
return func(fab HKTSAB) HKTSB {
|
||||||
|
return F.Flow2(
|
||||||
|
fab,
|
||||||
|
fchain(func(ab P.Pair[func(A) B, S]) HKTB {
|
||||||
|
return fmap(P.Map[S](P.Head(ab)))(fa(P.Tail(ab)))
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
35
statereaderioeither/eq.go
Normal file
35
statereaderioeither/eq.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
// Copyright (c) 2024 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 statereaderioeither
|
||||||
|
|
||||||
|
import (
|
||||||
|
EQ "github.com/IBM/fp-go/eq"
|
||||||
|
P "github.com/IBM/fp-go/pair"
|
||||||
|
RIOE "github.com/IBM/fp-go/readerioeither"
|
||||||
|
G "github.com/IBM/fp-go/statereaderioeither/generic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Eq implements the equals predicate for values contained in the [StateReaderIOEither] monad
|
||||||
|
func Eq[
|
||||||
|
S, R, E, A any](eqr EQ.Eq[RIOE.ReaderIOEither[R, E, P.Pair[A, S]]]) func(S) EQ.Eq[StateReaderIOEither[S, R, E, A]] {
|
||||||
|
return G.Eq[StateReaderIOEither[S, R, E, A]](eqr)
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||||
|
func FromStrictEquals[
|
||||||
|
S, R any, E, A comparable]() func(R) func(S) EQ.Eq[StateReaderIOEither[S, R, E, A]] {
|
||||||
|
return G.FromStrictEquals[StateReaderIOEither[S, R, E, A]]()
|
||||||
|
}
|
49
statereaderioeither/generic/eq.go
Normal file
49
statereaderioeither/generic/eq.go
Normal file
@ -0,0 +1,49 @@
|
|||||||
|
// Copyright (c) 2024 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 generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
ET "github.com/IBM/fp-go/either"
|
||||||
|
EQ "github.com/IBM/fp-go/eq"
|
||||||
|
F "github.com/IBM/fp-go/function"
|
||||||
|
P "github.com/IBM/fp-go/pair"
|
||||||
|
G "github.com/IBM/fp-go/readerioeither/generic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Eq implements the equals predicate for values contained in the [StateReaderIOEither] monad
|
||||||
|
func Eq[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
S, R, E, A any](eqr EQ.Eq[RIOEA]) func(S) EQ.Eq[SRIOEA] {
|
||||||
|
return func(s S) EQ.Eq[SRIOEA] {
|
||||||
|
return EQ.FromEquals(func(l, r SRIOEA) bool {
|
||||||
|
return eqr.Equals(l(s), r(s))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// FromStrictEquals constructs an [EQ.Eq] from the canonical comparison function
|
||||||
|
func FromStrictEquals[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
S, R any, E, A comparable]() func(R) func(S) EQ.Eq[SRIOEA] {
|
||||||
|
return F.Flow2(
|
||||||
|
G.FromStrictEquals[RIOEA](),
|
||||||
|
Eq[SRIOEA, RIOEA, IOEA, S, R, E, A],
|
||||||
|
)
|
||||||
|
}
|
159
statereaderioeither/generic/monad.go
Normal file
159
statereaderioeither/generic/monad.go
Normal file
@ -0,0 +1,159 @@
|
|||||||
|
// Copyright (c) 2024 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 generic
|
||||||
|
|
||||||
|
import (
|
||||||
|
ET "github.com/IBM/fp-go/either"
|
||||||
|
"github.com/IBM/fp-go/internal/applicative"
|
||||||
|
"github.com/IBM/fp-go/internal/functor"
|
||||||
|
"github.com/IBM/fp-go/internal/monad"
|
||||||
|
"github.com/IBM/fp-go/internal/pointed"
|
||||||
|
P "github.com/IBM/fp-go/pair"
|
||||||
|
)
|
||||||
|
|
||||||
|
type stateReaderIOEitherPointed[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
S, R, E, A any,
|
||||||
|
] struct{}
|
||||||
|
|
||||||
|
type stateReaderIOEitherFunctor[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
] struct{}
|
||||||
|
|
||||||
|
type stateReaderIOEitherApplicative[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
SRIOEAB ~func(S) RIOEAB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
RIOEAB ~func(R) IOEAB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
IOEAB ~func() ET.Either[E, P.Pair[func(A) B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
] struct{}
|
||||||
|
|
||||||
|
type stateReaderIOEitherMonad[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
SRIOEAB ~func(S) RIOEAB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
RIOEAB ~func(R) IOEAB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
IOEAB ~func() ET.Either[E, P.Pair[func(A) B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
] struct{}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherPointed[SRIOEA, RIOEA, IOEA, S, R, E, A]) Of(a A) SRIOEA {
|
||||||
|
return Of[SRIOEA](a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherMonad[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Of(a A) SRIOEA {
|
||||||
|
return Of[SRIOEA](a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherApplicative[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Of(a A) SRIOEA {
|
||||||
|
return Of[SRIOEA](a)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherMonad[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Map(f func(A) B) func(SRIOEA) SRIOEB {
|
||||||
|
return Map[SRIOEA, SRIOEB](f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherApplicative[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Map(f func(A) B) func(SRIOEA) SRIOEB {
|
||||||
|
return Map[SRIOEA, SRIOEB](f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherFunctor[SRIOEA, SRIOEB, RIOEA, RIOEB, IOEA, IOEB, S, R, E, A, B]) Map(f func(A) B) func(SRIOEA) SRIOEB {
|
||||||
|
return Map[SRIOEA, SRIOEB](f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherMonad[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Chain(f func(A) SRIOEB) func(SRIOEA) SRIOEB {
|
||||||
|
return Chain[SRIOEA, SRIOEB](f)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherMonad[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Ap(fa SRIOEA) func(SRIOEAB) SRIOEB {
|
||||||
|
return Ap[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B](fa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (o *stateReaderIOEitherApplicative[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]) Ap(fa SRIOEA) func(SRIOEAB) SRIOEB {
|
||||||
|
return Ap[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B](fa)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Pointed implements the pointed operations for [StateReaderIOEither]
|
||||||
|
func Pointed[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
S, R, E, A any,
|
||||||
|
]() pointed.Pointed[A, SRIOEA] {
|
||||||
|
return &stateReaderIOEitherPointed[SRIOEA, RIOEA, IOEA, S, R, E, A]{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functor implements the functor operations for [StateReaderIOEither]
|
||||||
|
func Functor[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
]() functor.Functor[A, B, SRIOEA, SRIOEB] {
|
||||||
|
return &stateReaderIOEitherFunctor[SRIOEA, SRIOEB, RIOEA, RIOEB, IOEA, IOEB, S, R, E, A, B]{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Applicative implements the applicative operations for [StateReaderIOEither]
|
||||||
|
func Applicative[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
SRIOEAB ~func(S) RIOEAB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
RIOEAB ~func(R) IOEAB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
IOEAB ~func() ET.Either[E, P.Pair[func(A) B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
]() applicative.Applicative[A, B, SRIOEA, SRIOEB, SRIOEAB] {
|
||||||
|
return &stateReaderIOEitherApplicative[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]{}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Monad implements the monadic operations for [StateReaderIOEither]
|
||||||
|
func Monad[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
SRIOEAB ~func(S) RIOEAB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
RIOEAB ~func(R) IOEAB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
IOEAB ~func() ET.Either[E, P.Pair[func(A) B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
]() monad.Monad[A, B, SRIOEA, SRIOEB, SRIOEAB] {
|
||||||
|
return &stateReaderIOEitherMonad[SRIOEA, SRIOEB, SRIOEAB, RIOEA, RIOEB, RIOEAB, IOEA, IOEB, IOEAB, S, R, E, A, B]{}
|
||||||
|
}
|
@ -104,3 +104,42 @@ func Chain[
|
|||||||
f,
|
f,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MonadAp[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
SRIOEAB ~func(S) RIOEAB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
RIOEAB ~func(R) IOEAB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
IOEAB ~func() ET.Either[E, P.Pair[func(A) B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
](fab SRIOEAB, fa SRIOEA) SRIOEB {
|
||||||
|
return ST.MonadAp[SRIOEA, SRIOEB, SRIOEAB](
|
||||||
|
G.MonadMap[RIOEA, RIOEB],
|
||||||
|
G.MonadChain[RIOEAB, RIOEB],
|
||||||
|
fab,
|
||||||
|
fa,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Ap[
|
||||||
|
SRIOEA ~func(S) RIOEA,
|
||||||
|
SRIOEB ~func(S) RIOEB,
|
||||||
|
SRIOEAB ~func(S) RIOEAB,
|
||||||
|
RIOEA ~func(R) IOEA,
|
||||||
|
RIOEB ~func(R) IOEB,
|
||||||
|
RIOEAB ~func(R) IOEAB,
|
||||||
|
IOEA ~func() ET.Either[E, P.Pair[A, S]],
|
||||||
|
IOEB ~func() ET.Either[E, P.Pair[B, S]],
|
||||||
|
IOEAB ~func() ET.Either[E, P.Pair[func(A) B, S]],
|
||||||
|
S, R, E, A, B any,
|
||||||
|
](fa SRIOEA) func(SRIOEAB) SRIOEB {
|
||||||
|
return ST.Ap[SRIOEA, SRIOEB, SRIOEAB](
|
||||||
|
G.Map[RIOEA, RIOEB],
|
||||||
|
G.Chain[RIOEAB, RIOEB],
|
||||||
|
fa,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
44
statereaderioeither/monad.go
Normal file
44
statereaderioeither/monad.go
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
// Copyright (c) 2024 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 statereaderioeither
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/IBM/fp-go/internal/applicative"
|
||||||
|
"github.com/IBM/fp-go/internal/functor"
|
||||||
|
"github.com/IBM/fp-go/internal/monad"
|
||||||
|
"github.com/IBM/fp-go/internal/pointed"
|
||||||
|
G "github.com/IBM/fp-go/statereaderioeither/generic"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Pointed returns the pointed operations for [StateReaderIOEither]
|
||||||
|
func Pointed[S, R, E, A any]() pointed.Pointed[A, StateReaderIOEither[S, R, E, A]] {
|
||||||
|
return G.Pointed[StateReaderIOEither[S, R, E, A]]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Functor returns the functor operations for [StateReaderIOEither]
|
||||||
|
func Functor[S, R, E, A, B any]() functor.Functor[A, B, StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]] {
|
||||||
|
return G.Functor[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Applicative returns the applicative operations for [StateReaderIOEither]
|
||||||
|
func Applicative[S, R, E, A, B any]() applicative.Applicative[A, B, StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]] {
|
||||||
|
return G.Applicative[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]]()
|
||||||
|
}
|
||||||
|
|
||||||
|
// Monad returns the monadic operations for [StateReaderIOEither]
|
||||||
|
func Monad[S, R, E, A, B any]() monad.Monad[A, B, StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]] {
|
||||||
|
return G.Monad[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]]()
|
||||||
|
}
|
@ -42,3 +42,11 @@ func MonadChain[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A], f func(A)
|
|||||||
func Chain[S, R, E, A, B any](f func(A) StateReaderIOEither[S, R, E, B]) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] {
|
func Chain[S, R, E, A, B any](f func(A) StateReaderIOEither[S, R, E, B]) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] {
|
||||||
return G.Chain[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](f)
|
return G.Chain[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](f)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func MonadAp[S, R, E, A, B any](fab StateReaderIOEither[S, R, E, func(A) B], fa StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] {
|
||||||
|
return G.MonadAp[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]](fab, fa)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Ap[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A]) func(StateReaderIOEither[S, R, E, func(A) B]) StateReaderIOEither[S, R, E, B] {
|
||||||
|
return G.Ap[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B], StateReaderIOEither[S, R, E, func(A) B]](fa)
|
||||||
|
}
|
||||||
|
87
statereaderioeither/testing/laws.go
Normal file
87
statereaderioeither/testing/laws.go
Normal file
@ -0,0 +1,87 @@
|
|||||||
|
// Copyright (c) 2024 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 testing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
ET "github.com/IBM/fp-go/either"
|
||||||
|
EQ "github.com/IBM/fp-go/eq"
|
||||||
|
L "github.com/IBM/fp-go/internal/monad/testing"
|
||||||
|
P "github.com/IBM/fp-go/pair"
|
||||||
|
RIOE "github.com/IBM/fp-go/readerioeither"
|
||||||
|
ST "github.com/IBM/fp-go/statereaderioeither"
|
||||||
|
)
|
||||||
|
|
||||||
|
// AssertLaws asserts the apply monad laws for the `Either` monad
|
||||||
|
func AssertLaws[S, E, R, A, B, C any](t *testing.T,
|
||||||
|
eqs EQ.Eq[S],
|
||||||
|
eqe EQ.Eq[E],
|
||||||
|
eqa EQ.Eq[A],
|
||||||
|
eqb EQ.Eq[B],
|
||||||
|
eqc EQ.Eq[C],
|
||||||
|
|
||||||
|
ab func(A) B,
|
||||||
|
bc func(B) C,
|
||||||
|
|
||||||
|
s S,
|
||||||
|
r R,
|
||||||
|
) func(a A) bool {
|
||||||
|
|
||||||
|
eqra := RIOE.Eq[R](ET.Eq(eqe, P.Eq(eqa, eqs)))(r)
|
||||||
|
eqrb := RIOE.Eq[R](ET.Eq(eqe, P.Eq(eqb, eqs)))(r)
|
||||||
|
eqrc := RIOE.Eq[R](ET.Eq(eqe, P.Eq(eqc, eqs)))(r)
|
||||||
|
|
||||||
|
fofc := ST.Pointed[S, R, E, C]()
|
||||||
|
fofaa := ST.Pointed[S, R, E, func(A) A]()
|
||||||
|
fofbc := ST.Pointed[S, R, E, func(B) C]()
|
||||||
|
fofabb := ST.Pointed[S, R, E, func(func(A) B) B]()
|
||||||
|
|
||||||
|
fmap := ST.Functor[S, R, E, func(B) C, func(func(A) B) func(A) C]()
|
||||||
|
|
||||||
|
fapabb := ST.Applicative[S, R, E, func(A) B, B]()
|
||||||
|
fapabac := ST.Applicative[S, R, E, func(A) B, func(A) C]()
|
||||||
|
|
||||||
|
maa := ST.Monad[S, R, E, A, A]()
|
||||||
|
mab := ST.Monad[S, R, E, A, B]()
|
||||||
|
mac := ST.Monad[S, R, E, A, C]()
|
||||||
|
mbc := ST.Monad[S, R, E, B, C]()
|
||||||
|
|
||||||
|
return L.MonadAssertLaws(t,
|
||||||
|
ST.Eq(eqra)(s),
|
||||||
|
ST.Eq(eqrb)(s),
|
||||||
|
ST.Eq(eqrc)(s),
|
||||||
|
|
||||||
|
fofc,
|
||||||
|
fofaa,
|
||||||
|
fofbc,
|
||||||
|
fofabb,
|
||||||
|
|
||||||
|
fmap,
|
||||||
|
|
||||||
|
fapabb,
|
||||||
|
fapabac,
|
||||||
|
|
||||||
|
maa,
|
||||||
|
mab,
|
||||||
|
mac,
|
||||||
|
mbc,
|
||||||
|
|
||||||
|
ab,
|
||||||
|
bc,
|
||||||
|
)
|
||||||
|
|
||||||
|
}
|
51
statereaderioeither/testing/laws_test.go
Normal file
51
statereaderioeither/testing/laws_test.go
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
// 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 testing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"context"
|
||||||
|
"fmt"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
A "github.com/IBM/fp-go/array"
|
||||||
|
EQ "github.com/IBM/fp-go/eq"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestMonadLaws(t *testing.T) {
|
||||||
|
// some comparison
|
||||||
|
eqs := A.Eq[string](EQ.FromStrictEquals[string]())
|
||||||
|
eqe := EQ.FromStrictEquals[error]()
|
||||||
|
eqa := EQ.FromStrictEquals[bool]()
|
||||||
|
eqb := EQ.FromStrictEquals[int]()
|
||||||
|
eqc := EQ.FromStrictEquals[string]()
|
||||||
|
|
||||||
|
ab := func(a bool) int {
|
||||||
|
if a {
|
||||||
|
return 1
|
||||||
|
}
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
|
||||||
|
bc := func(b int) string {
|
||||||
|
return fmt.Sprintf("value %d", b)
|
||||||
|
}
|
||||||
|
|
||||||
|
laws := AssertLaws(t, eqs, eqe, eqa, eqb, eqc, ab, bc, A.Empty[string](), context.Background())
|
||||||
|
|
||||||
|
assert.True(t, laws(true))
|
||||||
|
assert.True(t, laws(false))
|
||||||
|
}
|
Reference in New Issue
Block a user