This file contains hidden or 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 BinarySearch do | |
| def find(item, _left, [i | _right]) when i == item, do: true | |
| def find(item, _left, [i | right]) when i < item, do: find(item, right) | |
| def find(item, left, [i | _right]) when i > item, do: find(item, left) | |
| def find(_item, _left, []), do: false | |
| def find(item, list) do | |
| {left, right} = split(list) | |
| find(item, left, right) | |
| end |
This file contains hidden or 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
| ;; Align command !!! | |
| ;; from http://stackoverflow.com/questions/3633120/emacs-hotkey-to-align-equal-signs | |
| ;; another information: https://gist.github.com/700416 | |
| ;; use rx function http://www.emacswiki.org/emacs/rx | |
| (defun align-to-colon (begin end) | |
| "Align region to colon (:) signs" | |
| (interactive "r") |
This file contains hidden or 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 HStruct | |
| def self.new(*members, defaults: {}, &block) | |
| invalid_defaults = defaults.keys - members | |
| fail "Invalid defaults #{invalid_defaults}" if invalid_defaults.any? | |
| klass = Class.new do | |
| @members = members | |
| @defaults = defaults | |
| class << self |
This file contains hidden or 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
| 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 |
This file contains hidden or 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
| 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)); |
This file contains hidden or 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
| 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)); |
This file contains hidden or 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
| 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]; | |
| } |
This file contains hidden or 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
| 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 |
This file contains hidden or 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 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 |
This file contains hidden or 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 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) |