Skip to content

Instantly share code, notes, and snippets.

@Kazark
Last active August 29, 2015 14:20
Show Gist options
  • Save Kazark/ac732647a189e6a8d282 to your computer and use it in GitHub Desktop.
Save Kazark/ac732647a189e6a8d282 to your computer and use it in GitHub Desktop.
Imperative versus Functional solution of Pizza Problem
public class Pizza
{
public string Name { get; private set; }
private List<string> _ingredients;
public bool ContainsNuts { get; private set; }
public Pizza(string name, List<string> ingredients, bool containsNuts)
{
Name = name;
_ingredients = ingredients;
ContainsNuts = containsNuts;
}
public bool IngredientsContain(string ingredient)
{
foreach (var i in _ingredients)
{
if (i == ingredient)
{
return true;
}
}
return false;
}
}
var menu = new List<Pizza>
{
new Pizza("Sonoma", new List<string> {"artichoke", "sundried tomatoes", "mushrooms"}, false),
new Pizza("Pizza Primavera", new List<string> {"roma", "sundried tomatoes", "goats cheese", "rosemary"}, false),
new Pizza("South Of The Border", new List<string> {"black beans", "jalapenos", "mushrooms"}, false),
new Pizza("Blue Moon", new List<string> {"blue cheese", "garlic", "walnuts"}, true),
new Pizza("Taste Of Athens", new List<string> {"spinach", "kalamata olives", "sesame seeds"}, true)
};
Console.Write("If you are allergic to nuts and hate mushrooms, you can eat: ");
foreach (var pizza in menu)
{
if (!pizza.ContainsNuts && !pizza.IngredientsContain("mushrooms"))
{
Console.Write(pizza.Name + " ");
}
}
Console.WriteLine();
(* This example is inspired by a problem from Mr. David Laing's JavaScript
* Koans:
* https://github.com/mrdavidlaing/javascript-koans/blob/master/koans/AboutApplyingWhatWeHaveLearnt.js
* GistID: ac732647a189e6a8d282
* *)
type Pizza = {
Name : string
Ingredients : string list
ContainsNuts : bool
}
module Pizzeria =
let menu = [
{ Name = "Sonoma"; Ingredients = ["artichoke"; "sundried tomatoes"; "mushrooms"]; ContainsNuts = false }
{ Name = "Pizza Primavera"; Ingredients = ["roma"; "sundried tomatoes"; "goats cheese"; "rosemary"]; ContainsNuts = false }
{ Name = "South Of The Border"; Ingredients = ["black beans"; "jalapenos"; "mushrooms"]; ContainsNuts = false }
{ Name = "Blue Moon"; Ingredients = ["blue cheese"; "garlic"; "walnuts"]; ContainsNuts = true }
{ Name = "Taste Of Athens"; Ingredients = ["spinach"; "kalamata olives"; "sesame seeds"]; ContainsNuts = true }
];
let ``Find a pizza for someone who is allergic to nuts and hates mushrooms`` pizzas =
let safeToEat pizza = not pizza.ContainsNuts
let likeToEat pizza = not <| List.exists ((=) "mushrooms") pizza.Ingredients
let safeAndYummmy pizza = safeToEat pizza && likeToEat pizza
List.filter safeAndYummmy pizzas
|> List.map (fun pizza -> pizza.Name)
Pizzeria.``Find a pizza for someone who is allergic to nuts and hates mushrooms`` Pizzeria.menu
|> printf "If you are allergic to nuts and hate mushrooms, you can eat: %A\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment