Skip to content

Instantly share code, notes, and snippets.

View thiagoa's full-sized avatar

Thiago Araújo Silva thiagoa

  • thoughtbot
  • Natal / RN - Brazil
View GitHub Profile
module Coercible
module Types
BOOL_MAP = { 'true' => true, 'false' => false, true => true, false => false }
BOOL = proc { |v| BOOL_MAP[v] }
INTEGER = proc { |v| v.to_i }
ARRAY = proc { |v, rule| Array(v).map(&ARRAY_MAP[rule[:of]]) }
ARRAY_MAP = { Integer => :to_i, String => :to_s, nil => proc { |v| v } }
module_function
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));
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));
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
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];
}
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 June 9, 2017 01:55
Naive and Effective Bubble Sort in Elixir
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)
require 'minitest/autorun'
class Item
include Comparable
def initialize(item, priority:)
@item = item
@priority = priority
end
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
class HStruct
def self.new(*members, defaults: {}, &block)
klass = Class.new do
@members = members
@defaults = defaults
class << self
attr_reader :members, :defaults
end