Skip to content

Instantly share code, notes, and snippets.

View samjowen's full-sized avatar
🐈

Samuel Owen samjowen

🐈
View GitHub Profile
@samjowen
samjowen / eia_chapter4_tests.exs
Created January 7, 2024 01:27
Elixir in Action Chapter 4 Part 2 of 2
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
@samjowen
samjowen / todo_crud.exs
Created January 7, 2024 01:26
Elixir in Action Chapter 4 Part 1 of 2
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
@samjowen
samjowen / fraction.ex
Created January 6, 2024 16:10
Elixir in Action - Fractions (Simple Structs and Data Abstraction example)
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
@samjowen
samjowen / RangeGenerator.exs
Created January 4, 2024 19:00
RangeGenerator Module - Elixir in Action Exercise
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
@samjowen
samjowen / SumListTailCall.exs
Created January 4, 2024 16:25
Elixir in Action - Tail Call Recursion - List Length Finder
# 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
@samjowen
samjowen / TimeTeller.exs
Created January 4, 2024 15:47
Elixir in Action - Tail Calls, loops and recursion reader exercise example code
# 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())
@samjowen
samjowen / natural_nums.exs
Created January 4, 2024 15:29
Elixir in Action - Iterating with recursion reader exercise
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)