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

Add Allergies exercise #101

Merged
merged 3 commits into from
Sep 21, 2024
Merged

Add Allergies exercise #101

merged 3 commits into from
Sep 21, 2024

Conversation

isaacvando
Copy link
Contributor

No description provided.

@ageron
Copy link
Contributor

ageron commented Sep 21, 2024

Nice! I feel like the list function could be simpler:

list : U64 -> Set Allergen
list = \score ->
    [Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats]
    |> List.walk (score, Set.empty {}) \(bits, allergies), allergy ->
        updatedAllergies = if bits % 2 == 0 then allergies else allergies |> Set.insert allergy
        (bits // 2, updatedAllergies)
    |> .1

This removes the need for the functions largestPowerOfTwo and values.

Also, if list returns a Set, shouldn't we call it set?

@ageron
Copy link
Contributor

ageron commented Sep 21, 2024

Or maybe using a record rather than a tuple for the state would be clearer?

list : U64 -> Set Allergen
list = \score ->
    [Eggs, Peanuts, Shellfish, Strawberries, Tomatoes, Chocolate, Pollen, Cats]
    |> List.walk {bits: score, allergies: Set.empty {}} \{bits, allergies}, allergy ->
        updatedAllergies = if bits % 2 == 0 then allergies else allergies |> Set.insert allergy
        {bits: bits // 2, allergies: updatedAllergies}
    |> .allergies

@isaacvando
Copy link
Contributor Author

isaacvando commented Sep 21, 2024

Wow that solution is so much nicer! I was definitely over complicating things. Thanks @ageron! 😄
I also renamed the function to set and decreased the difficulty.

@isaacvando isaacvando merged commit aaa159a into exercism:main Sep 21, 2024
@isaacvando isaacvando deleted the allergies branch September 21, 2024 02:14
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