1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-08-26 19:38:58 +02:00

Compare commits

...

7 Commits

Author SHA1 Message Date
Carsten Leue
93a7e9964b Merge pull request #69 from IBM/cleue-add-missing-FromIO
fix: add missing FromIO to ReaderIO
2023-10-12 11:18:07 +02:00
Dr. Carsten Leue
9ef98e1ec4 fix: add missing FromIO to ReaderIO
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-10-12 11:17:31 +02:00
Carsten Leue
f1a730998d Merge pull request #68 from IBM/cleue-add-flap-to-readerio
fix: add Flap to more monads
2023-10-12 09:49:18 +02:00
Dr. Carsten Leue
f129297045 fix: add Flap to more monads
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-10-12 09:48:42 +02:00
Carsten Leue
b434f1fbf4 Merge pull request #67 from IBM/cleue-add-flap-to-reader
fix: add flap to Reader
2023-10-11 22:24:00 +02:00
Dr. Carsten Leue
756e1336dc fix: add flap to Reader
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
2023-10-11 22:23:19 +02:00
Carsten Leue
c629d18bb3 Merge pull request #66 from IBM/cleue-add-try-catch-for-single-value-return
fix: remove File.GetName and add Join for convenience
2023-10-11 21:25:17 +02:00
18 changed files with 161 additions and 0 deletions

View File

