Skip to content

Instantly share code, notes, and snippets.

@ray-sh
ray-sh / gist:50c0aa9b434e79cbd6c9bea490ed6065
Last active July 22, 2017 14:52
GenServe Supervisor study
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
@ray-sh
ray-sh / Registry.ex
Last active July 19, 2017 14:48
Elixir process register
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
@ray-sh
ray-sh / MyReduce.ex
Last active July 22, 2017 14:47
Demo how to use Enum.reduce in elixir
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
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}"))
@ray-sh
ray-sh / module_world.ex
Created July 23, 2017 08:40
How module the world with elixir
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
@ray-sh
ray-sh / learn_hash.ex
Created July 23, 2017 09:00
Use hash to reslove the real problem
http://www.zsythink.net/archives/1182
@ray-sh
ray-sh / function_transfor.ex
Created July 26, 2017 14:02
Transfor function between different and run
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
@ray-sh
ray-sh / tuple_list.ex
Created August 1, 2017 14:24
tuple and list
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
@ray-sh
ray-sh / auth.ex
Last active September 30, 2017 02:17
Auth functions
#Function used in auth
#1>Save the user id in session and current_user in conn.assign which will exposed as @current_user for all template
#Deliver pass port to user, user will carry the :user_id in next request
def deliver_passport_to_user(conn,user) do
conn
|>assign(:current_user, user)
|>put_session(:user_id,user.id)
|>configure_session(renew: true) #Renew the session to keep our safe
end
@ray-sh
ray-sh / relation.ex
Created October 5, 2017 14:09
Build relation CURD
#Build relation is very common in DB layer, like user and it's posts
#1> We need define the relation between Module user and posts
schema "users" do
has_many :posts, Blog.Post
end
schema "posts" do
belongs_to(:user, Blog.User)
end