Documentation
¶
Overview ¶
Package warp provides experimental monads.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Event ¶
An Event represents a collection of discrete occurrences of events with associated values.
Example ¶
package main
import (
"context"
"fmt"
"github.com/tetsuo/warp/event"
)
func main() {
dequeue := event.Fold(
event.Map(event.From([]int{1, 2, 3}), double),
0,
add,
)
r := make(chan int)
go dequeue(context.TODO(), r)
for i := range r {
fmt.Println(i)
}
}
func double(n int) int {
return n * 2
}
func add(b, a int) int {
return b + a
}
Output: 2 6 12
type Future ¶
A Future represents a collection of discrete occurrences of events with associated values or errors, in that, a Future is actually an Event that may fail and emits a value which is encapsulated in a Result.
type IO ¶
type IO[A any] func() A
An IO represents a computation that never fails and yields a value of type A.
type Nilable ¶
type Nilable[A any] *A
A Nilable represents an optional value which is either some value or nil.
type Result ¶
A Result represents a result of a computation which is either a value of type A, or an error.
Example ¶
package main
import (
"context"
"errors"
"fmt"
"github.com/tetsuo/warp"
"github.com/tetsuo/warp/result"
)
func main() {
errEmptySlice := errors.New("empty slice")
// head returns the first value contained in a slice, or fails with
// an empty slice error.
head := func(as []float64) warp.Result[float64] {
if len(as) == 0 {
return result.Error[float64](errEmptySlice)
}
return result.Ok(as[0])
}
errDivByZero := errors.New("cannot divide by zero")
// inverse returns an inverse of a number, or fails with
// division by zero error if 0 is encountered.
inverse := func(n float64) warp.Result[float64] {
if n == 0 {
return result.Error[float64](errDivByZero)
}
return result.Ok(1 / n)
}
double := func(n float64) float64 {
return n * 2
}
check := func(nums []float64) string {
return result.Reduce(
context.Background(),
result.Chain(
// double first number
result.Map(head(nums), double),
// take its inverse
inverse,
),
// error handler
func(err error) string {
return fmt.Sprintf("Error is %v", err)
},
// success handler
func(head float64) string {
return fmt.Sprintf("Result is %.3f", head)
},
)
}
fmt.Println(check([]float64{24, 25, 26}))
fmt.Println(check([]float64{0, 1, 2}))
fmt.Println(check([]float64{}))
}
Output: Result is 0.021 Error is cannot divide by zero Error is empty slice
Directories
¶
| Path | Synopsis |
|---|---|
|
Package event implements the Event type.
|
Package event implements the Event type. |
|
Package future implements the Future type.
|
Package future implements the Future type. |
|
Package io implements the IO type.
|
Package io implements the IO type. |
|
Package nilable implements the Nilable type.
|
Package nilable implements the Nilable type. |
|
Package result implements the Result type.
|
Package result implements the Result type. |
Click to show internal directories.
Click to hide internal directories.