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
# Regular Approach | |
find = fn(x) when x>10 or x<5 or x==7 -> x end | |
# Our Hack | |
hell = fn(x) when true in [x>10,x<5,x==7] -> x 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
// Use Gists to store code you would like to remember later on | |
console.log(window); // log the "window" object to the console |
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 HelloServer do | |
use GenServer | |
## Server API | |
def init(initial_value) do # initiating the state with the value 1 passed | |
{:ok,initial_value} | |
end | |
# add the value to the state and returns :ok | |
def handle_call({:add,value},_from,state) 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
def handle_call({:unlock, password}, _from, passwords) do # ----> synchronous request | |
if password in passwords do | |
{:reply, :ok, passwords} | |
else | |
write_to_logfile password | |
{:reply, {:error,"wrongpassword"}, passwords} | |
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
def init(password) do | |
{:ok, [password]} # ----------- state is stored as list of passwords | |
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
test "unlock success test", %{server: pid} do | |
assert :ok == PasswordLock.unlock(pid,"foo") | |
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
setup do | |
{:ok,server_pid} = PasswordLock.start_link("foo") | |
{:ok,server: server_pid} | |
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
@doc """ | |
Unlocks the given password | |
""" | |
def unlock(server_pid, password) do | |
GenServer.call(server_pid, {:unlock, password}) | |
end | |
@doc """ | |
resets the given password | |
""" |
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
@doc """ | |
Initiate with the given password . | |
""" | |
def start_link(password) do | |
GenServer.start_link(__MODULE__, password, []) | |
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 PasswordLockTest do | |
use ExUnit.Case | |
doctest PasswordLock | |
setup do | |
{:ok,server_pid} = PasswordLock.start_link("foo") | |
{:ok,server: server_pid} | |
end | |
test "unlock success test", %{server: pid} do |