-
Notifications
You must be signed in to change notification settings - Fork 1
/
curry.elm
103 lines (83 loc) · 2.97 KB
/
curry.elm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
module CurryGenerator where
import Html
import Html.Attributes as Attr
import Html.Events as Events
import StartApp.Simple as StartApp
import Random
import Random.Array as ShuffleArray
import Array
import String
-- StartApp
main =
StartApp.start { model = model, view = view, update = update }
-- My lists the ingredients
base =
Array.fromList [ "deglazed onions", "coconut milk", "tomatoes", "joghurt and creme" ]
spices =
Array.fromList [ "pepper", "Garam Masala", "cumin", "cardamom", "mustard seeds", "cinnamon", "clove", "chilli", "ginger" ]
mainIngredient =
Array.fromList [ "chicken", "lamb", "cauliflower", "aubergine" ]
--------------------------------------------------------------------------
-- Model holds the current recipe and a seed
type alias Model =
{ base : String
, spices : String
, mainIngredient : String
, seed : Random.Seed
}
-- initial model
model =
{ base = ""
, spices = ""
, mainIngredient = ""
, seed = Random.initialSeed newDate
}
--------------------------------------------------------------------------
-- simple view, which needs to get prettier
view address model =
Html.div []
[ Html.button [ Events.onClick address Generate ] [ Html.text "Generate recipe" ]
, Html.div [] [ Html.text ("This will be you watery base: " ++ model.base) ]
, Html.div [] [ Html.text ("You can use these spices: " ++ model.spices) ]
, Html.div [] [ Html.text ("And add this as your main ingredient: " ++ model.mainIngredient) ]
]
--------------------------------------------------------------------------
port newDate : Int
type Action = Generate
-- update is simple
update action model =
case action of
Generate -> createRandomRecipe model.seed
--------------------------------------------------------------------------
-- this creates a new model, but delegates all of the heavy lifting
createRandomRecipe : Random.Seed -> Model
createRandomRecipe seed =
let
(newBase, seedOne) = pick base 1 seed
(newSpices, seedTwo) = pick spices 3 seedOne
(newMainIngredient, seedThree) = pick mainIngredient 1 seedTwo
in
{ base = newBase
, spices = newSpices
, mainIngredient = newMainIngredient
, seed = seedThree
}
--------------------------------------------------------------------------
-- generalized function, the only one which has nothing to do with food ;)
pick : Array.Array String -> Int -> Random.Seed -> (String, Random.Seed)
pick input count seed =
let
(shuffledArray, newSeed) = ShuffleArray.shuffle seed input
result = shuffledArray |> Array.toList |> List.take count
prettyResult = String.join ", " result
in
(prettyResult, newSeed)
--------------------------------------------------------------------------
-- uses semi-random data to generate a new seed for the next recipe
getNewSeed : Model -> Random.Seed
getNewSeed model =
let
gen = Random.int (String.length model.base) (String.length model.mainIngredient)
(_, seed) = Random.generate gen model.seed
in
seed