Skip to content

Instantly share code, notes, and snippets.

View tiagopog's full-sized avatar

Tiago Guedes tiagopog

View GitHub Profile
@tiagopog
tiagopog / 20180711220702_create_foos.rb
Last active July 12, 2018 04:14
ActiveRecord - Outside Rails (microservice example)
class CreateFoo < ActiveRecord::Migration[5.0]
def change
create_table(:foos, force: true) do |t|
t.text :message, null: false
t.datetime :created_at, null: false
end
end
end
@tiagopog
tiagopog / fibonacci.ex
Last active July 11, 2018 13:20
Elixir - Body-recursion vs Tail-Recursion for Fibonacci
# Body-recursive
#
# Note: "+" is actually the last call, not calculate/1.
defmodule Fibonacci do
def calculate(x) when x <= 2, do: 1
def calculate(x), do: calculate(x - 1) + calculate(x - 2)
end
# Tail-recursive (Tail Call Optimization)
#
@tiagopog
tiagopog / Dockerfile.build
Last active April 3, 2018 11:55
Elixir - Deploy with mix_docker, Distillery, Docker and k8s
FROM elixir:1.4.2
ENV DEBIAN_FRONTEND=noninteractive
ENV HOME=/opt/app/ TERM=xterm
# Install Hex+Rebar
RUN mix local.hex --force && \
mix local.rebar --force
WORKDIR /opt/app
@tiagopog
tiagopog / resource.ex
Created March 21, 2018 13:27
Elixir - Define struct dynamically at compile-time.
defmodule Iugu.Resource do
@moduledoc """
Mixin module to easily include all the common functionality across the resources.
"""
defmacro __using__([name: resource, actions: actions]) do
quote do
@derive [Poison.Encoder]
@resource unquote(resource)
defstruct unquote(fields(resource))
@tiagopog
tiagopog / rubocop-diff
Last active March 8, 2018 21:46
Apply Rubocop linter but only on git diffs
#!/usr/bin/env ruby
##
# Helpers
##
module Helper
module_function
def display_start(count:)
##
# Simple "if" statement
##
if [ "foo" == "foo" ]; then
echo "I'm in! (1)"
fi
# Short version
[ "foo" = "foo" ] && echo "I'm in! (2)"
@tiagopog
tiagopog / Dockerfile
Last active January 26, 2018 12:49
Elixir/Phoenix - Dockerfiles
FROM elixir:1.5.2
ENV PHOENIX_VERSION=1.3.0 \
APP_PATH=/app
RUN mkdir -p $APP_PATH
WORKDIR $APP_PATH
RUN mix local.hex
RUN yes | mix archive.install https://github.com/phoenixframework/archives/raw/master/phx_new-$PHOENIX_VERSION.ez
@tiagopog
tiagopog / genomic_range_query.rb
Created January 22, 2018 00:32
Algorithms - Prefix Sum
# 100% - O(N + M) - https://app.codility.com/demo/results/trainingFZS58R-ENQ/
def solution(s, p, q)
nucleotides = { "A" => 1, "C" => 2, "G" => 3, "T" => 4 }
occurrences = { "A" => [0], "C" => [0], "G" => [0], "T" => [0] }
1.upto(s.length).each do |index|
nucleotides.keys.each do |nucleotide|
previous = occurrences[nucleotide][index - 1]
occurrences[nucleotide][index] = s[index - 1] == nucleotide ? previous + 1 : previous
end
@tiagopog
tiagopog / curry.rb
Last active December 11, 2017 13:30
Ruby - Curry examples
##
# Simple closure
##
power = -> (x, n) { x**n }.curry #=> #<Proc:0x00556a913f6af0 (lambda)>
power_of_two = power.(2) #=> #<Proc:0x00556a913f2d38 (lambda)>
power_of_two.(10) #=> 1024
power_of_ten = power.(10) #=> #<Proc:0x00556a913e03b8 (lambda)>
@tiagopog
tiagopog / binary_search.rb
Last active December 10, 2017 04:38
Algorithms
def solution(x, array)
min = 0
max = array.size - 1
index = -1
begin
# avg = (min + max) / 2
# avg = min + (max - min) / 2
# avg = min + (max - min) >> 1
avg = (min & max) + ((min ^ max) >> 1)