Skip to content

Add MapWithError#43

Closed
NilsJPWerner wants to merge 3 commits intosamber:masterfrom
NilsJPWerner:map-with-error
Closed

Add MapWithError#43
NilsJPWerner wants to merge 3 commits intosamber:masterfrom
NilsJPWerner:map-with-error

Conversation

@NilsJPWerner
Copy link

@NilsJPWerner NilsJPWerner commented Mar 16, 2022

This pr adds both lo.MapWithError and lop.MapWithError.

This is very useful if your internal function returns an error and you want to short circuit the Map. Currently you can keep track of an error outside of Map, however it won't short circuit the mapping and any subsequent errors will overwrite each other. This is a common situation when doing anything io related.

To implement the parallel version I included the golang.org/x/sync/errgroup package in order to use errgroup.Group which implements go routine cancellation.

This is what lo.MapWithError looks like:

x, err := lo.MapWithError[int64, string]([]int64{1, 2, 3, 4}, func(x int64, _ int) (string, error) {
    if x == 4 {
        return nil, errors.New("x is 4")
    }
    return strconv.FormatInt(x, 10), nil
})
// nil
// error "x is 4"

Here is a less contrived example of lop.MapWithError that shows parallel http requests that could error.

x, err := lop.MapWithError[string, string]([]string{"a", "b", "c"}, func(x string, _ int) (string, error) {
    resp, err := http.Get("https://example.com/" + x)
    if err != nil {
        return "", err
    }

    body, err := ioutil.ReadAll(resp.Body)
    if err != nil {
        return "", err
    }

    return string(body), nil
})

@NilsJPWerner
Copy link
Author

Also might be worth incorporating the suggestion from #16 since there are no limits to the number of goroutines that can be started.

@NilsJPWerner
Copy link
Author

Hey @samber do you have time to look at this?

@dolmen
Copy link
Contributor

dolmen commented Jun 3, 2022

lo.MapWithError looks good to me.

But the implementation of lop.MapWithError is more controversial (use of external dependency golang.org/x/sync/errgroup) and should be in separate PR for further discussion. There is no need to merge the two functions at the same time.


// Map manipulates a slice and transforms it to a slice of another type.
// `iteratee` is call in parallel. Result keep the same order.
// `iteratee` is called in parallel. Result keep the same order.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change should be in a separate MR.

@NilsJPWerner
Copy link
Author

I removed the errGroup import and the comment fix

@NilsJPWerner
Copy link
Author

@samber what do you think?

@k20human
Copy link

Hi, i'm very interesting about this 👍🏻
Any news about this PR ?

@NilsJPWerner
Copy link
Author

Bump again @samber

@tooilxui
Copy link

I also agree with the value of this PR

@YamiOdymel
Copy link

It will be the best Christmas gift if it gets merged.

@tooilxui
Copy link

It will be the best Christmas gift if it gets merged.

So true

@deregtd
Copy link

deregtd commented Apr 10, 2025

:sadge:

I've been thinking about forking lo to get MapWithError for a while now. Keep hoping this will get merged but looks pretty dead.

@denis-bc
Copy link

@samber could you please review this. 🙏 We all see community needs it! 🙂

@samber
Copy link
Owner

samber commented Mar 2, 2026

✅ Done in #823

Please upgrade to v1.53.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

8 participants