diff --git a/internal/statet/state.go b/internal/statet/state.go new file mode 100644 index 0000000..7393469 --- /dev/null +++ b/internal/statet/state.go @@ -0,0 +1,123 @@ +// 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 state + +import ( + F "github.com/IBM/fp-go/function" + P "github.com/IBM/fp-go/pair" +) + +func Of[ + HKTSA ~func(S) HKTA, + HKTA, + S, A any, +]( + fof func(P.Pair[A, S]) HKTA, + + a A) HKTSA { + + return F.Flow2( + F.Bind1st(P.MakePair[A, S], a), + fof, + ) +} + +func MonadMap[ + HKTSA ~func(S) HKTA, + HKTSB ~func(S) HKTB, + HKTA, + HKTB, + S, A, B any, +]( + fmap func(HKTA, func(P.Pair[A, S]) P.Pair[B, S]) HKTB, + + fa HKTSA, + f func(A) B, +) HKTSB { + + return F.Flow2( + fa, + F.Bind2nd(fmap, func(a P.Pair[A, S]) P.Pair[B, S] { + return P.MakePair(f(P.Head(a)), P.Tail(a)) + }), + ) +} + +func Map[ + HKTSA ~func(S) HKTA, + HKTSB ~func(S) HKTB, + HKTA, + HKTB, + S, A, B any, +]( + fmap func(func(P.Pair[A, S]) P.Pair[B, S]) func(HKTA) HKTB, + + f func(A) B, +) func(HKTSA) HKTSB { + mp := fmap(func(a P.Pair[A, S]) P.Pair[B, S] { + return P.MakePair(f(P.Head(a)), P.Tail(a)) + }) + + return func(fa HKTSA) HKTSB { + return F.Flow2( + fa, + mp, + ) + } +} + +func MonadChain[ + HKTSA ~func(S) HKTA, + HKTSB ~func(S) HKTB, + HKTA, + HKTB, + S, A any, +]( + fchain func(HKTA, func(P.Pair[A, S]) HKTB) HKTB, + + fa HKTSA, + f func(A) HKTSB, +) HKTSB { + return F.Flow2( + fa, + F.Bind2nd(fchain, func(a P.Pair[A, S]) HKTB { + return f(P.Head(a))(P.Tail(a)) + }), + ) +} + +func Chain[ + HKTSA ~func(S) HKTA, + HKTSB ~func(S) HKTB, + HKTA, + HKTB, + S, A any, +]( + fchain func(func(P.Pair[A, S]) HKTB) func(HKTA) HKTB, + + f func(A) HKTSB, +) func(HKTSA) HKTSB { + mp := fchain(func(a P.Pair[A, S]) HKTB { + return f(P.Head(a))(P.Tail(a)) + }) + + return func(fa HKTSA) HKTSB { + return F.Flow2( + fa, + mp, + ) + } +} diff --git a/statereaderioeither/generic/state.go b/statereaderioeither/generic/state.go new file mode 100644 index 0000000..011c1e8 --- /dev/null +++ b/statereaderioeither/generic/state.go @@ -0,0 +1,106 @@ +// 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" + ST "github.com/IBM/fp-go/internal/statet" + P "github.com/IBM/fp-go/pair" + G "github.com/IBM/fp-go/readerioeither/generic" +) + +func Right[ + SRIOEA ~func(S) RIOEA, + RIOEA ~func(R) IOEA, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + S, R, E, A any, +](a A) SRIOEA { + return ST.Of[SRIOEA]( + G.Of[RIOEA], + a, + ) +} + +func Of[ + SRIOEA ~func(S) RIOEA, + RIOEA ~func(R) IOEA, + IOEA ~func() ET.Either[E, P.Pair[A, S]], + S, R, E, A any, +](a A) SRIOEA { + return Right[SRIOEA](a) +} + +func MonadMap[ + 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, +](fa SRIOEA, f func(A) B) SRIOEB { + return ST.MonadMap[SRIOEA, SRIOEB]( + G.MonadMap[RIOEA, RIOEB], + fa, + f, + ) +} + +func Map[ + 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, +](f func(A) B) func(SRIOEA) SRIOEB { + return ST.Map[SRIOEA, SRIOEB]( + G.Map[RIOEA, RIOEB], + f, + ) +} + +func MonadChain[ + 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, +](fa SRIOEA, f func(A) SRIOEB) SRIOEB { + return ST.MonadChain[SRIOEA, SRIOEB]( + G.MonadChain[RIOEA, RIOEB], + fa, + f, + ) +} + +func Chain[ + 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, +](f func(A) SRIOEB) func(SRIOEA) SRIOEB { + return ST.Chain[SRIOEA, SRIOEB]( + G.Chain[RIOEA, RIOEB], + f, + ) +} diff --git a/statereaderioeither/state.go b/statereaderioeither/state.go new file mode 100644 index 0000000..8b56097 --- /dev/null +++ b/statereaderioeither/state.go @@ -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 ( + G "github.com/IBM/fp-go/statereaderioeither/generic" +) + +func Right[S, R, E, A any](a A) StateReaderIOEither[S, R, E, A] { + return G.Right[StateReaderIOEither[S, R, E, A]](a) +} + +func Of[S, R, E, A any](a A) StateReaderIOEither[S, R, E, A] { + return G.Of[StateReaderIOEither[S, R, E, A]](a) +} + +func MonadMap[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A], f func(A) B) StateReaderIOEither[S, R, E, B] { + return G.MonadMap[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](fa, f) +} + +func Map[S, R, E, A, B any](f func(A) B) func(StateReaderIOEither[S, R, E, A]) StateReaderIOEither[S, R, E, B] { + return G.Map[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](f) +} + +func MonadChain[S, R, E, A, B any](fa StateReaderIOEither[S, R, E, A], f func(A) StateReaderIOEither[S, R, E, B]) StateReaderIOEither[S, R, E, B] { + return G.MonadChain[StateReaderIOEither[S, R, E, A], StateReaderIOEither[S, R, E, B]](fa, f) +} + +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) +} diff --git a/statereaderioeither/type.go b/statereaderioeither/type.go new file mode 100644 index 0000000..c9f02c1 --- /dev/null +++ b/statereaderioeither/type.go @@ -0,0 +1,24 @@ +// 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 ( + P "github.com/IBM/fp-go/pair" + RD "github.com/IBM/fp-go/reader" + RIOE "github.com/IBM/fp-go/readerioeither" +) + +type StateReaderIOEither[S, R, E, A any] RD.Reader[S, RIOE.ReaderIOEither[R, E, P.Pair[A, S]]]