Responses for exercises in Exercism.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

142 lines
5.5 KiB

# Lasagna
Welcome to Lasagna on Exercism's Elixir Track.
If you need help running the tests or submitting your code, check out `HELP.md`.
If you get stuck on the exercise, check out `HINTS.md`, but try and solve it without using those first :)
## Introduction
## Basics
### Variables
Elixir is a dynamically-typed language, meaning that the type of a variable is only checked at runtime. Using the match `=` operator, we can bind a value of any type to a variable name:
```elixir
count = 1 # Bound an integer value of 1
count = 2 # You may re-bind variables
count = false # You may re-bind any type to a variable
message = "Success!" # Strings can be created by enclosing characters within double quotes
```
### Modules
Elixir is an [functional-programming language][functional-programming] and requires all named functions to be defined in a _module_. The `defmodule` keyword is used to define a module. All modules are available to all other modules at runtime and do not require an _access modifier_ to make them visible to other parts of the program. A _module_ is analogous to a _class_ in other programming languages.
```elixir
defmodule Calculator do
# ...
end
```
### Named functions
_Named Functions_ must be defined in a module. Each function can have zero or more arguments. All arguments are dynamically-typed, and the return type is not explicitly declared, it is the type of the value returned. An _access modifier_ can be specified for functions, making only desired functions available for use external to the module. In a function, the value of the last line is _implicitly returned_ to the calling function.
Invoking a function is done by specifying its module- and function name and passing arguments for each of the function's arguments. The module name may be omitted if the function is invoked inside of the module.
You may also write short functions using a one-line syntax (note the comma `,` and the colon `:` around the keyword `do`).
```elixir
defmodule Calculator do
def add(x, y) do
x + y
end
def short_add(x, y), do: x + y
end
sum = Calculator.add(1, 2)
# => 3
sum = Calculator.short_add(2, 2)
# => 4
```
### Arity of functions
It is common to refer to functions with their _arity_. The _arity_ of a function is the number of arguments it accepts.
```elixir
# add/3 because this function has three arguments, thus an arity of 3
def add(x, y, z) do
x + y + z
end
```
### Standard library
Elixir has a very rich and well-documented standard library. The documentation is available online at [hexdocs.pm/elixir][docs]. Save this link somewhere - you will use it a lot!
Most built-in data types have a corresponding module that offers functions for working with that data type, e.g. there's the `Integer` module for integers, `String` module for strings, `List` module for lists and so on.
A notable module is the `Kernel` module. It provides the basic capabilities on top of which the rest of the standard library is built, like arithmetic operators, control-flow macros, and much more. Functions for the `Kernel` module are automatically imported, so you can use them without the `Kernel.` prefix.
### Code comments
Comments can be used to leave notes for other developers reading the source code. Single line comments in Elixir are preceded by `#`.
[functional-programming]: https://en.wikipedia.org/wiki/Functional_programming
[docs]: https://hexdocs.pm/elixir/Kernel.html#content
## Instructions
In this exercise you're going to write some code to help you cook a brilliant lasagna from your favorite cooking book.
You have five tasks, all related to the time spent cooking the lasagna.
## 1. Define the expected oven time in minutes
Define the `Lasagna.expected_minutes_in_oven/0` method that does not take any arguments and returns how many minutes the lasagna should be in the oven. According to the cooking book, the expected oven time in minutes is 40:
```elixir
Lasagna.expected_minutes_in_oven()
# => 40
```
## 2. Calculate the remaining oven time in minutes
Define the `Lasagna.remaining_minutes_in_oven/1` method that takes the actual minutes the lasagna has been in the oven as a argument and returns how many minutes the lasagna still has to remain in the oven, based on the expected oven time in minutes from the previous task.
```elixir
Lasagna.remaining_minutes_in_oven(30)
# => 10
```
## 3. Calculate the preparation time in minutes
Define the `Lasagna.preparation_time_in_minutes/1` method that takes the number of layers you added to the lasagna as a argument and returns how many minutes you spent preparing the lasagna, assuming each layer takes you 2 minutes to prepare.
```elixir
Lasagna.preparation_time_in_minutes(2)
# => 4
```
## 4. Calculate the total working time in minutes
Define the `Lasagna.total_time_in_minutes/2` method that takes two arguments: the first argument is the number of layers you added to the lasagna, and the second argument is the number of minutes the lasagna has been in the oven. The function should return how many minutes in total you've worked on cooking the lasagna, which is the sum of the preparation time in minutes, and the time in minutes the lasagna has spent in the oven at the moment.
```elixir
Lasagna.total_time_in_minutes(3, 20)
# => 26
```
## 5. Create a notification that the lasagna is ready
Define the `Lasagna.alarm/0` method that does not take any arguments and returns a message indicating that the lasagna is ready to eat.
```elixir
Lasagna.alarm()
# => "Ding!"
```
## Source
### Created by
- @neenjaw
### Contributed to by
- @angelikatyborska