Module:Curry

From Wikipedia

Documentation for this module may be created at Module:Curry/doc

--[[ 
 A module to generate a random curry recipe. Uses some functional programming techniques to archieve
 an incredibly trivial goal, but you might learn a thing or two about Lua.
 
 Invoke it like this:
    
    {{#invoke:Curry|curry|}}

Jens Ohlig 17:47, 2 June 2012 (UTC)
--]]

-- Random Permutation
permute = function(tab, n, count)
  n = n or #tab
  for i = 1, count or n do
    local j = math.random(i, n)
    tab[i], tab[j] = tab[j], tab[i]
  end
  return tab
end
-- Use Lua's metatable protocol to implement a lazy table technique
do
  local meta = {}
  function meta:__index(k) return k end
  function PositiveIntegers() return setmetatable({}, meta) end
end
-- Random sample
random_sample = function(count, range)
  return {unpack(
               permute(PositiveIntegers(), range, count),
               1, count)
            }
end
-- map
map = function(func, array)
  local new_array = {}
  for i,v in ipairs(array) do
    new_array[i] = func(v)
  end
  return new_array
end
element = function(array)
    return function(x)
        return "* " .. array[x] .. "\n"
    end
end
-- Here is a function to return curried functions to pick stuff for the recipe. Curry, get it?
pick = function(l) 
  return function(n)
      local l_elem = element(l)
      return (function(x) if (x==1) then return "* " .. l[math.random(#l)] .. "\n" else return map(l_elem, random_sample(x, #l)) end end)(n)
  end
end
-- Helpers for printing
recipe = ""
print = function(s)
    if (type(s) == "string") then
        recipe = recipe .. s .. "\n"
    elseif (type(s) == "table") then
        for i,v in ipairs(s) do
            recipe = recipe .. v .. "\n"
        end
    end
end

spices = { 
           "Cinnamon stick", 
           "Cumin", 
           "Coriander", 
           "Cloves", 
           "Cardamom, preferably both green and black", 
           "Black peppercorns",
           "Red chilis, or red chili powder",
           "Wet ginger paste (go to an Indian grocer’s), or fresh ginger, never ever ever powdered ginger",
           "Garam masala", 
           "Turmeric"
         }
base = {
          "Sauteed and pureed yellow onions",
          "Plain yogurt with heavy cream added as a thickener",
          "Coconut milk"
       }
ingredients = {
          "Chicken",
          "Mutton",
          "Cauliflower and potatoes",
          "Paneer",
          "Eggplants"
       }
media = { 
          "[[File:Curry Ist.jpg|thumb]]",
          "[[File:Curry Powder.JPG|thumb]]",
          "[[File:Goat Curry, rice and naan.jpg|thumb]]", 
          "[[File:Vegetarian Curry.jpeg|thumb]]",
          "[[File:Meen curry 2.JPG|thumb]]",
          "[[File:Ipoh Curry Mee.jpg|thumb]]",
          "[[File:Chicken curry rice.jpg|thumb]]"
    }

pickSpices = pick(spices)
pickBase = pick(base)
pickIngredient = pick(ingredients)

-- Print out the recipe
curry = function(frame)
    math.randomseed(os.time())
    local random_media = permute(media)
    for i = 1, 3 do
        print(random_media[i])
    end
    print("Start by creating a base for your curry. For this curry, I suggest: ")
    print(pickBase(1))
    print("Fry the spices in a pan. You may want to use: ")
    print(pickSpices(5))
    print("Fry the hard ones for two to three minutes over medium heat (3.5 on an electric stove) and puree them.  Cinnamon stick should be left whole in the sauce to leach out its flavor.  Never are more than three cloves needed and they can be left whole too.  Cardamoms can be inserted whole and then removed, especially if large ones are smashed open a bit with a blunt edge.  Otherwise experiment with preferred combinations.")
    print("\nNow for the main ingredient. We will be using this as a main ingredient in our curry:")
    print(pickIngredient(1))
    print("In a separate pan, quickly cook your ingredient over high heat, then put it into the liquid base together with the spices and turn to low heat until the dish is ready.")
    print("\n\n'''Enjoy!'''")
    return recipe
end
-- Interface http://scribunto.wmflabs.org/
local p = {}
p.curry = curry
return p