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 / functions.ex
Last active February 27, 2017 10:36
Mix Tasks
defmodule Mix.Tasks.MyTasks.Functions do
use Mix.Task
def run([module_name]) do
module = Module.concat Elixir,module_name
data = apply(module, :__info__, [:functions])
IO.inspect data
end
end
@blackode
blackode / my_sigils.ex
Last active February 22, 2017 16:56
Custom Sigils
defmodule MySigils do
@moduledoc ~S"""
Genrating the custom sigils.
"""
@doc ~S"""
This converts the given strings in to the path by joining each string with /.
If you provide an option `u` it will treat the first string as domain and prepend
that string with https://www. and add the rest of strings as path.
## Examples
defmodule WhileLoop do
defmacro while(condition_expression, do: do_block) do
quote do
try do
for _ <- Stream.cycle([:fine]) do
if unquote(condition_expression) do
unquote(do_block)
else
throw :break
end
defmodule WhileLoop do
defmacro while(condition_expression, do: do_block) do
quote do
for _ <- Stream.cycle([:fine]) do
if unquote(condition_expression) do
unquote(do_block)
else
# break out of loop
end
end
@blackode
blackode / while.exs
Last active February 19, 2017 20:57
Defiinig the macros and Testing
defmodule WhileLoop do
defmacro while(condition_expression, do: do_block) do
quote do
try do
for _ <- Stream.cycle([:fine]) do
if(unquote(condition_expression)) do
unquote do_block
else
WhileLoop.break
end
@blackode
blackode / protocol.md
Created February 17, 2017 04:55
Defining Elixir Protocols

#Define a Protocol

A Protocol is a way to dispatch to a particular implementation of a function based on the type of the parameter.
The macros defprotocol and defimpl are used to define Protocols and Protocol implementations for different types in the following example.

defprotocol Triple do    
  def triple(input)  
end  

defimpl Triple, for: Integer do 
@blackode
blackode / nested_maps.md
Last active March 10, 2020 20:48
Nested Maps in Elixir

#Get a value from nested maps

The get_in function can be used to retrieve a nested value in nested maps using a list of keys.

nested_map = %{ name: %{ first_name: "blackode"} }     #Example of Nested Map
first_name = get_in(nested_map, [:name, :first_name])  # Retrieving the Key

# Returns nil for missing value 
nil = get_in(nested, [:name, :last_name])              # returns nil when key is not present
@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
@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 / .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 ],
],