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 / 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 / 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 / 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
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
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
@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
@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 / functions2.ex
Last active February 27, 2017 11:04
task with short docs
defmodule Mix.Tasks.MyTasks.Functions do
use Mix.Task
@shortdoc "Returns functions in Module"
@moduledoc ~S"""
This is used to list out the all available functions in the module.
You suppose to pass module_name as parameter to the task.
#Usage
```
@blackode
blackode / hello.ex
Created February 27, 2017 09:49
Module Demo files
defmodule Hello do
def hello1 do
IO.puts "hello1"
end
def hello2 do
IO.puts "hello2"
end
@blackode
blackode / hi.ex
Created February 27, 2017 09:52
demo mdoule
defmodule Hi do
def hi1 do
IO.puts "hi1"
end
def hi2 do
IO.puts "hi2"
end