Skip to content

Conversation

@caseyWebb
Copy link

@caseyWebb caseyWebb commented Jun 12, 2025

Replaces instances of

List.foldl Set.insert Set.empty items
    -> Set.fromList items

List.foldl (\(k, v) -> Dict.insert) Dict.empty items
    -> Dict.fromList items

@jfmengels
Copy link
Owner

jfmengels commented Jun 12, 2025

Hi @caseyWebb 👋
Thank you for making a PR ☺️

List.foldl Set.insert Set.empty items
--> Set.fromList items

This one sounds fine to me 👍

It would not replace a version with a lambda though which I guess would be nice, but that could be a future improvement.

List.foldl (\a b -> Set.insert a b) Set.empty items

Don't forget to add tests for this one.


List.foldl Dict.insert Dict.empty items
--> Dict.fromList items

This one doesn't work, or rather, the original line doesn't compile, so we should not try to simplify it
List.foldl (\(k, v) acc -> Dict.insert k v acc) Dict.empty [] would be what you were looking for. Which I guess we could simplify to Dict.fromList, but that's more work.


List.foldl (\x acc -> if condition x then Set.insert x acc else acc) Set.empty items
 --> items |> List.filterMap condition |> Set.fromList

List.foldl (\x acc -> if condition x then Dict.insert x acc else acc) Dict.empty items
 --> items |> List.filterMap condition |> Dict.fromList

These ones I don't think would be a good fit, because they're much slower. I tend to rewrite my code from the latter to the former when I need to crank out performance. Also, I think it should by |> List.filter condition?


In the future, don't hesitate to create an issue with the idea first to avoid you doing work on things that will not work out. But I sincerely hope you had fun building these anyway 😄

@caseyWebb
Copy link
Author

caseyWebb commented Jun 12, 2025

Thanks for the speedy reply!

This is very much a quick and dirty proof of concept, still very incomplete (as noted by the bad Dict simplification 😅). Mostly wanted to get some eyes on it. Tbh I initially started working on this to run as a one-time janitor in our codebase (and it's waaaay easier to fork elm-review-simplify for this kind of stuff than start a new rule from scratch–seriously great work here), but figured a PR doesn't hurt anything.

@caseyWebb
Copy link
Author

Yanked the conditionals, fixed the Dict case, added support for lambdas 🎉

@jfmengels
Copy link
Owner

Sorry that I took a bit longer to reply this time.

I think the code looks generally good, but I have noticed a few cases where the rule incorrectly applies a simplification. I've added those in this commit which you can cherry-pick. a98dc7c

Feel free to decide that you'd like to stick to a simpler set of simplifications. All is fine by me, and I wanted to say I highly appreciate the work you've put in, thank you ☺️

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.

2 participants