Skip to content

Conversation

WillardHu
Copy link
Contributor

Overview

Sometimes, errors can occur when processing data transformations. The SliceMap function is relatively weak in error handling. We've add a SliceMapE function to handle these scenarios and added unit tests for both functions.

@vMaroon
Copy link
Member

vMaroon commented Sep 17, 2025

Hi @WillardHu - I think abstractions are justified by use mostly. While this sounds reasonable, do you intend to propose using it instead of SliceMap in some instances?

@WillardHu
Copy link
Contributor Author

WillardHu commented Sep 18, 2025

The places where SliceMap is used now seem to be simple object conversions, but if different types of conversions are encountered in the future, an error handling may be required, such as string to constant enumeration, string to integer, etc.

For Example:

type Status string
const (
    StatusTrue Status = "True"
    StatusFalse Status = "False"
)

func Test() {
    items = []string{"True", "T"}
    statusItems, err := SliceMapE(items, func(s string)(Status, error) {
        switch s {
        case string(StatusTrue):
            return StatusTrue
        case string(StatusFalse):
            return StatusFalse
        default:
            return "", errors.New("...")
        }
    })
}

However, I think SliceMap is also necessary. If the conversion does not require error handling, there is no need to write a meaningless error handling.

func Test() {
    items = []int{1, 2}
    intItems, err := SliceMapE(items, func(s int)(string, error) {
        return strconv.Itoa(1), nil
    })
    if err != nil {...} // Meaningless error handling
}

@vMaroon
Copy link
Member

vMaroon commented Sep 19, 2025

Like I said in the other comment - in general abstractions should exist only when used, but I think this one may come in handy at some point or another. The test is good - thank you @WillardHu!

@vMaroon
Copy link
Member

vMaroon commented Sep 19, 2025

/lgtm
/approve

@github-actions github-actions bot added the lgtm label Sep 19, 2025
@github-actions github-actions bot merged commit f7d7e27 into llm-d:main Sep 19, 2025
2 checks passed
@WillardHu WillardHu deleted the util-slice branch September 28, 2025 07:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants