MCPcopy
hub / github.com/destel/rill / ExampleCatch

Function ExampleCatch

example_test.go:398–420  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

396}
397
398func ExampleCatch() {
399 // Convert a slice of strings into a stream
400 strs := rill.FromSlice([]string{"1", "2", "3", "4", "5", "not a number 6", "7", "8", "9", "10"}, nil)
401
402 // Convert strings to ints
403 // Concurrency = 3
404 ids := rill.Map(strs, 3, func(s string) (int, error) {
405 randomSleep(500 * time.Millisecond) // simulate some additional work
406 return strconv.Atoi(s)
407 })
408
409 // Catch and ignore number parsing errors
410 // Concurrency = 2
411 ids = rill.Catch(ids, 2, func(err error) error {
412 if errors.Is(err, strconv.ErrSyntax) {
413 return nil // Ignore this error
414 }
415 return err
416 })
417
418 // No error will be printed
419 printStream(ids)
420}
421
422// The same example as for the [Catch], but using ordered versions of functions.
423func ExampleOrderedCatch() {

Callers

nothing calls this directly

Calls 5

FromSliceFunction · 0.92
MapFunction · 0.92
CatchFunction · 0.92
printStreamFunction · 0.85
randomSleepFunction · 0.70

Tested by

no test coverage detected