jfmengels / elm-review-simplify

Provides elm-review rules to simplify your Elm code
https://package.elm-lang.org/packages/jfmengels/elm-review-simplify/latest/
BSD 3-Clause "New" or "Revised" License
20 stars 9 forks source link

List.length .isEmpty checks on Dict, Set and Array.toList (and more) #293

Closed morteako closed 4 months ago

morteako commented 4 months ago

Simplifies uneccessary convertions from a Collection into List before a List.length / List.size, when you could just do Collection.size or Collection.isEmpty

Mentioned before in comment here: https://github.com/jfmengels/elm-review-simplify/issues/290#issuecomment-1975196945

Also adds one simplification for Array.length (Array.fromList list)

Simplifications:

Array

-- The following simplification also works for Array.toIndexedList
List.length (Array.toList array)
--> Array.length array

-- The following simplification also works for Array.toIndexedList
List.isEmpty (Array.toList array)
--> Array.isEmpty array

Array.length (Array.fromList list)
--> List.length list

Set

List.length (Set.toList set)
--> Set.size set

List.isEmpty (Set.toList set)
--> Set.isEmpty set

Dict

-- The following simplification also works for Dict.keys, Dict.values
List.length (Dict.toList dict)
--> Dict.size dict

-- The following simplification also works for Dict.keys, Dict.values
List.isEmpty (Dict.toList dict)
--> Dict.isEmpty dict
lue-bird commented 4 months ago

If you want, add this to CHANGELOG.md under unreleased:

The rule now simplifies:
- `Array.length (Array.fromList list)` to `List.length list`
- `List.length (Array.toList array)` to `Array.length array` (same for `Array.toIndexedList`)
- `List.isEmpty (Array.toList array)` to `Array.isEmpty array` (same for `Array.toIndexedList`)
- `List.length (Set.toList set)` to `Set.size set`
- `List.isEmpty (Set.toList set)` to `Set.isEmpty set`
- `List.length (Dict.toList dict)` to `Dict.size dict` (same for `Dict.values` and `Dict.keys`)
- `List.isEmpty (Dict.toList dict)` to `ict.isEmpty dict` (same for `Dict.values` and `Dict.keys`)