First commit
This commit is contained in:
commit
1dba0af064
36 changed files with 1065 additions and 0 deletions
37
chapter3/enum_streams_practice.ex
Normal file
37
chapter3/enum_streams_practice.ex
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
defmodule FileHelper do
|
||||
defp filtered_lines!(path) do
|
||||
File.stream!(path)
|
||||
|> Stream.map(&String.replace(&1, "\n", ""))
|
||||
end
|
||||
|
||||
def line_lengths!(path) do
|
||||
path
|
||||
|> filtered_lines!()
|
||||
|> Enum.map(&String.length/1)
|
||||
end
|
||||
|
||||
def longest_line_length(path) do
|
||||
path
|
||||
|> filtered_lines!()
|
||||
|> Stream.map(&String.length/1)
|
||||
|> Enum.max()
|
||||
end
|
||||
|
||||
def longest_line(path) do
|
||||
path
|
||||
|> filtered_lines!()
|
||||
|> Enum.max_by(&String.length/1)
|
||||
end
|
||||
|
||||
def words_per_line(path) do
|
||||
path
|
||||
|> filtered_lines!()
|
||||
|> Enum.map(&word_count/1)
|
||||
end
|
||||
|
||||
def word_count(string) do
|
||||
string
|
||||
|> String.split()
|
||||
|> length()
|
||||
end
|
||||
end
|
||||
6
chapter3/enum_streams_practice.txt
Normal file
6
chapter3/enum_streams_practice.txt
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
This is a file
|
||||
that contains some words
|
||||
and is mean to be just an example
|
||||
to be used to practice
|
||||
with elixir streams
|
||||
and enums
|
||||
8
chapter3/natural_nums.ex
Normal file
8
chapter3/natural_nums.ex
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
defmodule NaturalNums do
|
||||
def print(1), do: IO.puts(1)
|
||||
|
||||
def print(n) when is_integer(n) and n > 1 do
|
||||
print(n - 1)
|
||||
IO.puts(n)
|
||||
end
|
||||
end
|
||||
21
chapter3/recursion_practice.ex
Normal file
21
chapter3/recursion_practice.ex
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
defmodule ListHelper do
|
||||
def list_len([]), do: 0
|
||||
|
||||
def list_len([_ | tail]) do
|
||||
1 + list_len(tail)
|
||||
end
|
||||
|
||||
def range(num, num), do: [num]
|
||||
|
||||
def range(num1, num2) do
|
||||
[num1 | range(num1 + 1, num2)]
|
||||
end
|
||||
|
||||
def positive([]), do: []
|
||||
|
||||
def positive([head | tail]) when head > 0 do
|
||||
[head | positive(tail)]
|
||||
end
|
||||
|
||||
def positive([_ | tail]), do: positive(tail)
|
||||
end
|
||||
33
chapter3/recursion_practice_tc.ex
Normal file
33
chapter3/recursion_practice_tc.ex
Normal file
|
|
@ -0,0 +1,33 @@
|
|||
defmodule ListHelper do
|
||||
def list_len(list) do
|
||||
list_len_helper(0, list)
|
||||
end
|
||||
|
||||
defp list_len_helper(current_len, []), do: current_len
|
||||
|
||||
defp list_len_helper(current_len, [_ | tail]) do
|
||||
list_len_helper(current_len + 1, tail)
|
||||
end
|
||||
|
||||
def range(num1, num2), do: range_helper([], num1, num2)
|
||||
|
||||
defp range_helper(current_list, num, num), do: [num | current_list]
|
||||
|
||||
defp range_helper(current_list, num1, num2) do
|
||||
range_helper([num2 | current_list], num1, num2 - 1)
|
||||
end
|
||||
|
||||
def positive(list) do
|
||||
Enum.reverse(positive_helper([], list))
|
||||
end
|
||||
|
||||
defp positive_helper(current_list, []), do: current_list
|
||||
|
||||
defp positive_helper(current_list, [head | tail]) when head > 0 do
|
||||
positive_helper([head | current_list], tail)
|
||||
end
|
||||
|
||||
defp positive_helper(current_list, [_ | tail]) do
|
||||
positive_helper(current_list, tail)
|
||||
end
|
||||
end
|
||||
7
chapter3/sum_list.ex
Normal file
7
chapter3/sum_list.ex
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
defmodule ListHelper do
|
||||
def sum([]), do: 0
|
||||
|
||||
def sum([head | tail]) do
|
||||
head + sum(tail)
|
||||
end
|
||||
end
|
||||
16
chapter3/sum_list_tc.ex
Normal file
16
chapter3/sum_list_tc.ex
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
defmodule ListHelper do
|
||||
def sum(list) do
|
||||
do_sum(0, list)
|
||||
end
|
||||
|
||||
defp do_sum(current_sum, []) do
|
||||
current_sum
|
||||
end
|
||||
|
||||
defp do_sum(current_sum, [head | tail]) do
|
||||
# More concise implementation
|
||||
# do_sum(current_sum + head, tail)
|
||||
new_sum = head + current_sum
|
||||
do_sum(new_sum, tail)
|
||||
end
|
||||
end
|
||||
40
chapter3/user_extraction.ex
Normal file
40
chapter3/user_extraction.ex
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
defmodule UserData do
|
||||
defp extract_login(%{"login" => login}), do: {:ok, login}
|
||||
defp extract_login(_), do: {:error, "login missing"}
|
||||
|
||||
defp extract_password(%{"password" => password}), do: {:ok, password}
|
||||
defp extract_password(_), do: {:error, "password mising"}
|
||||
|
||||
defp extract_email(%{"email" => email}), do: {:ok, email}
|
||||
defp extract_email(_), do: {:error, "email missing"}
|
||||
|
||||
def extract_user_case(user) do
|
||||
case extract_login(user) do
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
|
||||
{:ok, login} ->
|
||||
case extract_email(user) do
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
|
||||
{:ok, email} ->
|
||||
case extract_password(user) do
|
||||
{:error, reason} ->
|
||||
{:error, reason}
|
||||
|
||||
{:ok, password} ->
|
||||
%{login: login, email: email, password: password}
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def extract_user_with(user) do
|
||||
with {:ok, login} <- extract_login(user),
|
||||
{:ok, email} <- extract_email(user),
|
||||
{:ok, password} <- extract_password(user) do
|
||||
{:ok, %{login: login, email: email, password: password}}
|
||||
end
|
||||
end
|
||||
end
|
||||
11
chapter3/user_extraction_2.ex
Normal file
11
chapter3/user_extraction_2.ex
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
defmodule UserExtraction do
|
||||
def extract(user) do
|
||||
case Enum.filter(["login", "email", "password"], &(not Map.has_key?(user, &1))) do
|
||||
[] ->
|
||||
{:ok, %{login: user["login"], email: user["email"], password: user["password"]}}
|
||||
|
||||
missing_fields ->
|
||||
{:error, "missing fields: #{Enum.join(missing_fields, ", ")}"}
|
||||
end
|
||||
end
|
||||
end
|
||||
Loading…
Add table
Add a link
Reference in a new issue