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 / 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 / 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 / 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 / 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 / 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 / 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 / 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 / .iex.exs
Created February 17, 2017 00:52
iex> Customization
# IEx.configure colors: [enabled: true]
# IEx.configure colors: [ eval_result: [ :cyan, :bright ] ]
IO.puts IO.ANSI.red_background() <> IO.ANSI.white() <> " ❄❄❄ Good Luck with Elixir ❄❄❄ " <> IO.ANSI.reset
Application.put_env(:elixir, :ansi_enabled, true)
IEx.configure(
colors: [
eval_result: [:green, :bright] ,
eval_error: [[:red,:bright,"Bug Bug ..!!"]],
eval_info: [:yellow, :bright ],
],
@blackode
blackode / my_sigils.md
Created February 17, 2017 02:00
Custom Sigils

Custom Sigils

defmodule MySigils do
  #returns the downcasing string if option l is given then returns the list of downcase letters
  def sigil_l(string,[]), do: String.Casing.downcase(string)
  def sigil_l(string,[?l]), do: String.Casing.downcase(string) |> String.graphemes
  
 #returns the upcasing string if option l is given then returns the list of downcase letters
@blackode
blackode / custom_error.ex
Created February 17, 2017 02:15
Custom Errors
defmodule BugError do
defexception message: "BUG BUG .." # message is the default
end