Skip to content

Instantly share code, notes, and snippets.

Avatar

Thiago Araújo Silva thiagoa

  • thoughtbot
  • Natal / RN - Brazil
View GitHub Profile
View immutableTransitiveDependencies.js
const test = require('tape');
function resolver(deps) {
function directDeps(className) {
return deps[className] || [];
}
function transitiveDeps(className) {
return directDeps(className).reduce((set, className) => {
[className, ...transitiveDeps(className)].forEach((v) => set.add(v));
View collatz.js
const MAX = 1000000;
const cache = new Array(MAX);
function nextFor(n) {
if (n == 1) {
return 1;
}
else if (n <= MAX && cache[n - 1] !== undefined) {
return cache[n - 1];
}
View collatz.cr
ZERO = Int64.new(0)
ONE = Int64.new(1)
TWO = Int64.new(2)
THREE = Int64.new(3)
ONE_MILLION = Int64.new(1_000_000)
class Collatz
def initialize(min : Int64, max : Int64)
@min = min
@max = max
View collatz_elixir.ex
defmodule Collatz do
def call(n, cache), do: call(n, 0, cache)
def call(1, length, _), do: length + 1
def call(n, length, cache), do: get(n, length, cache, cache[n])
defp get(n, length, cache, nil), do: call(next(n), length, cache) + 1
defp get(_, _, _, value), do: value
defp next(n) when rem(n, 2) == 0, do: div(n, 2)
defp next(n) when rem(n, 2) == 1, do: n * 3 + 1
@thiagoa
thiagoa / elixir_bubble_sort.ex
Last active Jun 9, 2017
Naive and Effective Bubble Sort in Elixir
View elixir_bubble_sort.ex
defmodule NaiveBubbleSort do
def call([]), do: []
def call(list), do: call(list, 0, length(list) - 1, true)
def call(list, idx, last_idx, false) when last_idx == idx, do: list |> call
def call(list, idx, last_idx, true) when last_idx == idx, do: list
def call(list, idx, last_idx, done) do
{a, b} = values_to_compare(list, idx)
{list, done} = process(list, idx, a, b, done)
View mutable_priority_queue.rb
require 'minitest/autorun'
class Item
include Comparable
def initialize(item, priority:)
@item = item
@priority = priority
end
View elixir_logger.ex
defmodule FunctionLogger do
defmacro __using__(_) do
quote do
import Kernel, except: [def: 2]
import FunctionLogger
end
end
defmacro def(fun_def, do: block) do
quote do
View hstruct.rb
class HStruct
def self.new(*members, defaults: {}, &block)
klass = Class.new do
@members = members
@defaults = defaults
class << self
attr_reader :members, :defaults
end