From a1dc85b2f7744a1dd7a5dfc4089fe94892ab9555 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Christoffer=20M=C3=BCller=20Madsen?= Date: Sat, 8 Apr 2017 20:33:00 +0200 Subject: [PATCH] add scaling of servings for recipes --- html.scm | 10 +++--- parse.scm | 91 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 70 insertions(+), 31 deletions(-) diff --git a/html.scm b/html.scm index eecbde1..731a4a1 100644 --- a/html.scm +++ b/html.scm @@ -30,12 +30,14 @@ (define recipe-as-html (lambda (recipe) (string-append "

" - (title recipe) + (recipe-name recipe) "

" - "Ingredients
" - (ingredients-as-html (ingredients recipe)) + "Servings: " + (number->string (recipe-servings recipe)) + "
Ingredients
" + (ingredients-as-html (recipe-ingredients recipe)) "
Steps
" - (steps-as-html (steps recipe)) + (steps-as-html (recipe-steps recipe)) "
"))) (define pretty-ingredient diff --git a/parse.scm b/parse.scm index b238ce7..0489244 100644 --- a/parse.scm +++ b/parse.scm @@ -1,16 +1,21 @@ -;; Predicates - -(define title +(define recipe-name (lambda (recipe) - (car recipe))) + (cadr (assoc 'name recipe)))) -(define ingredients +(define recipe-ingredients (lambda (recipe) - (car (cdr recipe)))) + (cadr (assoc 'ingredients recipe)))) -(define steps +(define recipe-steps (lambda (recipe) - (car (cdr (cdr recipe))))) + (cadr (assoc 'steps recipe)))) + +(define recipe-servings + (lambda (recipe) + (if (recipe-specifies-servings? recipe) + (cadr (assoc 'servings recipe)) + 1) + )) (define ingredient-name (lambda (ingredient) @@ -28,7 +33,28 @@ "" (car (cdr ingredient))))) -;; +;; Predicates + +(define contains-ingredient? + (lambda (recipe search-ingredient) + (contains-ingredients? recipe (cons search-ingredient '())))) + +(define contains-ingredients? + (lambda (recipe search-ingredients) + (let ((ingredients (recipe-ingredients recipe))) + (cond [(null? search-ingredients) #t] + [(ingredient-by-name ingredients + (car search-ingredients)) + (contains-ingredients? recipe + (cdr search-ingredients))] + [else #f]) + ))) + +(define recipe-specifies-servings? + (lambda (recipe) + (assoc 'servings recipe))) + +;; Searching and indexing (define ingredient-ref (lambda (ingredients n) @@ -46,32 +72,43 @@ [else (ingredient-by-name (cdr ingredients) search-name)])))) -(define contains-ingredient? - (lambda (recipe search-ingredient) - (contains-ingredients? recipe (cons search-ingredient '())))) - -(define contains-ingredients? - (lambda (recipe search-ingredients) - (let ((ingredients (ingredients recipe))) - (cond [(null? search-ingredients) #t] - [(ingredient-by-name ingredients - (car search-ingredients)) - (contains-ingredients? recipe - (cdr search-ingredients))] - [else #f]) - ))) - (define recipes-by-ingredients (lambda (recipes search-ingredients) (filter (lambda (x) (contains-ingredients? x search-ingredients)) recipes))) (define recipe-by-name - (lambda (recipes name) + (lambda (name recipes) (if (null? recipes) #f (let ((recipe (car recipes))) - (if (equal? (title recipe) + (if (equal? (recipe-name recipe) name) recipe - (recipe-by-name (cdr recipes) name)))))) + (recipe-by-name name (cdr recipes))))))) + +;; Manipulation + +(define scale-recipe + (lambda (recipe wanted-servings) + (let ([name (recipe-name recipe)] + [ingredients (recipe-ingredients recipe)] + [steps (recipe-steps recipe)] + [servings (recipe-servings recipe)]) + (let ([new-name name] + [new-ingredients (map (lambda (ingr) + (list (* (/ (car ingr) + servings) + wanted-servings) + (cdr ingr))) + ingredients)] + [new-steps steps] + [new-servings wanted-servings]) + (make-recipe new-name new-servings new-ingredients new-steps) + )))) + +;; Constructors + +(define make-recipe + (lambda (name servings ingredients steps) + `((name ,name) (servings ,servings) (ingredients ,ingredients) (steps ,steps))))