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

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

Merged
merged 8 commits into from Mar 7, 2024

Conversation

morteako
Copy link
Contributor

@morteako morteako commented Mar 6, 2024

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: #290 (comment)

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

@morteako morteako force-pushed the list-length-and-isempty-checks branch from 58d57a9 to 54a4361 Compare March 6, 2024 19:22
@morteako morteako force-pushed the list-length-and-isempty-checks branch from 54a4361 to 57e6dc7 Compare March 6, 2024 19:40
Copy link
Collaborator

@lue-bird lue-bird left a comment

Choose a reason for hiding this comment

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

💙

@lue-bird lue-bird added the enhancement New feature or request label Mar 6, 2024
@lue-bird
Copy link
Collaborator

lue-bird commented Mar 6, 2024

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`)

Copy link
Owner

@jfmengels jfmengels left a comment

Choose a reason for hiding this comment

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

This looks good to me, thank you @morteako 😊

@jfmengels jfmengels merged commit 2c46808 into jfmengels:main Mar 7, 2024
2 checks passed
@morteako morteako deleted the list-length-and-isempty-checks branch March 7, 2024 07:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants