1
0
mirror of https://github.com/IBM/fp-go.git synced 2025-06-17 00:07:49 +02:00

fix: add more examples

Signed-off-by: Dr. Carsten Leue <carsten.leue@de.ibm.com>
This commit is contained in:
Dr. Carsten Leue
2023-08-11 16:26:15 +02:00
parent bb630810fc
commit e9f03e2d26
31 changed files with 11709 additions and 11035 deletions

View File

@ -28,6 +28,22 @@ func OnNone(msg string, args ...any) func() error {
}
}
// OnSome generates a unary function that produces a formatted error
func OnSome[T any](msg string, args ...any) func(T) error {
l := len(args)
if l == 0 {
return func(value T) error {
return fmt.Errorf(msg, value)
}
}
return func(value T) error {
data := make([]any, l)
copy(data[1:], args)
data[0] = value
return fmt.Errorf(msg, data...)
}
}
// OnError generates a unary function that produces a formatted error. The argument
// to that function is the root cause of the error and the message will be augmented with
// a format string containing %w