This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule TodoListTest do | |
| use ExUnit.Case | |
| doctest TodoList | |
| test "it can convert a cleaned csv line into a todo entry" do | |
| assert TodoList.convert_csv_line_to_entry("2018/12/19,Dentist") == %{ | |
| date: ~D[2018-12-19], | |
| title: "Dentist" | |
| } | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule TodoList do | |
| defstruct auto_id: 1, entries: %{} | |
| def new(entries \\ []) do | |
| Enum.reduce(entries, %TodoList{}, fn entry, todo_acc -> | |
| add_entry(todo_acc, entry) | |
| end) | |
| end | |
| def add_entry(todo_list, entry) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule Fraction do | |
| defstruct a: nil, b: nil | |
| def new(a, b) do | |
| %Fraction{a: a, b: b} | |
| end | |
| def value(%Fraction{a: a, b: b}) do | |
| a / b | |
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule RangeGenerator do | |
| @spec range(pos_integer(), pos_integer()) :: list(pos_integer()) | |
| def range(minimum, maximum) | |
| when is_integer(minimum) and minimum > -1 and | |
| is_integer(maximum) and maximum > minimum do | |
| make_range([], minimum, maximum) | |
| end | |
| defp make_range(current_list, minimum, maximum, iteration) do | |
| case iteration < minimum do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Remember that the end of a list is not nil, like a singly linked list like you might see in other languages. | |
| # In Elixir, the list is always of pattern [head | tail], not something like [head | nil]. | |
| # What you might think could be [head | nil] is actaully like this: [head | []] i.e., an empty list. | |
| defmodule ListHelper do | |
| def list_len(list) do | |
| get_len(0, list) | |
| end | |
| defp get_len(current_len, []) do |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Tail calls are actually very efficient in Elixir as they don't involve stack pushes, | |
| # they are closer to a GO TO in something like FORTRAN. | |
| defmodule TimeTeller do | |
| @moduledoc """ | |
| A simple way to create a clock in the console that updates every second. | |
| """ | |
| def clock(:first) do | |
| IO.puts(NaiveDateTime.utc_now()) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| defmodule NaturalNums do | |
| # Base case: if n is 1, print 1 | |
| def print(1) do | |
| IO.puts(1) | |
| end | |
| # Recursive case: if n is a positive integer greater than 1, print numbers from 1 to n | |
| # The guard clause "when n > 1 and is_integer(n)" ensures that this function is only called for positive integers greater than 1 | |
| def print(n) when n > 1 and is_integer(n) do | |
| print(n - 1) |