Skip to content

Instantly share code, notes, and snippets.

@chase-lambert
Created April 23, 2025 01:05
Show Gist options
  • Save chase-lambert/d554ecb0537a2dfbb10e9eb1adf8b617 to your computer and use it in GitHub Desktop.
Save chase-lambert/d554ecb0537a2dfbb10e9eb1adf8b617 to your computer and use it in GitHub Desktop.
rendezvous with cassidoo challenge: 25.04.21
#[derive(Debug, PartialEq)]
pub struct Ingredient<'a> {
pub name: &'a str,
pub amount: u32,
}
impl<'a> Ingredient<'a> {
fn new(name: &'a str, amount: u32) -> Self {
Self { name, amount }
}
}
pub fn calculate_ingredients<'a>(
ingredients: &'a [Ingredient],
target_servings: u32,
) -> Vec<Ingredient<'a>> {
ingredients
.iter()
.map(|ingredient| Ingredient::new(ingredient.name, ingredient.amount * target_servings))
.collect()
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn calculate_ingredients_test() {
let ingredients = vec![
Ingredient::new("flour", 200),
Ingredient::new("sugar", 100),
Ingredient::new("eggs", 2),
];
let target_servings = 3;
let result = calculate_ingredients(&ingredients, target_servings);
let expected = vec![
Ingredient::new("flour", 600),
Ingredient::new("sugar", 300),
Ingredient::new("eggs", 6),
];
assert_eq!(result, expected);
}
}
@chase-lambert
Copy link
Author

;; Clojure version

(defn calculate-ingredients [ingredients target-servings]
  (for [{:keys [name amount]} ingredients]
    {:name name 
     :amount (* amount target-servings)}))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment