Skip to content

Instantly share code, notes, and snippets.

View blackode's full-sized avatar
:octocat:
Pushed code so fast, even my coffee is still loading. ☕💻 #CodingFuel

Ankanna blackode

:octocat:
Pushed code so fast, even my coffee is still loading. ☕💻 #CodingFuel
View GitHub Profile
@blackode
blackode / multiple_or.exs
Last active September 20, 2017 04:40
Multiple Or Conditions
# 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
@blackode
blackode / 0_reuse_code.js
Created February 16, 2017 18:41
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@blackode
blackode / hello_server.ex
Last active August 6, 2018 01:03
GenServer Callbacks examples
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
@blackode
blackode / server_callbacks.exs
Last active November 2, 2017 20:57
GenServer in Elixir Server Callbacks
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
@blackode
blackode / init.exs
Created February 14, 2017 12:14
GenServer initiation
def init(password) do
{:ok, [password]} # ----------- state is stored as list of passwords
end
@blackode
blackode / sample_test.exs
Created February 14, 2017 10:42
Test sample code
test "unlock success test", %{server: pid} do
assert :ok == PasswordLock.unlock(pid,"foo")
end
@blackode
blackode / genserver_test_setup.exs
Created February 14, 2017 10:36
Testing GenServer Setup
setup do
{:ok,server_pid} = PasswordLock.start_link("foo")
{:ok,server: server_pid}
end
@blackode
blackode / unlock_reset.exs
Created February 14, 2017 10:34
unlock and reset definition
@doc """
Unlocks the given password
"""
def unlock(server_pid, password) do
GenServer.call(server_pid, {:unlock, password})
end
@doc """
resets the given password
"""
@blackode
blackode / start_link.exs
Created February 14, 2017 10:33
GenServer start_link definition
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