@@ -14,6 +14,8 @@ go get github.com/IBM/fp-go
Refer to the [samples](./samples/).
Find API documentation [here](https://pkg.go.dev/github.com/IBM/fp-go)
## Design Goal
This library aims to provide a set of data types and functions that make it easy and fun to write maintainable and testable code in golang. It encourages the following patterns:
@@ -192,3 +194,14 @@ this would be the completely generic method signature for all possible monads. I
This FP library addresses this by introducing the HKTs as individual types, e.g. `HKT[A]` would be represented as a new generic type `HKTA`. This loses the correlation to the type `A` but allows to implement generic algorithms, at the price of readability.
For that reason these implementations are kept in the `internal` package. These are meant to be used by the library itself or by extensions, not by end users.
## Map/Ap/Flap
The following table lists the relationship between some selected operators
| Opertator | Parameter | Monad | Result |
| -------- | ---------------- | --------------- | -------- |
| Map | `func(A) B` | `HKT[A]` | `HKT[B]` |
| Chain | `func(A) HKT[B]` | `HKT[A]` | `HKT[B]` |
| Ap | `HKT[A]` | `HKT[func(A)B]` | `HKT[B]` |
| Flap | `A` | `HKT[func(A)B]` | `HKT[B]` |

View File

@@ -56,3 +56,11 @@ func Ask() Reader[context.Context] {
func Asks[A any](r Reader[A]) Reader[A] {
return R.Asks(r)
}
func MonadFlap[B, A any](fab Reader[func(A) B], a A) Reader[B] {
return R.MonadFlap[Reader[func(A) B], Reader[B]](fab, a)
}
func Flap[B, A any](a A) func(Reader[func(A) B]) Reader[B] {
return R.Flap[Reader[func(A) B], Reader[B]](a)
}

View File

@@ -119,3 +119,11 @@ func ChainEitherK[A, B any](f func(A) ET.Either[error, B]) func(ma ReaderEither[
func ChainOptionK[A, B any](onNone func() error) func(func(A) O.Option[B]) func(ReaderEither[A]) ReaderEither[B] {
return RE.ChainOptionK[ReaderEither[A], ReaderEither[B]](onNone)
}
func MonadFlap[B, A any](fab ReaderEither[func(A) B], a A) ReaderEither[B] {
return RE.MonadFlap[ReaderEither[func(A) B], ReaderEither[B]](fab, a)
}
func Flap[B, A any](a A) func(ReaderEither[func(A) B]) ReaderEither[B] {
return RE.Flap[ReaderEither[func(A) B], ReaderEither[B]](a)
}

View File

@@ -23,6 +23,11 @@ import (
R "github.com/IBM/fp-go/readerio/generic"
)
// FromIO converts an [IO.IO] to a [ReaderIO]
func FromIO[A any](a IO.IO[A]) ReaderIO[A] {
return R.FromIO[ReaderIO[A]](a)
}
func MonadMap[A, B any](fa ReaderIO[A], f func(A) B) ReaderIO[B] {
return R.MonadMap[ReaderIO[A], ReaderIO[B]](fa, f)
}
@@ -87,3 +92,11 @@ func Memoize[A any](rdr ReaderIO[A]) ReaderIO[A] {
func Flatten[A any](mma ReaderIO[ReaderIO[A]]) ReaderIO[A] {
return R.Flatten[ReaderIO[A]](mma)
}
func MonadFlap[B, A any](fab ReaderIO[func(A) B], a A) ReaderIO[B] {
return R.MonadFlap[ReaderIO[func(A) B], ReaderIO[B]](fab, a)
}
func Flap[B, A any](a A) func(ReaderIO[func(A) B]) ReaderIO[B] {
return R.Flap[ReaderIO[func(A) B], ReaderIO[B]](a)
}

View File

@@ -664,3 +664,11 @@ func LeftReaderIO[
A any](ma GRIOE) GRIOEA {
return RIE.LeftReaderIO[GRIOEA](ma)
}
func MonadFlap[GREAB ~func(context.Context) GEAB, GREB ~func(context.Context) GEB, GEAB ~func() E.Either[error, func(A) B], GEB ~func() E.Either[error, B], B, A any](fab GREAB, a A) GREB {
return RIE.MonadFlap[GREAB, GREB](fab, a)
}
func Flap[GREAB ~func(context.Context) GEAB, GREB ~func(context.Context) GEB, GEAB ~func() E.Either[error, func(A) B], GEB ~func() E.Either[error, B], B, A any](a A) func(GREAB) GREB {
return RIE.Flap[GREAB, GREB](a)
}

View File

@@ -250,3 +250,11 @@ func RightReaderIO[A any](ma RIO.ReaderIO[A]) ReaderIOEither[A] {
func LeftReaderIO[A any](ma RIO.ReaderIO[error]) ReaderIOEither[A] {
return G.LeftReaderIO[ReaderIOEither[A]](ma)
}
func MonadFlap[B, A any](fab ReaderIOEither[func(A) B], a A) ReaderIOEither[B] {
return G.MonadFlap[ReaderIOEither[func(A) B], ReaderIOEither[B]](fab, a)
}
func Flap[B, A any](a A) func(ReaderIOEither[func(A) B]) ReaderIOEither[B] {
return G.Flap[ReaderIOEither[func(A) B], ReaderIOEither[B]](a)
}

View File

@@ -25,6 +25,7 @@ import (
"github.com/IBM/fp-go/internal/eithert"
FE "github.com/IBM/fp-go/internal/fromeither"
FI "github.com/IBM/fp-go/internal/fromio"
FC "github.com/IBM/fp-go/internal/functor"
IO "github.com/IBM/fp-go/io/generic"
O "github.com/IBM/fp-go/option"
)
@@ -304,3 +305,11 @@ func MonadAlt[LAZY ~func() GIOA, GIOA ~func() ET.Either[E, A], E, A any](first G
func Alt[LAZY ~func() GIOA, GIOA ~func() ET.Either[E, A], E, A any](second LAZY) func(GIOA) GIOA {
return F.Bind2nd(MonadAlt[LAZY], second)
}
func MonadFlap[GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B], E, B, A any](fab GEAB, a A) GEB {
return FC.MonadFlap(MonadMap[GEAB, GEB], fab, a)
}
func Flap[GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B], E, B, A any](a A) func(GEAB) GEB {
return FC.Flap(MonadMap[GEAB, GEB], a)
}

View File

@@ -231,3 +231,11 @@ func MonadAlt[E, A any](first IOEither[E, A], second L.Lazy[IOEither[E, A]]) IOE
func Alt[E, A any](second L.Lazy[IOEither[E, A]]) func(IOEither[E, A]) IOEither[E, A] {
return G.Alt(second)
}
func MonadFlap[E, B, A any](fab IOEither[E, func(A) B], a A) IOEither[E, B] {
return G.MonadFlap[IOEither[E, func(A) B], IOEither[E, B]](fab, a)
}
func Flap[E, B, A any](a A) func(IOEither[E, func(A) B]) IOEither[E, B] {
return G.Flap[IOEither[E, func(A) B], IOEither[E, B]](a)
}

View File

@@ -18,6 +18,7 @@ package generic
import (
F "github.com/IBM/fp-go/function"
I "github.com/IBM/fp-go/identity/generic"
FC "github.com/IBM/fp-go/internal/functor"
T "github.com/IBM/fp-go/tuple"
)
@@ -120,3 +121,11 @@ func Local[GA1 ~func(R1) A, GA2 ~func(R2) A, R2, R1, A any](f func(R2) R1) func(
func Read[GA ~func(E) A, E, A any](e E) func(GA) A {
return I.Ap[GA](e)
}
func MonadFlap[GAB ~func(R) func(A) B, GB ~func(R) B, R, A, B any](fab GAB, a A) GB {
return FC.MonadFlap(MonadMap[GAB, GB], fab, a)
}
func Flap[GAB ~func(R) func(A) B, GB ~func(R) B, R, A, B any](a A) func(GAB) GB {
return FC.Flap(MonadMap[GAB, GB], a)
}

View File

@@ -105,3 +105,11 @@ func Local[R2, R1, A any](f func(R2) R1) func(Reader[R1, A]) Reader[R2, A] {
func Read[E, A any](e E) func(Reader[E, A]) A {
return G.Read[Reader[E, A]](e)
}
func MonadFlap[R, A, B any](fab Reader[R, func(A) B], a A) Reader[R, B] {
return G.MonadFlap[Reader[R, func(A) B], Reader[R, B]](fab, a)
}
func Flap[R, A, B any](a A) func(Reader[R, func(A) B]) Reader[R, B] {
return G.Flap[Reader[R, func(A) B], Reader[R, B]](a)
}

View File

@@ -21,6 +21,7 @@ import (
"github.com/IBM/fp-go/internal/eithert"
FE "github.com/IBM/fp-go/internal/fromeither"
FR "github.com/IBM/fp-go/internal/fromreader"
FC "github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/readert"
O "github.com/IBM/fp-go/option"
R "github.com/IBM/fp-go/reader/generic"
@@ -155,3 +156,11 @@ func Local[GA1 ~func(R1) ET.Either[E, A], GA2 ~func(R2) ET.Either[E, A], R2, R1,
func Read[GA ~func(E) ET.Either[E1, A], E, E1, A any](e E) func(GA) ET.Either[E1, A] {
return R.Read[GA](e)
}
func MonadFlap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L, B], L, E, A, B any](fab GEFAB, a A) GEB {
return FC.MonadFlap(MonadMap[GEFAB, GEB], fab, a)
}
func Flap[GEFAB ~func(E) ET.Either[L, func(A) B], GEB ~func(E) ET.Either[L, B], L, E, A, B any](a A) func(GEFAB) GEB {
return FC.Flap(MonadMap[GEFAB, GEB], a)
}

View File

@@ -143,3 +143,11 @@ func Local[E, A, R2, R1 any](f func(R2) R1) func(ReaderEither[R1, E, A]) ReaderE
func Read[E1, A, E any](e E) func(ReaderEither[E, E1, A]) ET.Either[E1, A] {
return G.Read[ReaderEither[E, E1, A]](e)
}
func MonadFlap[L, E, A, B any](fab ReaderEither[L, E, func(A) B], a A) ReaderEither[L, E, B] {
return G.MonadFlap[ReaderEither[L, E, func(A) B], ReaderEither[L, E, B]](fab, a)
}
func Flap[L, E, B, A any](a A) func(ReaderEither[L, E, func(A) B]) ReaderEither[L, E, B] {
return G.Flap[ReaderEither[L, E, func(A) B], ReaderEither[L, E, B]](a)
}

View File

@@ -21,6 +21,7 @@ import (
F "github.com/IBM/fp-go/function"
FIO "github.com/IBM/fp-go/internal/fromio"
FR "github.com/IBM/fp-go/internal/fromreader"
FC "github.com/IBM/fp-go/internal/functor"
"github.com/IBM/fp-go/internal/readert"
IO "github.com/IBM/fp-go/io/generic"
R "github.com/IBM/fp-go/reader/generic"
@@ -155,3 +156,11 @@ func Memoize[GEA ~func(E) GA, GA ~func() A, E, A any](rdr GEA) GEA {
func Flatten[GEA ~func(R) GIOA, GGEA ~func(R) GIOEA, GIOA ~func() A, GIOEA ~func() GEA, R, A any](mma GGEA) GEA {
return MonadChain(mma, F.Identity[GEA])
}
func MonadFlap[GEFAB ~func(E) GIOFAB, GEB ~func(E) GIOB, GIOFAB ~func() func(A) B, GIOB ~func() B, E, A, B any](fab GEFAB, a A) GEB {
return FC.MonadFlap(MonadMap[GEFAB, GEB], fab, a)
}
func Flap[GEFAB ~func(E) GIOFAB, GEB ~func(E) GIOB, GIOFAB ~func() func(A) B, GIOB ~func() B, E, A, B any](a A) func(GEFAB) GEB {
return FC.Flap(MonadMap[GEFAB, GEB], a)
}

View File

@@ -23,6 +23,7 @@ import (
type ReaderIO[E, A any] R.Reader[E, IO.IO[A]]
// FromIO converts an [IO.IO] to a [ReaderIO]
func FromIO[E, A any](t IO.IO[A]) ReaderIO[E, A] {
return G.FromIO[ReaderIO[E, A]](t)
}
@@ -86,3 +87,11 @@ func Memoize[E, A any](rdr ReaderIO[E, A]) ReaderIO[E, A] {
func Flatten[E, A any](mma ReaderIO[E, ReaderIO[E, A]]) ReaderIO[E, A] {
return G.Flatten[ReaderIO[E, A], ReaderIO[E, ReaderIO[E, A]]](mma)
}
func MonadFlap[E, A, B any](fab ReaderIO[E, func(A) B], a A) ReaderIO[E, B] {
return G.MonadFlap[ReaderIO[E, func(A) B], ReaderIO[E, B]](fab, a)
}
func Flap[E, A, B any](a A) func(ReaderIO[E, func(A) B]) ReaderIO[E, B] {
return G.Flap[ReaderIO[E, func(A) B], ReaderIO[E, B]](a)
}

View File

@@ -24,6 +24,7 @@ import (
FIO "github.com/IBM/fp-go/internal/fromio"
FIOE "github.com/IBM/fp-go/internal/fromioeither"
FR "github.com/IBM/fp-go/internal/fromreader"
FC "github.com/IBM/fp-go/internal/functor"
IOE "github.com/IBM/fp-go/ioeither/generic"
O "github.com/IBM/fp-go/option"
RD "github.com/IBM/fp-go/reader/generic"
@@ -431,3 +432,11 @@ func Memoize[
GEA ~func(R) GIOA, GIOA ~func() ET.Either[E, A], R, E, A any](rdr GEA) GEA {
return G.Memoize[GEA](rdr)
}
func MonadFlap[GREAB ~func(R) GEAB, GREB ~func(R) GEB, GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B], R, E, B, A any](fab GREAB, a A) GREB {
return FC.MonadFlap(MonadMap[GREAB, GREB], fab, a)
}
func Flap[GREAB ~func(R) GEAB, GREB ~func(R) GEB, GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B], R, E, B, A any](a A) func(GREAB) GREB {
return FC.Flap(MonadMap[GREAB, GREB], a)
}

View File

@@ -272,3 +272,11 @@ func Memoize[
R, E, A any](rdr ReaderIOEither[R, E, A]) ReaderIOEither[R, E, A] {
return G.Memoize[ReaderIOEither[R, E, A]](rdr)
}
func MonadFlap[R, E, B, A any](fab ReaderIOEither[R, E, func(A) B], a A) ReaderIOEither[R, E, B] {
return G.MonadFlap[ReaderIOEither[R, E, func(A) B], ReaderIOEither[R, E, B]](fab, a)
}
func Flap[R, E, B, A any](a A) func(ReaderIOEither[R, E, func(A) B]) ReaderIOEither[R, E, B] {
return G.Flap[ReaderIOEither[R, E, func(A) B], ReaderIOEither[R, E, B]](a)
}

View File

@@ -19,6 +19,7 @@ import (
"sort"
F "github.com/IBM/fp-go/function"
FC "github.com/IBM/fp-go/internal/functor"
G "github.com/IBM/fp-go/internal/record"
Mg "github.com/IBM/fp-go/magma"
Mo "github.com/IBM/fp-go/monoid"
@@ -514,3 +515,11 @@ func FoldMapOrdWithIndex[AS ~map[K]A, K comparable, A, B any](o ord.Ord[K]) func
}
}
}
func MonadFlap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](fab GFAB, a A) GB {
return FC.MonadFlap(MonadMap[GFAB, GB], fab, a)
}
func Flap[GFAB ~map[K]func(A) B, GB ~map[K]B, K comparable, A, B any](a A) func(GFAB) GB {
return FC.Flap(MonadMap[GFAB, GB], a)
}

View File

@@ -271,3 +271,11 @@ func KeysOrd[V any, K comparable](o ord.Ord[K]) func(r map[K]V) []K {
func ValuesOrd[V any, K comparable](o ord.Ord[K]) func(r map[K]V) []V {
return G.ValuesOrd[map[K]V, []V, K, V](o)
}
func MonadFlap[B any, K comparable, A any](fab map[K]func(A) B, a A) map[K]B {
return G.MonadFlap[map[K]func(A) B, map[K]B](fab, a)
}
func Flap[B any, K comparable, A any](a A) func(map[K]func(A) B) map[K]B {
return G.Flap[map[K]func(A) B, map[K]B](a)
}