Skip to content

Instantly share code, notes, and snippets.

View micahboyd's full-sized avatar

Micah Boyd micahboyd

  • Qantas Hotels
  • Melbourne, AUS
View GitHub Profile
@micahboyd
micahboyd / state_practice_2.ex
Created April 29, 2018 23:24
Elixir State practice 2
defmodule TodoServer do
def start, do: spawn(fn -> loop(TodoList.new) end)
defp loop(todo_list) do
new_todo_list = receive do
message -> process_message(todo_list, message)
end
loop(new_todo_list)
end
@micahboyd
micahboyd / state_practice.ex
Last active April 23, 2018 07:06
Elixir State practice
defmodule Calculator do
def start, do: spawn(fn -> loop(0) end)
def value(server_pid) do
send(server_pid, {:value, self()})
receive do
{:response, value} -> value
end
end
@micahboyd
micahboyd / processes_practice_2.ex
Last active April 20, 2018 05:33
Elixir Processes practice 2
defmodule DatabaseServer do
def start do
spawn(fn ->
connection = :rand.uniform(1000)
loop(connection)
end)
end
def run_async(server_pid, query_def) do
@micahboyd
micahboyd / processes_practice.ex
Created April 20, 2018 04:47
Elixir Processes practice
run_query = fn(query_def) ->
:timer.sleep(2)
"#{query_def} result"
end
async_query = fn(query_def) ->
caller = self
spawn(fn ->
send(caller, {:query_result, run_query.(query_def)})
end)
@micahboyd
micahboyd / struct_practice.ex
Created April 20, 2018 01:28
Elixir Struct practice
defmodule Fraction do
defstruct a: nil, b: nil
def new(a,b), do: %Fraction{a: a, b: b}
def value(%Fraction{a: a, b: b}) do
a / b
end
def add(%Fraction{a: a1, b: b1}, %Fraction{a: a2, b: b2}) do
@micahboyd
micahboyd / stream_practice.ex
Created April 18, 2018 04:14
Elixir Stream practice
# lines_lengths!/1 that takes a file path and returns a list of numbers, with each number representing the lenght of the corresponding line from the file.
def lines_lenght(path) do
File.stream!(path)
|> Enum.map(&String.length/1)
end
# longest_line_length!/1 that returns the length of the longest line in a file.
def longest_line_length!(path) do
@micahboyd
micahboyd / iteration_practice.ex
Created April 17, 2018 02:12
Elixir iteration practice
defmodule Practice do
# list_len/1 function that calculates the lenght of a list
def list_len(list), do: length(0, list)
defp length(acc, []), do: acc
defp length(acc, [_ | tail]), do: acc + 1 |> length(tail)
# range/2 function that takes two integers: from and to and returns a list of all number in a given range
@micahboyd
micahboyd / ruby_closures_3.rb
Created April 13, 2018 04:36
Ruby Closures exercises 3
# 1. Reimplement Symbol#to_proc. Now that you’ve seen how Symbol#to_proc is implemented, you should have a go at it yourself.
class Symbol
def to_proc
proc { |x, args=nil| x.send(self, *args) }
end
end
# 2. You can use #to_proc instantiate classes. . Consider this behavior:
@micahboyd
micahboyd / ruby_closures_2.rb
Last active April 13, 2018 03:04
Ruby Closures exercises 2
# 1. Implement Array#map using Array#each:
class Array
def map
i = 0
[].tap do |arr|
while i < self.length
arr << yield(self[i])
i += 1
@micahboyd
micahboyd / ruby_closures.rb
Last active April 13, 2018 01:48
Ruby Closures exercises
# 3. You work in a music store and you’ve been tasked with writing a miniature
# database to store artists and album titles. The database should be able to
# insert, delete, and list entries, but you cannot use objects other than arrays
# and hashes. Only lambdas are allowed. Here’s the API:
# >> db = new_db.call
# >> db[:insert].call("Eagles", "Hell Freezes Over") => Hell Freezes Over
# >> db[:insert].call("Pink Floyd", "The Wall") => The Wall
# >> db[:dump].call => {"Eagles"=>"Hell Freezes Over", "Pink Floyd"=>"The Wall"}