Documentation ¶
Overview ¶
Package io implements IO tools similar to what is available in Scala cats library (and Haskell IO).
Index ¶
- Variables
- func IOFuncToGoResult[A any, B any](f func(a A) IO[B]) func(A) GoResult[B]
- func LiftFunc[A any, B any](f func(A) B) func(A) IO[B]
- func Memoize[A comparable, B any](f func(a A) IO[B]) func(A) IO[B]
- func ObtainResult[A any](c Continuation[A]) (res A, err error)
- func ParallelInExecutionContext[A any](ec ExecutionContext) func(ios []IO[A]) IO[[]A]
- func RetryStrategyMaxCount(substring string) func(s int, err error) IO[option.Option[int]]
- func StartInExecutionContext[A any](ec ExecutionContext) func(io IO[A]) IO[Fiber[A]]
- func ToChannel[A any](ch chan<- A) func(A) IO[fun.Unit]
- func ToChannelAndClose[A any](ch chan<- A) func(A) IO[fun.Unit]
- func UnsafeRunSync[A any](io IO[A]) (res A, err error)
- func WithTimeout[A any](d time.Duration) func(ioa IO[A]) IO[A]
- type Callback
- type Consumer
- type Continuation
- type ExecutionContext
- type Fiber
- type GoResult
- type IO
- func AfterTimeout[A any](duration time.Duration, ioa IO[A]) IO[A]
- func AndThen[A any, B any](ioa IO[A], iob IO[B]) IO[B]
- func Async[A any](k func(Callback[A])) IO[A]
- func CloseChannel[A any](ch chan<- A) IO[fun.Unit]
- func ConcurrentlyFirst[A any](ios []IO[A]) IO[A]
- func Delay[A any](f func() IO[A]) IO[A]
- func Eval[A any](f func() (A, error)) IO[A]
- func Fail[A any](err error) IO[A]
- func Finally[A any](io IO[A], finalizer IO[fun.Unit]) IO[A]
- func FireAndForget[A any](ioa IO[A]) IO[fun.Unit]
- func FlatMap[A any, B any](ioA IO[A], f func(a A) IO[B]) IO[B]
- func FlatMapErr[A any, B any](ioA IO[A], f func(a A) (B, error)) IO[B]
- func Fold[A any, B any](ioA IO[A], f func(a A) IO[B], recover func(error) IO[B]) IO[B]
- func FoldErr[A any, B any](ioA IO[A], f func(a A) (B, error), recover func(error) (B, error)) IO[B]
- func FoldToGoResult[A any](io IO[A]) IO[GoResult[A]]
- func ForEach[A any](io IO[A], cb func(a A)) IO[fun.Unit]
- func FromChannel[A any](ch chan A) IO[A]
- func FromConstantGoResult[A any](gr GoResult[A]) IO[A]
- func FromPureEffect(f func()) IO[fun.Unit]
- func FromUnit(f func() error) IO[fun.Unit]
- func JoinWithTimeout[A any](f Fiber[A], d time.Duration) IO[A]
- func Lift[A any](a A) IO[A]
- func LiftPair[A any](a A, err error) IO[A]
- func MakeUnbufferedChannel[A any]() IO[chan A]
- func Map[A any, B any](ioA IO[A], f func(a A) B) IO[B]
- func MapConst[A any, B any](ioA IO[A], b B) IO[B]
- func MapErr[A any, B any](ioA IO[A], f func(a A) (B, error)) IO[B]
- func MapSlice[A any, B any](ioas IO[[]A], f func(a A) B) IO[[]B]
- func MeasureDuration[A any](ioa IO[A]) IO[fun.Pair[A, time.Duration]]
- func Never[A any]() IO[A]
- func Notify[A any](d time.Duration, value A, cb Callback[A]) IO[fun.Unit]
- func NotifyToChannel[A any](d time.Duration, value A, ch chan A) IO[fun.Unit]
- func OnError[A any](io IO[A], onError func(err error) IO[fun.Unit]) IO[A]
- func PairParallel[A any, B any](ioa IO[A], iob IO[B]) IO[fun.Pair[A, B]]
- func PairSequentially[A any, B any](ioa IO[A], iob IO[B]) IO[fun.Pair[A, B]]
- func Parallel[A any](ios ...IO[A]) IO[[]A]
- func Pure[A any](f func() A) IO[A]
- func Recover[A any](io IO[A], recover func(err error) IO[A]) IO[A]
- func Retry[A any, S any](ioa IO[A], strategy func(s S, err error) IO[option.Option[S]], zero S) IO[A]
- func RetryS[A any, S any](ioa IO[A], strategy func(s S, err error) IO[option.Option[S]], zero S) IO[fun.Pair[A, S]]
- func RunAlso[A any](ioa IO[A], other IOUnit) IO[A]
- func Sequence[A any](ioas []IO[A]) (res IO[[]A])
- func Sleep(d time.Duration) IO[fun.Unit]
- func SleepA[A any](d time.Duration, value A) IO[A]
- func Start[A any](io IO[A]) IO[Fiber[A]]
- func StartInGoRoutineAndWaitForResult[A any](io IO[A]) IO[A]
- func UnfoldGoResult[A any](iogr IO[GoResult[A]]) IO[A]
- func Unptr[A any](ptra *A) IO[A]
- func Wrapf[A any](io IO[A], format string, args ...interface{}) IO[A]
- type IOUnit
- type ResultOrContinuation
- type Runnable
Constants ¶
This section is empty.
Variables ¶
var ErrorNPE = errors.New("nil pointer")
var ErrorTimeout = errors.New("timeout")
ErrorTimeout is an error that will be returned in case of timeout.
var IOUnit1 = Lift(fun.Unit1)
IOUnit1 is a IO[Unit] that will always return Unit1.
var MaxContinuationDepth = 1000000000
MaxContinuationDepth is equal to 1000000000. It's the maximum depth we run continuation before giving up.
Functions ¶
func IOFuncToGoResult ¶ added in v0.3.7
IOFuncToGoResult converts a function that returns IO to a function that will return GoResult.
func Memoize ¶ added in v0.3.7
func Memoize[A comparable, B any](f func(a A) IO[B]) func(A) IO[B]
Memoize returns a function that will remember the original function in a map. It's thread safe, however, not super performant.
func ObtainResult ¶ added in v0.2.0
func ObtainResult[A any](c Continuation[A]) (res A, err error)
ObtainResult executes continuation until final result is obtained.
func ParallelInExecutionContext ¶ added in v0.1.8
func ParallelInExecutionContext[A any](ec ExecutionContext) func(ios []IO[A]) IO[[]A]
ParallelInExecutionContext starts the given IOs in the provided `ExecutionContext` and waits for all results.
func RetryStrategyMaxCount ¶ added in v0.3.2
RetryStrategyMaxCount is a strategy that retries n times immediately.
func StartInExecutionContext ¶ added in v0.1.8
func StartInExecutionContext[A any](ec ExecutionContext) func(io IO[A]) IO[Fiber[A]]
StartInExecutionContext executes the given task in the provided ExecutionContext It'll establish a channel with callbacks, so that any number of listeners could join the returned fiber. (Simultaneously not more than MaxCallbackCount though.) When completed it'll start sending the results to the callbacks. The same value will be delivered to all listeners.
func ToChannelAndClose ¶ added in v0.0.10
ToChannelAndClose sends the value to the channel and then closes the channel.
func UnsafeRunSync ¶
UnsafeRunSync runs the given IO[A] synchronously and returns the result.
Types ¶
type Callback ¶ added in v0.0.10
Callback[A] is a function that takes A and error. A is only valid if error is nil.
type Consumer ¶ added in v0.3.1
Consumer can receive an instance of A and perform some operation on it.
type Continuation ¶ added in v0.2.0
type Continuation[A any] func() ResultOrContinuation[A]
Continuation represents some multistep computation. It is being used to avoid stack overflow. It's a universal way to do "trampolining".
type ExecutionContext ¶ added in v0.1.8
type ExecutionContext interface { // Start returns an IO which will return immediately when executed. // It'll place the runnable into this execution context. Start(neverFailingTask Runnable) IOUnit // Close stops receiving new tasks. Subsequent start invocations will fail. Close() IOUnit }
ExecutionContext is a resource capable of running tasks in parallel. NB! This is not a safe resource and it is not intended to be used directly.
func BoundedExecutionContext ¶ added in v0.1.8
func BoundedExecutionContext(size int, queueLimit int) ExecutionContext
BoundedExecutionContext creates an execution context that will execute tasks concurrently. Simultaneously there could be as many as size executions. If there are more tasks than could be started immediately they will be placed in a queue. If the queue is exhausted, Start will block until some tasks are run. Recommended queue size is 0.
func UnboundedExecutionContext ¶ added in v0.1.8
func UnboundedExecutionContext() ExecutionContext
UnboundedExecutionContext runs each task in a new go routine.
type Fiber ¶ added in v0.0.10
type Fiber[A any] interface { // Join waits for results of the fiber. // When fiber completes, this IO will complete and return the result. // After this fiber is closed, all join IOs fail immediately. Join() IO[A] // Closes the fiber and stops sending callbacks. // After closing, the respective go routine may complete // This is not Cancel, it does not send any signals to the fiber. // The work will still be done. Close() IO[fun.Unit] }
Fiber[A] is a type safe representation of Go routine. One might Join() and receive the result of the go routine. After Close() subsequent joins will fail.
func FailedFiber ¶ added in v0.2.5
FailedFiber creates a fiber that will fail on Join or Close with the given error.
type GoResult ¶ added in v0.0.3
GoResult[A] is a data structure that represents the Go-style result of a function that could fail.
func JoinFiberAsGoResult ¶ added in v0.2.7
JoinFiberAsGoResult joins the fiber synchronously and returns GoResult.
func NewFailedGoResult ¶ added in v0.2.7
NewFailedGoResult constructs a GoResult with an error.
func NewGoResult ¶ added in v0.2.7
NewGoResult constructs a GoResult.
type IO ¶
type IO[A any] Continuation[A]
IO[A] represents a calculation that will yield a value of type A once executed. The calculation might as well fail. It is designed to not panic ever.
func AfterTimeout ¶ added in v0.3.5
AfterTimeout sleeps the given time and then starts the other IO.
func AndThen ¶ added in v0.0.10
AndThen runs the first IO, ignores it's result and then runs the second one.
func Async ¶ added in v0.0.10
Async[A] constructs an IO given a function that will eventually call a callback. Internally this function creates a channel and blocks on it until the function calls it.
func CloseChannel ¶ added in v0.1.0
CloseChannel is an IO that closes the given channel.
func ConcurrentlyFirst ¶ added in v0.0.10
ConcurrentlyFirst - runs all IOs in parallel. returns the very first result. TODO: after obtaining result - cancel the other IOs.
func Eval ¶
Eval[A] constructs an IO[A] from a simple function that might fail. If there is panic in the function, it's recovered from and represented as an error.
func Finally ¶ added in v0.1.3
Finally runs the finalizer regardless of the success of the IO. In case finalizer fails as well, the second error is printed to log.
func FireAndForget ¶ added in v0.0.10
FireAndForget runs the given IO in a go routine and ignores the result It uses Fiber underneath.
func FlatMap ¶
FlatMap converts the result of IO[A] using a function that itself returns an IO[B]. It'll fail if any of IO[A] or IO[B] fail.
func FlatMapErr ¶ added in v0.0.3
FlatMapErr converts IO[A] result using a function that might fail. It seems to be identical to MapErr.
func Fold ¶ added in v0.0.3
Fold performs different calculations based on whether IO[A] failed or succeeded.
func FoldToGoResult ¶ added in v0.0.10
FoldToGoResult converts either value or error to go result typically it should never fail.
func FromChannel ¶ added in v0.0.10
FromChannel reads a single value from the channel
func FromConstantGoResult ¶ added in v0.1.4
FromConstantGoResult converts an existing GoResult value into a fake IO. NB! This is not for normal delayed IO execution!
func FromPureEffect ¶ added in v0.1.3
FromPureEffect constructs IO from the simplest function signature.
func FromUnit ¶ added in v0.0.3
FromUnit consturcts IO[fun.Unit] from a simple function that might fail.
func JoinWithTimeout ¶ added in v0.3.5
JoinWithTimeout joins the given fiber and waits no more than the given duration.
func MakeUnbufferedChannel ¶ added in v0.1.0
MakeUnbufferedChannel allocates a new unbufered channel.
func MapConst ¶ added in v0.3.2
MapConst ignores the result and replaces it with the given constant.
func MapSlice ¶ added in v0.3.6
MapSlice converts each element of the slice inside IO[[]A] using the provided function that cannot fail.
func MeasureDuration ¶ added in v0.2.8
MeasureDuration captures the wall time that was needed to evaluate the given IO.
func Notify ¶ added in v0.0.10
Notify starts a separate thread that will call the given callback after the specified time.
func NotifyToChannel ¶ added in v0.1.0
NotifyToChannel sends message to channel after specified duration.
func PairParallel ¶ added in v0.2.8
PairParallel runs two IOs in parallel and returns both results.
func PairSequentially ¶ added in v0.2.8
PairSequentially runs two IOs sequentially and returns both results.
func Parallel ¶ added in v0.0.10
Parallel starts the given IOs in Go routines and waits for all results.
func Retry ¶ added in v0.3.2
func Retry[A any, S any](ioa IO[A], strategy func(s S, err error) IO[option.Option[S]], zero S) IO[A]
Retry performs the same operation a few times based on the retry strategy.
func RetryS ¶ added in v0.3.2
func RetryS[A any, S any](ioa IO[A], strategy func(s S, err error) IO[option.Option[S]], zero S) IO[fun.Pair[A, S]]
RetryS performs the same operation a few times based on the retry strategy. Also returns the last state of the error-handling strategy.
func RunAlso ¶ added in v0.3.0
RunAlso runs the other IO in parallel, but returns only the result of the first IO.
func Sequence ¶ added in v0.0.5
Sequence takes a slice of IOs and returns an IO that will contain a slice of results. It'll fail if any of the internal computations fail.
func Start ¶ added in v0.0.10
Start will start the IO in a separate go-routine (actually in the global unbounded execution context). It'll establish a channel with callbacks, so that any number of listeners could join the returned fiber. When completed it'll start sending the results to the callbacks. The same value will be delivered to all listeners.
func StartInGoRoutineAndWaitForResult ¶ added in v0.0.10
StartInGoRoutineAndWaitForResult - not very useful function. While it executes the IO in the go routine, the current thread is blocked.
func UnfoldGoResult ¶ added in v0.0.10
UnfoldGoResult represents GoResult back to ordinary IO.
type IOUnit ¶ added in v0.0.3
IOUnit is IO[Unit]
func SequenceUnit ¶ added in v0.0.5
SequenceUnit takes a slice of IO units and returns IO that executes all of them. It'll fail if any of the internal computations fail.
type ResultOrContinuation ¶ added in v0.2.0
type ResultOrContinuation[A any] struct { Value A Error error Continuation *Continuation[A] }
ResultOrContinuation is either a final result (value or error) or another continuation.