Skip to content

Instantly share code, notes, and snippets.

View sashaafm's full-sized avatar

Sasha Fonseca sashaafm

View GitHub Profile
@sashaafm
sashaafm / erlang_formats.erl
Created October 4, 2016 13:07
Example on how to get all Erlang formats from source code to disassembled bytecode
% Example on how to get all Erlang formats from source code to disassembled bytecode
% Erlang Source --> Erlang AST --> Erlang Expanded AST --> Core Erlang --> BEAM Bytecode
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%%% First the original module
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
-module(add).
-export([add/2]).
@sashaafm
sashaafm / bfs.ex
Created February 20, 2016 16:47
breadth-first search Elixir
@doc """
Performs a Breadth-First Search in the given 'tree'. The nodes' values are
returned as a list.
"""
@spec breadth_first_search(%{}) :: list(any)
def breadth_first_search(tree) do
bfs(tree)
end
@sashaafm
sashaafm / dfs.ex
Created February 20, 2016 16:38
edpth-first search Elixir
@doc """
Does a Depth-First Search in the given 'tree'. The nodes' values are
returned in a list. The order of the search is passed into 'order' using
the atoms ':in_order', ':pre_order' or ':post_order'
"""
@spec depth_first_search(%{}, atom) :: list(any)
def depth_first_search(tree, order) when order == :pre_order or
order == :in_order or
order == :post_order do
@sashaafm
sashaafm / delete.ex
Last active April 16, 2017 21:27
delete BST
@doc """
Removes a node with 'node_value' from the given 'tree'. Returns :leaf if the
node does not exist.
"""
@spec delete_node(%{}, any) :: %{} | nil
def delete_node(tree, node_value) do
if exists?(tree, node_value) do
delete tree, node_value
else
@sashaafm
sashaafm / insert.ex
Created February 20, 2016 15:55
insert bst
def new(value) do
%{value: value, left: :leaf, right: :leaf}
end
@doc """
Creates and inserts a node with its value as 'node_value' into the tree.
"""
@spec insert(%{} | :leaf, any) :: %{}
def insert(:leaf, node_value), do: new node_value
@sashaafm
sashaafm / tests.ex
Created February 17, 2016 13:16
tests
setup %{conn: conn} = config do
case config[:login_as] do
:client ->
user = insert_user username: config[:username]
conn = assign conn, :current_user, user
{:ok, conn: conn, user: user}
:admin ->
admin = insert_admin username: config[:username]
conn = assign conn, :current_user, admin
@sashaafm
sashaafm / iep.ex
Last active February 9, 2016 16:17
def increase_element_priority([], _, _), do: []
def increase_element_priority(queue, {item, prio} = elem, new_prio)
when new_prio >= prio do
pos = position_by_order queue, elem
first_half = (queue |> Enum.take(pos)) -- [elem]
sec_half = queue |> Enum.slice(pos, length(queue))
first_half ++ [{item, new_prio}] ++ sec_half
end
@sashaafm
sashaafm / front.ex
Last active February 9, 2016 15:42
@spec front(list(tuple())) :: tuple() | nil
def front([]), do: nil
def front(queue) do
{result, _} = gfe queue
result
end
@spec insert_with_priority(list(tuple()), tuple()) :: list(tuple())
def insert_with_priority(queue, {_item, _prio} = elem) do
queue ++ [elem]
end
@sashaafm
sashaafm / gfe.ex
Last active February 9, 2016 15:29
gfe
defp gfe([]), do: nil
defp gfe(queue) do
{_, front_elem} = queue
|> Enum.group_by(fn {_, prio} -> prio end)
|> Map.to_list
|> List.last
{List.last(front_elem), queue -- [List.last(front_elem)]}
end