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
sdfsdfsdfs |
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
sfsd | |
sfds |
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 PrimeCalculator do | |
@moduledoc """ | |
Returns all prime numbers within a range | |
""" | |
import Enum, only: [sort: 1, to_list: 1, filter: 2] | |
import Map, only: [keys: 1, put: 3] | |
import Float, only: [ceil: 1] | |
import :math, only: [sqrt: 1] |
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
# This works for the use case below, but not for every timeout-related task. | |
defmodule Timeout do | |
@default_timeout 1_000 | |
@count 0 | |
def wait(cond, on_after, timeout \\ @default_timeout) do | |
result = call time(), @count, timeout, cond, cond.() | |
on_after.(result) | |
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
defmodule Hub do | |
defmacro on_match(pattern, do: on_match, after: on_after) do | |
on_match = quote do | |
term -> Hub.match term, unquote(pattern), do: unquote(on_match) | |
end | |
quote do | |
receive(do: unquote(on_match), after: unquote(on_after)) | |
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
defmodule Hub do | |
use GenServer | |
def start_link(options \\ []) do | |
GenServer.start_link(__MODULE__, :ok, options) | |
end | |
def init(:ok) do | |
{:ok, []} | |
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
defmodule TermEvaluator do | |
def call(term, pattern: pattern) do | |
term = Macro.escape(term) | |
ast = quote do | |
case unquote(term) do | |
unquote(pattern) -> true | |
_ -> false | |
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
require 'benchmark/ips' | |
require 'active_support/core_ext/hash' | |
def sample | |
{ a: 1, b: 2, c: 3, d: 4, c: 5, d: 6, e: 7, f: 8, g: 9, h: 10 } | |
end | |
Benchmark.ips do |x| | |
x.report 'map + to_h' do | |
h = sample |
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
module Imageable | |
def self.included(model_class) | |
model_class.has_many :images, polymorphic: true | |
model_class.extend ClassMethods | |
end | |
module ClassMethods | |
def has_one_image(*names) | |
names.each do |image_kind| | |
define_method image_kind do |
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
module Imageable | |
def self.build(methods) | |
mod = Module.new do | |
methods.each do |method| | |
define_method method do | |
images.find { |image| image.kind == 'main_image' } | |
end | |
end | |
end |