# paulfioravanti's solution

## to Allergies in the Elm Track

Published at Jul 28 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution was written.

Given a person's allergy score, determine whether or not they're allergic to a given item, and their full list of allergies.

An allergy test produces a single numeric score which contains the information about all the allergies the person has (that they were tested for).

The list of items (and their value) that were tested are:

• eggs (1)
• peanuts (2)
• shellfish (4)
• strawberries (8)
• tomatoes (16)
• chocolate (32)
• pollen (64)
• cats (128)

So if Tom is allergic to peanuts and chocolate, he gets a score of 34.

Now, given just that score of 34, your program should be able to say:

• Whether Tom is allergic to any one of those allergens listed above.
• All the allergens Tom is allergic to.

Note: a given score may include allergens not listed above (i.e. allergens that score 256, 512, 1024, etc.). Your program should ignore those components of the score. For example, if the allergy score is 257, your program should only report the eggs (1) allergy.

## Elm Installation

Refer to the Installing Elm page for information about installing elm.

## Writing the Code

The first time you start an exercise, you'll need to ensure you have the appropriate dependencies installed. Thankfully, Elm makes that easy for you and will install dependencies when you try to run tests or build the code.

Execute the tests with:

``````\$ elm-test
``````

Automatically run tests again when you save changes:

``````\$ elm-test --watch
``````

As you work your way through the test suite, be sure to remove the `skip <|` calls from each test until you get them all passing!

## Source

Jumpstart Lab Warm-up http://jumpstartlab.com

## Submitting Incomplete Solutions

It is possible to submit an incomplete solution so you can see how others have completed the exercise.

### Tests.elm

``````module Tests exposing (tests)

import Allergies exposing (isAllergicTo, toList)
import Expect
import List
import Test exposing (..)

tests : Test
tests =
describe "Allergies"
[ describe "isAllergicTo"
[ describe "no allergies means not allergic"
[ test "peanuts" <|
\() -> Expect.equal False (isAllergicTo "peanuts" 0)
, skip <|
test "cats" <|
\() -> Expect.equal False (isAllergicTo "cats" 0)
, skip <|
test "strawberries" <|
\() -> Expect.equal False (isAllergicTo "strawberries" 0)
]
, skip <|
test "is allergic to eggs" <|
\() -> Expect.equal True (isAllergicTo "eggs" 1)
, describe "has the right allergies"
[ skip <|
test "eggs" <|
\() -> Expect.equal True (isAllergicTo "eggs" 5)
, skip <|
test "shellfish" <|
\() -> Expect.equal True (isAllergicTo "shellfish" 5)
, skip <|
test "strawberries" <|
\() -> Expect.equal False (isAllergicTo "strawberries" 5)
]
]
, describe "toList"
[ skip <|
test "no allergies at all" <|
\() -> Expect.equal [] (toList 0)
, skip <|
test "allergic to just peanuts" <|
\() -> Expect.equal [ "peanuts" ] (toList 2)
, skip <|
test "allergic to everything" <|
\() ->
Expect.equal (List.sort [ "eggs", "peanuts", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats" ])
(255 |> toList |> List.sort)
, skip <|
test "ignore non allergen score parts" <|
\() -> Expect.equal [ "eggs" ] (toList 257)
, skip <|
test "ignore all non allergen score parts" <|
\() ->
Expect.equal (List.sort [ "eggs", "shellfish", "strawberries", "tomatoes", "chocolate", "pollen", "cats" ])
(509 |> toList |> List.sort)
]
]``````
``````module Allergies exposing (isAllergicTo, toList)

import Bitwise

isAllergicTo : String -> Int -> Bool
isAllergicTo name score =
score
|> toList
|> List.member name

toList : Int -> List String
toList score =
allergens
|> List.indexedMap Tuple.pair
|> List.filter (isAllergen score)
|> List.map Tuple.second

-- PRIVATE

allergens : List String
allergens =
[ "eggs"
, "peanuts"
, "shellfish"
, "strawberries"
, "tomatoes"
, "chocolate"
, "pollen"
, "cats"
]

isAllergen : Int -> ( Int, String ) -> Bool
isAllergen score ( index, _ ) =
let
indexAllergenScore =
index
|> allergenScore
|> Bitwise.and score
in
indexAllergenScore > 0

allergenScore : Int -> Int
allergenScore index =
1 |> Bitwise.shiftLeftBy index``````