mirror of
https://github.com/IBM/fp-go.git
synced 2025-12-09 23:11:40 +02:00
Compare commits
7 Commits
cleue-add-
...
v1.0.69
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
8f49c1328c | ||
|
|
2a1d5821db | ||
|
|
6abbdc5ee1 | ||
|
|
5fea9858a9 | ||
|
|
e6426c90c0 | ||
|
|
54ce59105e | ||
|
|
8bb006c741 |
4
.github/workflows/build.yml
vendored
4
.github/workflows/build.yml
vendored
@@ -33,7 +33,7 @@ jobs:
|
||||
with:
|
||||
fetch-depth: 0
|
||||
- name: Set up go ${{ matrix.go-version }}
|
||||
uses: actions/setup-go@v4
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{ matrix.go-version }}
|
||||
-
|
||||
@@ -65,7 +65,7 @@ jobs:
|
||||
node-version: ${{ env.NODE_VERSION }}
|
||||
|
||||
- name: Set up go ${{env.GO_VERSION}}
|
||||
uses: actions/setup-go@v4
|
||||
uses: actions/setup-go@v5
|
||||
with:
|
||||
go-version: ${{env.GO_VERSION}}
|
||||
|
||||
|
||||
115
di/provider.go
115
di/provider.go
@@ -73,6 +73,46 @@ func eraseProviderFactory2[T1, T2 any, R any](
|
||||
}
|
||||
}
|
||||
|
||||
func eraseProviderFactory3[T1, T2, T3 any, R any](
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
f func(T1, T2, T3) IOE.IOEither[error, R]) func(params ...any) IOE.IOEither[error, any] {
|
||||
ft := T.Tupled3(f)
|
||||
t1 := lookupAt[T1](0, d1)
|
||||
t2 := lookupAt[T2](1, d2)
|
||||
t3 := lookupAt[T3](2, d3)
|
||||
return func(params ...any) IOE.IOEither[error, any] {
|
||||
return F.Pipe3(
|
||||
E.SequenceT3(t1(params), t2(params), t3(params)),
|
||||
IOE.FromEither[error, T.Tuple3[T1, T2, T3]],
|
||||
IOE.Chain(ft),
|
||||
IOE.Map[error](F.ToAny[R]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func eraseProviderFactory4[T1, T2, T3, T4 any, R any](
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
d4 Dependency[T4],
|
||||
f func(T1, T2, T3, T4) IOE.IOEither[error, R]) func(params ...any) IOE.IOEither[error, any] {
|
||||
ft := T.Tupled4(f)
|
||||
t1 := lookupAt[T1](0, d1)
|
||||
t2 := lookupAt[T2](1, d2)
|
||||
t3 := lookupAt[T3](2, d3)
|
||||
t4 := lookupAt[T4](3, d4)
|
||||
return func(params ...any) IOE.IOEither[error, any] {
|
||||
return F.Pipe3(
|
||||
E.SequenceT4(t1(params), t2(params), t3(params), t4(params)),
|
||||
IOE.FromEither[error, T.Tuple4[T1, T2, T3, T4]],
|
||||
IOE.Chain(ft),
|
||||
IOE.Map[error](F.ToAny[R]),
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
func MakeProviderFactory0[R any](
|
||||
fct func() IOE.IOEither[error, R],
|
||||
) DIE.ProviderFactory {
|
||||
@@ -160,6 +200,81 @@ func MakeProvider2[T1, T2, R any](
|
||||
)
|
||||
}
|
||||
|
||||
func MakeProviderFactory3[T1, T2, T3, R any](
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
fct func(T1, T2, T3) IOE.IOEither[error, R],
|
||||
) DIE.ProviderFactory {
|
||||
|
||||
return DIE.MakeProviderFactory(
|
||||
A.From[DIE.Dependency](d1, d2, d3),
|
||||
eraseProviderFactory3(d1, d2, d3, fct),
|
||||
)
|
||||
}
|
||||
|
||||
// MakeTokenWithDefault3 create a unique `InjectionToken` for a specific type with an attached default provider
|
||||
func MakeTokenWithDefault3[T1, T2, T3, R any](name string,
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
fct func(T1, T2, T3) IOE.IOEither[error, R]) InjectionToken[R] {
|
||||
return MakeTokenWithDefault[R](name, MakeProviderFactory3(d1, d2, d3, fct))
|
||||
}
|
||||
|
||||
func MakeProvider3[T1, T2, T3, R any](
|
||||
token InjectionToken[R],
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
fct func(T1, T2, T3) IOE.IOEither[error, R],
|
||||
) DIE.Provider {
|
||||
|
||||
return DIE.MakeProvider(
|
||||
token,
|
||||
MakeProviderFactory3(d1, d2, d3, fct),
|
||||
)
|
||||
}
|
||||
|
||||
func MakeProviderFactory4[T1, T2, T3, T4, R any](
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
d4 Dependency[T4],
|
||||
fct func(T1, T2, T3, T4) IOE.IOEither[error, R],
|
||||
) DIE.ProviderFactory {
|
||||
|
||||
return DIE.MakeProviderFactory(
|
||||
A.From[DIE.Dependency](d1, d2, d3, d4),
|
||||
eraseProviderFactory4(d1, d2, d3, d4, fct),
|
||||
)
|
||||
}
|
||||
|
||||
// MakeTokenWithDefault4 create a unique `InjectionToken` for a specific type with an attached default provider
|
||||
func MakeTokenWithDefault4[T1, T2, T3, T4, R any](name string,
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
d4 Dependency[T4],
|
||||
fct func(T1, T2, T3, T4) IOE.IOEither[error, R]) InjectionToken[R] {
|
||||
return MakeTokenWithDefault[R](name, MakeProviderFactory4(d1, d2, d3, d4, fct))
|
||||
}
|
||||
|
||||
func MakeProvider4[T1, T2, T3, T4, R any](
|
||||
token InjectionToken[R],
|
||||
d1 Dependency[T1],
|
||||
d2 Dependency[T2],
|
||||
d3 Dependency[T3],
|
||||
d4 Dependency[T4],
|
||||
fct func(T1, T2, T3, T4) IOE.IOEither[error, R],
|
||||
) DIE.Provider {
|
||||
|
||||
return DIE.MakeProvider(
|
||||
token,
|
||||
MakeProviderFactory4(d1, d2, d3, d4, fct),
|
||||
)
|
||||
}
|
||||
|
||||
// ConstProvider simple implementation for a provider with a constant value
|
||||
func ConstProvider[R any](token InjectionToken[R], value R) DIE.Provider {
|
||||
return MakeProvider0[R](token, F.Constant(IOE.Of[error](value)))
|
||||
|
||||
2
go.mod
2
go.mod
@@ -4,7 +4,7 @@ go 1.20
|
||||
|
||||
require (
|
||||
github.com/stretchr/testify v1.8.4
|
||||
github.com/urfave/cli/v2 v2.25.7
|
||||
github.com/urfave/cli/v2 v2.26.0
|
||||
)
|
||||
|
||||
require (
|
||||
|
||||
2
go.sum
2
go.sum
@@ -10,6 +10,8 @@ github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcU
|
||||
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
|
||||
github.com/urfave/cli/v2 v2.25.7 h1:VAzn5oq403l5pHjc4OhD54+XGO9cdKVL/7lDjF+iKUs=
|
||||
github.com/urfave/cli/v2 v2.25.7/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/urfave/cli/v2 v2.26.0 h1:3f3AMg3HpThFNT4I++TKOejZO8yU55t3JnnSr4S4QEI=
|
||||
github.com/urfave/cli/v2 v2.26.0/go.mod h1:8qnjx1vcq5s2/wpsqoZFndg2CE5tNFyrTvS6SinrnYQ=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673 h1:bAn7/zixMGCfxrRTfdpNzjtPYqr8smhKouy9mxVdGPU=
|
||||
github.com/xrash/smetrics v0.0.0-20201216005158-039620a65673/go.mod h1:N3UwUGtsrSj3ccvlPHLoLsHnpR27oXr4ZE984MbSER8=
|
||||
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
|
||||
|
||||
@@ -49,6 +49,16 @@ func MonadChain[A, B, HKTFA, HKTFB any](
|
||||
return fchain(ma, O.Fold(F.Nullary2(O.None[B], fof), f))
|
||||
}
|
||||
|
||||
func Chain[A, B, HKTFA, HKTFB any](
|
||||
fchain func(HKTFA, func(O.Option[A]) HKTFB) HKTFB,
|
||||
fof func(O.Option[B]) HKTFB,
|
||||
f func(A) HKTFB) func(ma HKTFA) HKTFB {
|
||||
// dispatch to the even more generic implementation
|
||||
return func(ma HKTFA) HKTFB {
|
||||
return MonadChain(fchain, fof, ma, f)
|
||||
}
|
||||
}
|
||||
|
||||
func MonadAp[A, B, HKTFAB, HKTFGAB, HKTFA, HKTFB any](
|
||||
fap func(HKTFGAB, HKTFA) HKTFB,
|
||||
fmap func(HKTFAB, func(O.Option[func(A) B]) func(O.Option[A]) O.Option[B]) HKTFGAB,
|
||||
|
||||
30
io/generic/resource.go
Normal file
30
io/generic/resource.go
Normal file
@@ -0,0 +1,30 @@
|
||||
// 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 generic
|
||||
|
||||
import (
|
||||
F "github.com/IBM/fp-go/function"
|
||||
)
|
||||
|
||||
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
|
||||
func WithResource[
|
||||
GA ~func() A,
|
||||
GR ~func() R,
|
||||
GANY ~func() ANY,
|
||||
R, A, ANY any](onCreate GR, onRelease func(R) GANY) func(func(R) GA) GA {
|
||||
// simply map to implementation of bracket
|
||||
return F.Bind13of3(Bracket[GR, GA, GANY, R, A, ANY])(onCreate, F.Ignore2of2[A](onRelease))
|
||||
}
|
||||
27
io/resource.go
Normal file
27
io/resource.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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 io
|
||||
|
||||
import (
|
||||
G "github.com/IBM/fp-go/io/generic"
|
||||
)
|
||||
|
||||
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
|
||||
func WithResource[
|
||||
R, A, ANY any](onCreate IO[R], onRelease func(R) IO[ANY]) func(func(R) IO[A]) IO[A] {
|
||||
// just dispatch
|
||||
return G.WithResource[IO[A], IO[R], IO[ANY]](onCreate, onRelease)
|
||||
}
|
||||
32
ioeither/http/di.go
Normal file
32
ioeither/http/di.go
Normal file
@@ -0,0 +1,32 @@
|
||||
// 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 http
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
|
||||
DI "github.com/IBM/fp-go/di"
|
||||
IOE "github.com/IBM/fp-go/ioeither"
|
||||
L "github.com/IBM/fp-go/lazy"
|
||||
)
|
||||
|
||||
var (
|
||||
// InjHttpClient is the injection token for the default http client
|
||||
InjHttpClient = DI.MakeTokenWithDefault0("HTTP_CLIENT", L.Of(IOE.Of[error](http.DefaultClient)))
|
||||
|
||||
// InjClient is the injection token for the default [Client]
|
||||
InjClient = DI.MakeTokenWithDefault1("CLIENT", InjHttpClient.IOEither(), IOE.Map[error](MakeClient))
|
||||
)
|
||||
@@ -20,6 +20,7 @@ import (
|
||||
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
F "github.com/IBM/fp-go/function"
|
||||
C "github.com/IBM/fp-go/internal/chain"
|
||||
FI "github.com/IBM/fp-go/internal/fromio"
|
||||
"github.com/IBM/fp-go/internal/optiont"
|
||||
IO "github.com/IBM/fp-go/io/generic"
|
||||
@@ -76,8 +77,48 @@ func MonadChain[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](fa GA,
|
||||
return optiont.MonadChain(IO.MonadChain[GA, GB, O.Option[A], O.Option[B]], IO.MonadOf[GB, O.Option[B]], fa, f)
|
||||
}
|
||||
|
||||
// MonadChainFirst runs the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirst[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](ma GA, f func(A) GB) GA {
|
||||
return C.MonadChainFirst(
|
||||
MonadChain[GA, GA, A, A],
|
||||
MonadMap[GB, GA, B, A],
|
||||
ma,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// ChainFirst runs the monad returned by the function but returns the result of the original monad
|
||||
func ChainFirst[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](f func(A) GB) func(GA) GA {
|
||||
return C.ChainFirst(
|
||||
MonadChain[GA, GA, A, A],
|
||||
MonadMap[GB, GA, B, A],
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// MonadChainFirstIOK runs the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirstIOK[GA ~func() O.Option[A], GIOB ~func() B, A, B any](first GA, f func(A) GIOB) GA {
|
||||
return FI.MonadChainFirstIOK(
|
||||
MonadChain[GA, GA, A, A],
|
||||
MonadMap[func() O.Option[B], GA, B, A],
|
||||
FromIO[func() O.Option[B], GIOB, B],
|
||||
first,
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
// ChainFirstIOK runs the monad returned by the function but returns the result of the original monad
|
||||
func ChainFirstIOK[GA ~func() O.Option[A], GIOB ~func() B, A, B any](f func(A) GIOB) func(GA) GA {
|
||||
return FI.ChainFirstIOK(
|
||||
MonadChain[GA, GA, A, A],
|
||||
MonadMap[func() O.Option[B], GA, B, A],
|
||||
FromIO[func() O.Option[B], GIOB, B],
|
||||
f,
|
||||
)
|
||||
}
|
||||
|
||||
func Chain[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](f func(A) GB) func(GA) GB {
|
||||
return F.Bind2nd(MonadChain[GA, GB, A, B], f)
|
||||
return optiont.Chain(IO.MonadChain[GA, GB, O.Option[A], O.Option[B]], IO.MonadOf[GB, O.Option[B]], f)
|
||||
}
|
||||
|
||||
func MonadChainOptionK[GA ~func() O.Option[A], GB ~func() O.Option[B], A, B any](ma GA, f func(A) O.Option[B]) GB {
|
||||
|
||||
31
iooption/generic/resource.go
Normal file
31
iooption/generic/resource.go
Normal file
@@ -0,0 +1,31 @@
|
||||
// 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 generic
|
||||
|
||||
import (
|
||||
F "github.com/IBM/fp-go/function"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
)
|
||||
|
||||
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
|
||||
func WithResource[
|
||||
GA ~func() O.Option[A],
|
||||
GR ~func() O.Option[R],
|
||||
GANY ~func() O.Option[ANY],
|
||||
R, A, ANY any](onCreate GR, onRelease func(R) GANY) func(func(R) GA) GA {
|
||||
// simply map to implementation of bracket
|
||||
return F.Bind13of3(Bracket[GR, GA, GANY, R, A, ANY])(onCreate, F.Ignore2of2[O.Option[A]](onRelease))
|
||||
}
|
||||
@@ -18,6 +18,7 @@ package iooption
|
||||
import (
|
||||
ET "github.com/IBM/fp-go/either"
|
||||
I "github.com/IBM/fp-go/io"
|
||||
IO "github.com/IBM/fp-go/io"
|
||||
G "github.com/IBM/fp-go/iooption/generic"
|
||||
L "github.com/IBM/fp-go/lazy"
|
||||
O "github.com/IBM/fp-go/option"
|
||||
@@ -143,3 +144,23 @@ func MonadAlt[A any](first IOOption[A], second L.Lazy[IOOption[A]]) IOOption[A]
|
||||
func Alt[A any](second L.Lazy[IOOption[A]]) func(IOOption[A]) IOOption[A] {
|
||||
return G.Alt(second)
|
||||
}
|
||||
|
||||
// MonadChainFirst runs the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirst[A, B any](ma IOOption[A], f func(A) IOOption[B]) IOOption[A] {
|
||||
return G.MonadChainFirst[IOOption[A], IOOption[B]](ma, f)
|
||||
}
|
||||
|
||||
// ChainFirst runs the monad returned by the function but returns the result of the original monad
|
||||
func ChainFirst[A, B any](f func(A) IOOption[B]) func(IOOption[A]) IOOption[A] {
|
||||
return G.ChainFirst[IOOption[A], IOOption[B]](f)
|
||||
}
|
||||
|
||||
// MonadChainFirstIOK runs the monad returned by the function but returns the result of the original monad
|
||||
func MonadChainFirstIOK[A, B any](first IOOption[A], f func(A) IO.IO[B]) IOOption[A] {
|
||||
return G.MonadChainFirstIOK[IOOption[A], IO.IO[B]](first, f)
|
||||
}
|
||||
|
||||
// ChainFirstIOK runs the monad returned by the function but returns the result of the original monad
|
||||
func ChainFirstIOK[A, B any](f func(A) IO.IO[B]) func(IOOption[A]) IOOption[A] {
|
||||
return G.ChainFirstIOK[IOOption[A], IO.IO[B]](f)
|
||||
}
|
||||
|
||||
27
iooption/resource.go
Normal file
27
iooption/resource.go
Normal file
@@ -0,0 +1,27 @@
|
||||
// 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 iooption
|
||||
|
||||
import (
|
||||
G "github.com/IBM/fp-go/iooption/generic"
|
||||
)
|
||||
|
||||
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
|
||||
func WithResource[
|
||||
R, A, ANY any](onCreate IOOption[R], onRelease func(R) IOOption[ANY]) func(func(R) IOOption[A]) IOOption[A] {
|
||||
// just dispatch
|
||||
return G.WithResource[IOOption[A], IOOption[R], IOOption[ANY]](onCreate, onRelease)
|
||||
}
|
||||
Reference in New Issue
Block a user