This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) | |
# |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env ruby | |
## | |
# Helpers | |
## | |
module Helper | |
module_function | |
def display_start(count:) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# Simple "if" statement | |
## | |
if [ "foo" == "foo" ]; then | |
echo "I'm in! (1)" | |
fi | |
# Short version | |
[ "foo" = "foo" ] && echo "I'm in! (2)" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
## | |
# 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)> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |