Skip to content

Commit

Permalink
add an example
Browse files Browse the repository at this point in the history
  • Loading branch information
camdencheek committed Jan 2, 2023
1 parent e6e82cf commit 1f9fc41
Showing 1 changed file with 47 additions and 0 deletions.
47 changes: 47 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,53 @@ func main() {
</tr>
</table>

Concurrently map a slice:

<table>
<tr>
<th><code>stdlib</code></th>
<th><code>conc</code></th>
</tr>
<tr>
<td>

```go
func concMap(input []int, f func(int) int) []int {
res := make([]int, len(input))
var idx atomic.Int64

var wg sync.WaitGroup
for i := 0; i < 10; i++ {
wg.Add(1)
go func() {
defer wg.Done()

for {
i := int(idx.Add(1) - 1)
if i >= len(input) {
return
}

res[i] = f(input[i])
}
}()
}
wg.Wait()
return res
}
```
</td>
<td>

```go
func concMap(input []int, f func(int) int) []int {
iter.Map(input, f)
}
```
</td>
</tr>
</table>

Process an ordered stream concurrently:


Expand Down

0 comments on commit 1f9fc41

Please sign in to comment.