Skip to content

Instantly share code, notes, and snippets.

@fczuardi
Last active June 9, 2019 13:33
Show Gist options
  • Save fczuardi/3967873ecd1f8fc7a56d8f16c5eb7c40 to your computer and use it in GitHub Desktop.
Save fczuardi/3967873ecd1f8fc7a56d8f16c5eb7c40 to your computer and use it in GitHub Desktop.
Caipyra Code Snippets
type product;
type payment;
type sale;
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
type payment;
type sale;
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
type payment;
type sale = {
products: list(product),
payments: list(payment),
};
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
type amount;
type provider;
type payment =
| Cash(amount)
| CreditCard(provider, amount);
type sale = {
products: list(product),
payments: list(payment),
};
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
module Payment = {
type amount;
type provider;
type t =
| Cash(amount)
| CreditCard(provider, amount);
};
type sale = {
products: list(product),
payments: list(Payment.t),
};
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
module Payment = {
type amount;
type provider;
type t =
| Cash(amount)
| CreditCard(provider, amount);
};
type sale = {
products: list(product),
payments: list(Payment.t),
};
/* implementation */
let addProduct = (cart, product) => cart @ [product];
let cart =
[]
->addProduct(GELATO_MEDIO)
->addProduct(AGUA_300ML)
->addProduct(CAFE_ESPRESSO);
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
module Payment = {
type amount;
type provider;
type t =
| Cash(amount)
| CreditCard(provider, amount);
};
type sale = {
products: list(product),
payments: list(Payment.t),
};
/* implementation */
let getPrice = product =>
switch (product) {
| GELATO_MEDIO => 14.00
| GELATO_PEQUENO => 12.00
| CAFE_ESPRESSO => 6.00
| AGUA_300ML => 4.00
};
let addProduct = (cart, product) => cart @ [product];
let cart =
[]
->addProduct(GELATO_MEDIO)
->addProduct(AGUA_300ML)
->addProduct(CAFE_ESPRESSO);
Js.log(getPrice(CAFE_ESPRESSO));
type product =
| AGUA_300ML
| GELATO_PEQUENO
| GELATO_MEDIO
| CAFE_ESPRESSO;
module Payment = {
type amount;
type provider;
type t =
| Cash(amount)
| CreditCard(provider, amount);
};
type sale = {
products: list(product),
payments: list(Payment.t),
};
/* implementation */
let getPrice = product =>
switch (product) {
| GELATO_MEDIO => 14.00
| GELATO_PEQUENO => 12.00
| CAFE_ESPRESSO => 6.00
| AGUA_300ML => 4.00
};
let addProduct = (cart, product) => cart @ [product];
let cart =
[]
->addProduct(GELATO_MEDIO)
->addProduct(AGUA_300ML)
->addProduct(CAFE_ESPRESSO);
let sumPrice = (subtotal, product) => getPrice(product) +. subtotal;
let evaluateTotal = ListLabels.fold_left(~f=sumPrice, ~init=0.0, _);
Js.log(evaluateTotal(cart));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment