mirror of
https://github.com/IBM/fp-go.git
synced 2025-08-24 19:29:11 +02:00
Compare commits
3 Commits
cleue-fix-
...
v1.0.60
Author | SHA1 | Date | |
---|---|---|---|
|
ffd9418cac | ||
|
acfcea59f4 | ||
|
b4bf511f03 |
@@ -117,7 +117,7 @@ var (
|
||||
mp,
|
||||
R.Map[int](F.Flow3(
|
||||
getAt(res),
|
||||
IOO.FromIOEither[error, any],
|
||||
IOE.ToIOOption[error, any],
|
||||
F.ToAny[IOO.IOOption[any]],
|
||||
)),
|
||||
IOE.Of[error, paramValue],
|
||||
|
@@ -32,6 +32,17 @@ func MonadAlt[LAZY ~func() HKTFA, E, A, HKTFA any](
|
||||
return fchain(first, ET.Fold(F.Ignore1of1[E](second), F.Flow2(ET.Of[E, A], fof)))
|
||||
}
|
||||
|
||||
func Alt[LAZY ~func() HKTFA, E, A, HKTFA any](
|
||||
fof func(ET.Either[E, A]) HKTFA,
|
||||
fchain func(HKTFA, func(ET.Either[E, A]) HKTFA) HKTFA,
|
||||
|
||||
second LAZY) func(HKTFA) HKTFA {
|
||||
|
||||
return func(fa HKTFA) HKTFA {
|
||||
return MonadAlt(fof, fchain, fa, second)
|
||||
}
|
||||
}
|
||||
|
||||
// HKTFA = HKT<F, Either<E, A>>
|
||||
// HKTFB = HKT<F, Either<E, B>>
|
||||
func MonadMap[E, A, B, HKTFA, HKTFB any](fmap func(HKTFA, func(ET.Either[E, A]) ET.Either[E, B]) HKTFB, fa HKTFA, f func(A) B) HKTFB {
|
||||
|
@@ -78,3 +78,24 @@ func MonadChainOptionK[A, B, HKTA, HKTB any](
|
||||
) HKTB {
|
||||
return MonadChain(fchain, fof, ma, FromOptionK(fof, f))
|
||||
}
|
||||
|
||||
func MonadAlt[LAZY ~func() HKTFA, A, HKTFA any](
|
||||
fof func(O.Option[A]) HKTFA,
|
||||
fchain func(HKTFA, func(O.Option[A]) HKTFA) HKTFA,
|
||||
|
||||
first HKTFA,
|
||||
second LAZY) HKTFA {
|
||||
|
||||
return fchain(first, O.Fold(second, F.Flow2(O.Of[A], fof)))
|
||||
}
|
||||
|
||||
func Alt[LAZY ~func() HKTFA, A, HKTFA any](
|
||||
fof func(O.Option[A]) HKTFA,
|
||||
fchain func(HKTFA, func(O.Option[A]) HKTFA) HKTFA,
|
||||
|
||||
second LAZY) func(HKTFA) HKTFA {
|
||||
|
||||
return func(fa HKTFA) HKTFA {
|
||||
return MonadAlt(fof, fchain, fa, second)
|
||||
}
|
||||
}
|
||||
|
@@ -275,6 +275,27 @@ func ChainFirstIOK[GA ~func() ET.Either[E, A], GIOB ~func() B, E, A, B any](f fu
|
||||
)
|
||||
}
|
||||
|
||||
// MonadChainFirstEitherK runs the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirstEitherK[GA ~func() ET.Either[E, A], E, A, B any](first GA, f func(A) ET.Either[E, B]) GA {
|
||||
return FE.MonadChainFirstEitherK(
|
||||
MonadChain[GA, GA, E, A, A],
|
||||
MonadMap[func() ET.Either[E, B], GA, E, B, A],
|
||||
FromEither[func() ET.Either[E, B], E, B],
|
||||
first,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// ChainFirstEitherK runs the monad returned by the function but returns the result of the original monad
|
||||
func ChainFirstEitherK[GA ~func() ET.Either[E, A], E, A, B any](f func(A) ET.Either[E, B]) func(GA) GA {
|
||||
return FE.ChainFirstEitherK(
|
||||
MonadChain[GA, GA, E, A, A],
|
||||
MonadMap[func() ET.Either[E, B], GA, E, B, A],
|
||||
FromEither[func() ET.Either[E, B], E, B],
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// Swap changes the order of type parameters
|
||||
func Swap[GEA ~func() ET.Either[E, A], GAE ~func() ET.Either[A, E], E, A any](val GEA) GAE {
|
||||
return MonadFold(val, Right[GAE], Left[GAE])
|
||||
@@ -315,3 +336,14 @@ func MonadFlap[GEAB ~func() ET.Either[E, func(A) B], GEB ~func() ET.Either[E, B]
|
||||
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)
|
||||
}
|
||||
|
||||
func ToIOOption[GA ~func() O.Option[A], GEA ~func() ET.Either[E, A], E, A any](ioe GEA) GA {
|
||||
return F.Pipe1(
|
||||
ioe,
|
||||
IO.Map[GEA, GA](ET.ToOption[E, A]),
|
||||
)
|
||||
}
|
||||
|
||||
func FromIOOption[GEA ~func() ET.Either[E, A], GA ~func() O.Option[A], E, A any](onNone func() E) func(ioo GA) GEA {
|
||||
return IO.Map[GA, GEA](ET.FromOption[A](onNone))
|
||||
}
|
||||
|
@@ -20,54 +20,94 @@ import (
|
||||
"net/http"
|
||||
|
||||
B "github.com/IBM/fp-go/bytes"
|
||||
ER "github.com/IBM/fp-go/errors"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
H "github.com/IBM/fp-go/http"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
IOEF "github.com/IBM/fp-go/ioeither/file"
|
||||
J "github.com/IBM/fp-go/json"
|
||||
T "github.com/IBM/fp-go/tuple"
|
||||
)
|
||||
|
||||
type Client interface {
|
||||
Do(req *http.Request) IOE.IOEither[error, *http.Response]
|
||||
}
|
||||
type (
|
||||
// Requester is a reader that constructs a request
|
||||
Requester = IOE.IOEither[error, *http.Request]
|
||||
|
||||
type client struct {
|
||||
delegate *http.Client
|
||||
}
|
||||
Client interface {
|
||||
Do(Requester) IOE.IOEither[error, *http.Response]
|
||||
}
|
||||
|
||||
func (client client) Do(req *http.Request) IOE.IOEither[error, *http.Response] {
|
||||
return IOE.TryCatch(func() (*http.Response, error) {
|
||||
return client.delegate.Do(req)
|
||||
}, ER.IdentityError)
|
||||
client struct {
|
||||
delegate *http.Client
|
||||
doIOE func(*http.Request) IOE.IOEither[error, *http.Response]
|
||||
}
|
||||
)
|
||||
|
||||
var (
|
||||
// MakeRequest is an eitherized version of [http.NewRequest]
|
||||
MakeRequest = IOE.Eitherize3(http.NewRequest)
|
||||
makeRequest = F.Bind13of3(MakeRequest)
|
||||
|
||||
// specialize
|
||||
MakeGetRequest = makeRequest("GET", nil)
|
||||
)
|
||||
|
||||
func (client client) Do(req Requester) IOE.IOEither[error, *http.Response] {
|
||||
return F.Pipe1(
|
||||
req,
|
||||
IOE.Chain(client.doIOE),
|
||||
)
|
||||
}
|
||||
|
||||
func MakeClient(httpClient *http.Client) Client {
|
||||
return client{delegate: httpClient}
|
||||
return client{delegate: httpClient, doIOE: IOE.Eitherize1(httpClient.Do)}
|
||||
}
|
||||
|
||||
func ReadAll(client Client) func(*http.Request) IOE.IOEither[error, []byte] {
|
||||
return func(req *http.Request) IOE.IOEither[error, []byte] {
|
||||
return IOEF.ReadAll(F.Pipe2(
|
||||
req,
|
||||
client.Do,
|
||||
IOE.Map[error](func(resp *http.Response) io.ReadCloser {
|
||||
return resp.Body
|
||||
}),
|
||||
),
|
||||
)
|
||||
}
|
||||
// ReadFullResponse sends a request, reads the response as a byte array and represents the result as a tuple
|
||||
func ReadFullResponse(client Client) func(Requester) IOE.IOEither[error, H.FullResponse] {
|
||||
return F.Flow3(
|
||||
client.Do,
|
||||
IOE.ChainEitherK(H.ValidateResponse),
|
||||
IOE.Chain(func(resp *http.Response) IOE.IOEither[error, H.FullResponse] {
|
||||
return F.Pipe1(
|
||||
F.Pipe3(
|
||||
resp,
|
||||
H.GetBody,
|
||||
IOE.Of[error, io.ReadCloser],
|
||||
IOEF.ReadAll[io.ReadCloser],
|
||||
),
|
||||
IOE.Map[error](F.Bind1st(T.MakeTuple2[*http.Response, []byte], resp)),
|
||||
)
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
||||
func ReadText(client Client) func(*http.Request) IOE.IOEither[error, string] {
|
||||
// ReadAll sends a request and reads the response as bytes
|
||||
func ReadAll(client Client) func(Requester) IOE.IOEither[error, []byte] {
|
||||
return F.Flow2(
|
||||
ReadFullResponse(client),
|
||||
IOE.Map[error](H.Body),
|
||||
)
|
||||
}
|
||||
|
||||
// ReadText sends a request, reads the response and represents the response as a text string
|
||||
func ReadText(client Client) func(Requester) IOE.IOEither[error, string] {
|
||||
return F.Flow2(
|
||||
ReadAll(client),
|
||||
IOE.Map[error](B.ToString),
|
||||
)
|
||||
}
|
||||
|
||||
func ReadJson[A any](client Client) func(*http.Request) IOE.IOEither[error, A] {
|
||||
return F.Flow2(
|
||||
ReadAll(client),
|
||||
IOE.ChainEitherK(J.Unmarshal[A]),
|
||||
// ReadJson sends a request, reads the response and parses the response as JSON
|
||||
func ReadJson[A any](client Client) func(Requester) IOE.IOEither[error, A] {
|
||||
return F.Flow3(
|
||||
ReadFullResponse(client),
|
||||
IOE.ChainFirstEitherK(F.Flow2(
|
||||
H.Response,
|
||||
H.ValidateJsonResponse,
|
||||
)),
|
||||
IOE.ChainEitherK(F.Flow2(
|
||||
H.Body,
|
||||
J.Unmarshal[A],
|
||||
)),
|
||||
)
|
||||
}
|
||||
|
@@ -23,7 +23,6 @@ import (
|
||||
|
||||
AR "github.com/IBM/fp-go/array"
|
||||
E "github.com/IBM/fp-go/either"
|
||||
HE "github.com/IBM/fp-go/either/http"
|
||||
"github.com/IBM/fp-go/errors"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
@@ -53,10 +52,9 @@ func TestRetryHttp(t *testing.T) {
|
||||
client := MakeClient(&http.Client{})
|
||||
|
||||
action := func(status R.RetryStatus) IOE.IOEither[error, *PostItem] {
|
||||
return F.Pipe2(
|
||||
HE.GetRequest(urls[status.IterNumber]),
|
||||
IOE.FromEither[error, *http.Request],
|
||||
IOE.Chain(ReadJson[*PostItem](client)),
|
||||
return F.Pipe1(
|
||||
MakeGetRequest(urls[status.IterNumber]),
|
||||
ReadJson[*PostItem](client),
|
||||
)
|
||||
}
|
||||
|
||||
|
@@ -19,6 +19,7 @@ import (
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
I "github.com/IBM/fp-go/io"
|
||||
G "github.com/IBM/fp-go/ioeither/generic"
|
||||
IOO "github.com/IBM/fp-go/iooption"
|
||||
L "github.com/IBM/fp-go/lazy"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
)
|
||||
@@ -63,6 +64,10 @@ func FromOption[A, E any](onNone func() E) func(o O.Option[A]) IOEither[E, A] {
|
||||
return G.FromOption[IOEither[E, A]](onNone)
|
||||
}
|
||||
|
||||
func FromIOOption[A, E any](onNone func() E) func(o IOO.IOOption[A]) IOEither[E, A] {
|
||||
return G.FromIOOption[IOEither[E, A], IOO.IOOption[A]](onNone)
|
||||
}
|
||||
|
||||
func ChainOptionK[A, B, E any](onNone func() E) func(func(A) O.Option[B]) func(IOEither[E, A]) IOEither[E, B] {
|
||||
return G.ChainOptionK[IOEither[E, A], IOEither[E, B]](onNone)
|
||||
}
|
||||
@@ -211,6 +216,14 @@ func ChainFirst[E, A, B any](f func(A) IOEither[E, B]) func(IOEither[E, A]) IOEi
|
||||
return G.ChainFirst[IOEither[E, A]](f)
|
||||
}
|
||||
|
||||
func MonadChainFirstEitherK[A, E, B any](ma IOEither[E, A], f func(A) ET.Either[E, B]) IOEither[E, A] {
|
||||
return G.MonadChainFirstEitherK[IOEither[E, A]](ma, f)
|
||||
}
|
||||
|
||||
func ChainFirstEitherK[A, E, B any](f func(A) ET.Either[E, B]) func(ma IOEither[E, A]) IOEither[E, A] {
|
||||
return G.ChainFirstEitherK[IOEither[E, A]](f)
|
||||
}
|
||||
|
||||
// MonadChainFirstIOK runs [IO] the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirstIOK[E, A, B any](ma IOEither[E, A], f func(A) I.IO[B]) IOEither[E, A] {
|
||||
return G.MonadChainFirstIOK(ma, f)
|
||||
@@ -258,3 +271,8 @@ func MonadFlap[E, B, A any](fab IOEither[E, func(A) B], a A) IOEither[E, B] {
|
||||
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)
|
||||
}
|
||||
|
||||
// ToIOOption converts an [IOEither] to an [IOO.IOOption]
|
||||
func ToIOOption[E, A any](ioe IOEither[E, A]) IOO.IOOption[A] {
|
||||
return G.ToIOOption[IOO.IOOption[A]](ioe)
|
||||
}
|
||||
|
@@ -64,13 +64,6 @@ func FromEither[GA ~func() O.Option[A], E, A any](e ET.Either[E, A]) GA {
|
||||
)
|
||||
}
|
||||
|
||||
func FromIOEither[GA ~func() O.Option[A], GEA ~func() ET.Either[E, A], E, A any](ioe GEA) GA {
|
||||
return F.Pipe1(
|
||||
ioe,
|
||||
IO.Map[GEA, GA](ET.ToOption[E, A]),
|
||||
)
|
||||
}
|
||||
|
||||
func MonadMap[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](fa GA, f func(A) B) GB {
|
||||
return optiont.MonadMap(IO.MonadMap[GA, GB, O.Option[A], O.Option[B]], fa, f)
|
||||
}
|
||||
@@ -194,3 +187,17 @@ func Fold[GA ~func() O.Option[A], GB ~func() B, A, B any](onNone func() GB, onSo
|
||||
func Defer[GA ~func() O.Option[A], A any](gen func() GA) GA {
|
||||
return IO.Defer[GA](gen)
|
||||
}
|
||||
|
||||
func MonadAlt[LAZY ~func() GIOA, GIOA ~func() O.Option[A], A any](first GIOA, second LAZY) GIOA {
|
||||
return optiont.MonadAlt(
|
||||
IO.Of[GIOA],
|
||||
IO.MonadChain[GIOA, GIOA],
|
||||
|
||||
first,
|
||||
second,
|
||||
)
|
||||
}
|
||||
|
||||
func Alt[LAZY ~func() GIOA, GIOA ~func() O.Option[A], A any](second LAZY) func(GIOA) GIOA {
|
||||
return F.Bind2nd(MonadAlt[LAZY], second)
|
||||
}
|
||||
|
@@ -18,8 +18,8 @@ package iooption
|
||||
import (
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
I "github.com/IBM/fp-go/io"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
G "github.com/IBM/fp-go/iooption/generic"
|
||||
L "github.com/IBM/fp-go/lazy"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
)
|
||||
|
||||
@@ -129,12 +129,17 @@ func Defer[A any](gen func() IOOption[A]) IOOption[A] {
|
||||
return G.Defer[IOOption[A]](gen)
|
||||
}
|
||||
|
||||
// FromIOEither converts an [IOEither] into an [IOOption]
|
||||
func FromIOEither[E, A any](ioe IOE.IOEither[E, A]) IOOption[A] {
|
||||
return G.FromIOEither[IOOption[A]](ioe)
|
||||
}
|
||||
|
||||
// FromEither converts an [Either] into an [IOOption]
|
||||
func FromEither[E, A any](e ET.Either[E, A]) IOOption[A] {
|
||||
return G.FromEither[IOOption[A]](e)
|
||||
}
|
||||
|
||||
// MonadAlt identifies an associative operation on a type constructor
|
||||
func MonadAlt[A any](first IOOption[A], second L.Lazy[IOOption[A]]) IOOption[A] {
|
||||
return G.MonadAlt(first, second)
|
||||
}
|
||||
|
||||
// Alt identifies an associative operation on a type constructor
|
||||
func Alt[A any](second L.Lazy[IOOption[A]]) func(IOOption[A]) IOOption[A] {
|
||||
return G.Alt(second)
|
||||
}
|
||||
|
@@ -52,7 +52,7 @@ func TestIOEitherToOption2(t *testing.T) {
|
||||
content,
|
||||
IOEF.WriteFile(filepath.Join(tmpDir, "test.txt"), os.ModePerm),
|
||||
IOE.Swap[error, []byte],
|
||||
IOO.FromIOEither[[]byte, error],
|
||||
IOE.ToIOOption[[]byte, error],
|
||||
)
|
||||
|
||||
fmt.Println(resIOO())
|
||||
|
Reference in New Issue
Block a user