From 65e09e0e90a79467ca23be155eaa890a13d8726e Mon Sep 17 00:00:00 2001 From: "Dr. Carsten Leue" Date: Fri, 16 Feb 2024 15:54:40 +0100 Subject: [PATCH] fix: expose Monad and Pointed for IOEither Signed-off-by: Dr. Carsten Leue --- ioeither/generic/monad.go | 56 +++++++++++++++++++++++++++++++++++++++ ioeither/monad.go | 32 ++++++++++++++++++++++ 2 files changed, 88 insertions(+) create mode 100644 ioeither/generic/monad.go create mode 100644 ioeither/monad.go diff --git a/ioeither/generic/monad.go b/ioeither/generic/monad.go new file mode 100644 index 0000000..f3989a0 --- /dev/null +++ b/ioeither/generic/monad.go @@ -0,0 +1,56 @@ +// 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/monad" + "github.com/IBM/fp-go/internal/pointed" +) + +type ioEitherPointed[E, A any, GA ~func() ET.Either[E, A]] struct{} + +type ioEitherMonad[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GAB ~func() ET.Either[E, func(A) B]] struct{} + +func (o *ioEitherPointed[E, A, GA]) Of(a A) GA { + return Of[GA, E, A](a) +} + +func (o *ioEitherMonad[E, A, B, GA, GB, GAB]) Of(a A) GA { + return Of[GA, E, A](a) +} + +func (o *ioEitherMonad[E, A, B, GA, GB, GAB]) Map(f func(A) B) func(GA) GB { + return Map[GA, GB, E, A, B](f) +} + +func (o *ioEitherMonad[E, A, B, GA, GB, GAB]) Chain(f func(A) GB) func(GA) GB { + return Chain[GA, GB, E, A, B](f) +} + +func (o *ioEitherMonad[E, A, B, GA, GB, GAB]) Ap(fa GA) func(GAB) GB { + return Ap[GB, GAB, GA, E, A, B](fa) +} + +// Pointed implements the pointed operations for [IOEither] +func Pointed[E, A any, GA ~func() ET.Either[E, A]]() pointed.Pointed[A, GA] { + return &ioEitherPointed[E, A, GA]{} +} + +// Monad implements the monadic operations for [IOEither] +func Monad[E, A, B any, GA ~func() ET.Either[E, A], GB ~func() ET.Either[E, B], GAB ~func() ET.Either[E, func(A) B]]() monad.Monad[A, B, GA, GB, GAB] { + return &ioEitherMonad[E, A, B, GA, GB, GAB]{} +} diff --git a/ioeither/monad.go b/ioeither/monad.go new file mode 100644 index 0000000..2eeaa3b --- /dev/null +++ b/ioeither/monad.go @@ -0,0 +1,32 @@ +// 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 ioeither + +import ( + "github.com/IBM/fp-go/internal/monad" + "github.com/IBM/fp-go/internal/pointed" + G "github.com/IBM/fp-go/ioeither/generic" +) + +// Pointed returns the pointed operations for [IOEither] +func Pointed[E, A any]() pointed.Pointed[A, IOEither[E, A]] { + return G.Pointed[E, A, IOEither[E, A]]() +} + +// Monad returns the monadic operations for [IOEither] +func Monad[E, A, B any]() monad.Monad[A, B, IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]] { + return G.Monad[E, A, B, IOEither[E, A], IOEither[E, B], IOEither[E, func(A) B]]() +}