Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Result.toMaybe,Maybe.map,Maybe.withDefault -> Result.map,Result.withDefault #287

Open
Janiczek opened this issue Feb 9, 2024 · 0 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@Janiczek
Copy link

Janiczek commented Feb 9, 2024

|> Result.toMaybe
|> Maybe.map foo
|> Maybe.withDefault default

⬇️

|> Result.map foo
|> Result.withDefault default

Slack thread: https://elmlang.slack.com/archives/C010RT4D1PT/p1707473993252109
(The main result is that we should focus on the single Maybe.map first, and only handle sequences of multiple Maybe.maps as a nice-to-have later.)

What the rule should do:
Remove an unneeded conversion from Result to Maybe if all we do with the Maybe later is to map+withDefault it.

Now that I write this, there is also a simpler variant without the maps:

|> Result.toMaybe
|> Maybe.withDefault default

⬇️

|> Result.withDefault default

So in its full generality, the rule should handle 0+ Maybe.maps in the same way.

What problems does it solve:
Needless conversion that could be skipped

Example of things the rule would report:

0 Maybe.maps:

|> Result.toMaybe
|> Maybe.withDefault default

⬇️

|> Result.withDefault default

1 Maybe.map:

|> Result.toMaybe
|> Maybe.map foo
|> Maybe.withDefault default

⬇️

|> Result.map foo
|> Result.withDefault default

(Nice to have) 2+ Maybe.maps:

|> Result.toMaybe
|> Maybe.map foo
|> Maybe.map bar
|> Maybe.withDefault default

⬇️

|> Result.map foo
|> Result.map bar
|> Result.withDefault default

Should this be part of the Simplify rule or should it be a new rule?

Should be part of Simplify. We intentionally don't have an opinion on fusing maps together in this simplification, so it should be non-controversial.

I am looking for:

  • Someone to implement it for me
@jfmengels jfmengels added enhancement New feature or request help wanted Extra attention is needed labels Mar 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants