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 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 |
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 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 |
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 DatabaseServer do | |
| def start do | |
| spawn(fn -> | |
| connection = :rand.uniform(1000) | |
| loop(connection) | |
| end) | |
| end | |
| def run_async(server_pid, query_def) 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
| 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) |
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} | |
| def value(%Fraction{a: a, b: b}) do | |
| a / b | |
| end | |
| def add(%Fraction{a: a1, b: b1}, %Fraction{a: a2, b: b2}) 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
| # 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 |
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 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 |
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
| # 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: |
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
| # 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 |
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
| # 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"} |