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 TupleAndListTest do | |
@moduledoc false | |
use ExUnit.Case | |
test "tuple cur" do | |
''' | |
Tuples are most appropriate to group a small, | |
fixed number of elements together. When you need a dynamically sized collection, you can use lists. | |
''' | |
#Bench mark for tuple and list https://aneta-bielska.github.io/blog/benchmarking-elixir-lists-and-tuples-example.html |
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.Demo how to transfor function | |
2.Demo how to construct function and call it | |
3.& capture operator will conver Module.Function/arity to lambda | |
4.& capture operation could be also used to shorten lambda | |
defmodule FunctionTransfor do | |
@moduledoc false | |
def display(msg), do: IO.puts msg | |
def add1 do | |
fn(x)-> x + 1 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
http://www.zsythink.net/archives/1182 |
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. How to distiguish the same object? | |
We use tuple which consist of type and ID | |
def init({db_folder, pool_size}) do | |
processes = for worker_id <- 1..pool_size do | |
worker( | |
Todo.DatabaseWorker, [db_folder, worker_id], | |
id: {:database_worker, worker_id} | |
) | |
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
Demo how to create supervised pool worker | |
defmodule SupPoolWorker do | |
@moduledoc false | |
use Supervisor | |
def start_link(name,number) do | |
Supervisor.start_link(SupPoolWorker,number,name: name) | |
end | |
def init(number) do | |
children = for i <- 1..number, do: worker(DBWorker,[String.to_atom("Worker#{i}")], id: String.to_atom("Worker#{i}")) |
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>Find the max number of a list | |
2>Remove some elements of a Map | |
3>Remove some elements of a List | |
4>Map/reduce | |
defmodule MyReduce do | |
@moduledoc false | |
def max_number_of_list(list) do | |
Enum.reduce list,List.first(list),fn(x,acc) -> | |
if(x > acc) 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
Requirements | |
1>Two kinds of GenServer | |
2>The GenServer will share same process registry module | |
3>The Registry module's is used to keep the name/pip mapping with the process, so the client will talk with the Registry directly | |
to get correspoding PID by name. The Registry should monitor the process and keep the name/pid map up-to-date all the time. | |
How the following work? | |
1>The My.Registry must implement register_name/unregister_name/whereis_name/send | |
2>The client start_link function must use name: {:via, Registry, name} since the Registry will keep different kind of process, | |
we usually use tuple {:server,name} to distinguish |
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 Bucket do | |
use GenServer | |
def start_link(name) do | |
GenServer.start_link(__MODULE__, %{}, name: name) | |
end | |
#callbacks | |
def handle_call(request, from, state) do | |
case request do |
NewerOlder