View zalgo.ex
defmodule Zalgo do
def this(string) do
String.graphemes(string)
|> Enum.map(fn (char) -> char <> the_funk end)
|> Enum.join
end
def the_funk do
accent_codes
|> Enum.map(fn (hex) ->
View experiment.md

(Update - thanks to Chris for looking at this and not saying I'm crazy. :))

"Metaprogramming Elixir" (Chris McCord) talks about how String.Unicode reads a text file of unicode characters at compile time and defines a separate function head for each character we might want to upcase. It says this leans on the Erlang VM's pattern matching prowess and implies (I think) that it's more performant than it would be to create a lower -> upper map at compile time and consult it at runtime.

Similarly, McCord in advocates this approach for some example code that looks up I18n keys.

By generating function heads for each translation mapping, we again let the Virtual Machine take over for fast lookup.

Although defining multiple function heads is idiomatic Elixir, this seemed odd to me. I've heard that the Erlang VM is really fast at pattern matching and hence at finding the right function f

View activerecord_model_scopes.rb
# Scopes that make use of the index
def self.within_distance_of(lat:, lng:, meters:)
where(%{
ST_DWithin(
ST_GeographyFromText(
'SRID=4326;POINT(' || #{table_name}.longitude || ' ' || #{table_name}.latitude || ')'
),
ST_GeographyFromText('SRID=4326;POINT(%f %f)'),
%d
)
View .vimrc
" <cr> should not only clear highlighted search, but flash the current
" cursor location.
:nnoremap <CR> :nohlsearch<CR>:set cul cuc<cr>:sleep 50m<cr>:set nocul nocuc<cr>/<BS>
View stupid_t.rb
# config/initializers/stupid_t.rb
# Any text that doesn't appear as "localized" isn't localized
module ActionView::Helpers::TranslationHelper
def t(*args)
result = I18n.t!(*args)
if result.respond_to?(:map)
result.map { |r| "localized" }
else
"localized"
end
View exponential_backoff.rb
# Exponential backoff in Ruby
begin
make_request
rescue RequestError => e
if retries <= max_retries
retries += 1
sleep 2 ** retries
retry
else
raise "Timeout: #{e.message}"
View traveling_salesman_brute_force_time.rb
def factorial(n)
(1..n).reduce(:*)
end
SECONDS_PER_STEP = 0.001
def total_seconds(n)
factorial(n) * SECONDS_PER_STEP
end
View test_pathfinding.rb
# This can be run from the voltron directory while the NetSkycanner has a couple of `rake pathfinder` processes running
require 'bundler/setup'
Bundler.require :default, :pathfinder
require 'json'
require 'thump'
require 'mr_sulu_messages'
test = "searching"
View p.rb
def p(*args)
location = caller_locations(1,1)[0]
location_string = "#{location.path.split('/').last}:#{location.lineno}(#{location.label})"
super([*args, location_string])
end
View ruby_log_formatting.rb
require 'logger'
l = Logger.new(STDOUT)
l.formatter = proc { |severity, datetime, progname, msg|
dt = datetime.strftime('%Y-%b-%d@%H:%M:%S:%z')
"#{[severity,dt,progname,msg].join(' ').squeeze(' ')}\n"
}
l.info "woot" #=> INFO 2014-Feb-11@09:48:32:-0500 woot