-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from go-utils/feature/superlo/main
Upgrade superlo
- Loading branch information
Showing
4 changed files
with
102 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,2 @@ | ||
github.com/samber/lo v1.38.1 h1:j2XEAqXKb09Am4ebOg31SpvzUTTs6EN3VfgeLUhPdXM= | ||
github.com/samber/lo v1.38.1/go.mod h1:+m/ZKRl6ClXCE2Lgf3MsQlWfh4bn1bz6CXEOxnEXnEA= | ||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17 h1:3MTrJm4PyNL9NBqvYDSj3DHl46qQakyfqfWo4jgfaEM= | ||
golang.org/x/exp v0.0.0-20220303212507-bbda1eaf7a17/go.mod h1:lgLbSvA5ygNOMpwM/9anMpWVlVJ7Z+cHWq/eFuinpGE= | ||
golang.org/x/sync v0.5.0 h1:60k92dhOjHxJkrqnwsfl8KuaHbn/5dl0lUPUklKo3qE= | ||
golang.org/x/sync v0.5.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,56 @@ | ||
package superlo | ||
|
||
import "github.com/samber/lo" | ||
import ( | ||
"sync" | ||
|
||
"golang.org/x/sync/errgroup" | ||
) | ||
|
||
// Map - returns a new collection of mapped values and error | ||
func Map[T any, R any](collection []T, iteratee func(item T, index int) (R, error)) (mappedCollection []R, topErr error) { | ||
defer func() { | ||
_ = recover() | ||
}() | ||
return lo.Map(collection, func(item T, index int) R { | ||
returnValue, err := iteratee(item, index) | ||
func Map[T any, R any](collection []T, iteratee func(item T, index int) (R, error)) ([]R, error) { | ||
result := make([]R, len(collection)) | ||
|
||
for i, item := range collection { | ||
returnValue, err := iteratee(item, i) | ||
if err != nil { | ||
topErr = err | ||
panic(nil) | ||
return nil, err | ||
} | ||
return returnValue | ||
}), nil | ||
result[i] = returnValue | ||
} | ||
|
||
return result, nil | ||
} | ||
|
||
// ParallelMap - returns a new collection of mapped values and error | ||
// `iteratee` is call in parallel. Result keep the same order. | ||
func ParallelMap[T any, R any](collection []T, iteratee func(item T, index int) (R, error)) ([]R, error) { | ||
result := make([]R, len(collection)) | ||
|
||
var ( | ||
eg errgroup.Group | ||
mu sync.Mutex | ||
) | ||
|
||
for i, item := range collection { | ||
func(_item T, _i int) { | ||
eg.Go(func() error { | ||
returnValue, err := iteratee(_item, _i) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
mu.Lock() | ||
result[_i] = returnValue | ||
mu.Unlock() | ||
|
||
return nil | ||
}) | ||
}(item, i) | ||
} | ||
|
||
if err := eg.Wait(); err != nil { | ||
return nil, err | ||
} | ||
|
||
return result, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters