FromChan converts a regular channel into a stream. Additionally, this function can take an error, that will be added to the output stream alongside the values. Either argument can be nil, in which case it is ignored. If both arguments are nil, the function returns nil. Such function signature allow
(values <-chan A, err error)
| 76 | // |
| 77 | // stream := rill.FromChan(someFunc()) |
| 78 | func FromChan[A any](values <-chan A, err error) <-chan Try[A] { |
| 79 | if values == nil && err == nil { |
| 80 | return nil |
| 81 | } |
| 82 | |
| 83 | out := make(chan Try[A]) |
| 84 | go func() { |
| 85 | defer close(out) |
| 86 | |
| 87 | // error goes first |
| 88 | if err != nil { |
| 89 | out <- Try[A]{Error: err} |
| 90 | } |
| 91 | |
| 92 | for x := range values { |
| 93 | out <- Try[A]{Value: x} |
| 94 | } |
| 95 | }() |
| 96 | |
| 97 | return out |
| 98 | } |
| 99 | |
| 100 | // FromChans converts a regular channel into a stream. |
| 101 | // Additionally, this function can take a channel of errors, which will be added to |
no outgoing calls