mirror of
https://github.com/IBM/fp-go.git
synced 2025-11-25 22:21:49 +02:00
fix: add support for context sensitive readers
Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
33
readerioeither/generic/resource.go
Normal file
33
readerioeither/generic/resource.go
Normal file
@@ -0,0 +1,33 @@
|
||||
package generic
|
||||
|
||||
import (
|
||||
ET "github.com/ibm/fp-go/either"
|
||||
IOE "github.com/ibm/fp-go/ioeither/generic"
|
||||
)
|
||||
|
||||
// WithResource constructs a function that creates a resource, then operates on it and then releases the resource
|
||||
func WithResource[
|
||||
GEA ~func(L) TEA,
|
||||
GER ~func(L) TER,
|
||||
GEANY ~func(L) TEANY,
|
||||
|
||||
TEA ~func() ET.Either[E, A],
|
||||
TER ~func() ET.Either[E, R],
|
||||
TEANY ~func() ET.Either[E, ANY],
|
||||
|
||||
L, E, R, A, ANY any](onCreate GER, onRelease func(R) GEANY) func(func(R) GEA) GEA {
|
||||
|
||||
return func(f func(R) GEA) GEA {
|
||||
return func(l L) TEA {
|
||||
// dispatch to the generic implementation
|
||||
return IOE.WithResource[TEA](
|
||||
onCreate(l),
|
||||
func(r R) TEANY {
|
||||
return onRelease(r)(l)
|
||||
},
|
||||
)(func(r R) TEA {
|
||||
return f(r)(l)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user