This will define anything that we choose to export to the outside world importing our module. In this case, we only want to expose our Customer type alias & freePizza function for our tests (see below).
typealiasCustomer={name:String,purchases:ListInt}
This will define a customer in our application. Note that I choosed to use a name field for the name of the customer and a purchases field for the total record of the customer's purchases.
This will define the inner logic of the challenge. Basically what it does is:
Grab the purchases out of the current iterated object (customer). Almost anything in Elm is a function, so is .purchases which is a function that accepts a customer and returns a list of integers (the purchases). .purchases : Customer -> List Int. It is equivalent to this in JavaScript: const { purchases } = customer;.
Filter out all the customer's purchases that are not above or equal to the minimum order's price defined by our free pizza's policy. The ((<=) minimumPrice) part is equivalent to (\purchase -> purchase >= minimumPrice)
Calculate the length of the filtered list.
Check whether the length of the list is at least equal or above the minimum orders, which means that it will return true when the customer has made enough purchases.
This will filter out all the customer that are non-eligible to our current free pizza's policy.
Tests
moduleFreePizzaTestexposing(suite)importExpectexposing(equal)importFreePizzaexposing(Customer,freePizza)importTestexposing(Test,describe,test)suite:Testsuite=describe"Free Pizza"[test"it should return a set of customers who are eligible for a reward #1"<|\_->letminimumOrders:IntminimumOrders=5minimumPrice:IntminimumPrice=20customers:ListCustomercustomers=[{name="John Doe",purchases=[22,30,11,17,15,52,27,12]},{name="Jane Doe",purchases=[5,17,30,33,40,22,26,10,11,45]}]expectations:ListCustomerexpectations=[{name="Jane Doe",purchases=[5,17,30,33,40,22,26,10,11,45]}]inequalexpectations<|freePizzaminimumOrdersminimumPricecustomers,test"it should return a set of customers who are eligible for a reward #2"<|\_->letminimumOrders:IntminimumOrders=2minimumPrice:IntminimumPrice=50customers:ListCustomercustomers=[{name="Joey Bonzo",purchases=[22,67,53,29]},{name="Jennifer Bonzo",purchases=[51,19]}]expectations:ListCustomerexpectations=[{name="Joey Bonzo",purchases=[22,67,53,29]}]inequalexpectations<|freePizzaminimumOrdersminimumPricecustomers,test"it should return a set of customers who are eligible for a reward #3"<|\_->letminimumOrders:IntminimumOrders=3minimumPrice:IntminimumPrice=15customers:ListCustomercustomers=[{name="Natsumi Sakamoto",purchases=[15,15,14]},{name="Gorou Hironaka",purchases=[15,15,15]},{name="Shinju Tanabe",purchases=[120,240]}]expectations:ListCustomerexpectations=[{name="Gorou Hironaka",purchases=[15,15,15]}]inequalexpectations<|freePizzaminimumOrdersminimumPricecustomers]
Elm
Explainations
This will define anything that we choose to export to the outside world importing our module. In this case, we only want to expose our
Customer
type alias &freePizza
function for our tests (see below).This will define a customer in our application. Note that I choosed to use a
name
field for the name of the customer and apurchases
field for the total record of the customer's purchases.This will define the inner logic of the challenge. Basically what it does is:
.purchases
which is a function that accepts a customer and returns a list of integers (the purchases)..purchases : Customer -> List Int
. It is equivalent to this in JavaScript:const { purchases } = customer;
.((<=) minimumPrice)
part is equivalent to(\purchase -> purchase >= minimumPrice)
This will filter out all the customer that are non-eligible to our current free pizza's policy.
Tests
Playground
Test it online here.