Skip to content

Instantly share code, notes, and snippets.

@padde
padde / unicode_pattern_matching.exs
Created Jun 5, 2015
Elixir Unicode Pattern Matching
View unicode_pattern_matching.exs
iex> <<first, rest::binary>> = "☺ how dare you, unicode!"
"☺ how dare you, unicode!"
iex> <<first>>
<<226>>
iex> rest
<<152, 186, 32, 104, 111, 119, 32, 100, 97, 114, 101, 32, 121, 111, 117, 44, 32, 117, 110, 105, 99, 111, 100, 101, 33>>
@padde
padde / chained-comparison.rb
Created Apr 20, 2012
chained comparison in Ruby
View chained-comparison.rb
# Chained comparisons in Ruby
# inspired by http://coffeescript.org/#comparisons
# as well as http://refactormycode.com/codes/1284
[:<, :>, :<=, :>=].each do |operator|
[Float, Fixnum, Rational, Comparable].each do |klass|
klass.class_eval do
alias_method "_#{operator}", operator
define_method operator do |rhs|
send("_#{operator}", rhs) && rhs
@padde
padde / filter_matching.exs
Last active May 8, 2017
Filter a list by pattern matching in Elixir
View filter_matching.exs
defmodule MyEnum do
defmacro filter_matching(collection, pattern) do
quote do
Enum.filter unquote(collection), fn
unquote(pattern) -> true
_ -> false
end
end
end
end
@padde
padde / .gitignore
Created Dec 2, 2011
LaTeX .gitignore
View .gitignore
*.aux
*.glo
*.idx
*.log
*.toc
*.ist
*.acn
*.acr
*.alg
*.bbl
@padde
padde / gist:1216920
Created Sep 14, 2011
Rails Helper for Typekit integration
View gist:1216920
def typekit_include_tag apikey
javascript_include_tag("http://use.typekit.com/#{apikey}.js") +
javascript_tag("try{Typekit.load()}catch(e){}")
end
View letsencrypt-nginx-autorenew.sh
#!/usr/bin/env sh
# Automatically renew all installed letsencrypt certificates on this server
service nginx stop
/root/letsencrypt/letsencrypt-auto renew -nvv --standalone > /var/log/letsencrypt/renew.log 2>&1
RESULT=$?
service nginx start
# optional: notify yourself of succeeded/failed renewals
@padde
padde / mutate.exs
Created Mar 24, 2015
Stream.mutate/3
View mutate.exs
defmodule MyStream do
def mutate(enum, user_acc, user) do
step = fn val, _acc -> {:suspend, val} end
next = &Enumerable.reduce(enum, &1, step)
&do_mutate([], user_acc, user, next, &1, &2)
end
defp do_mutate(values, user_acc, user, next, {:suspend, acc}, fun) do
{:suspended, acc, &do_mutate(values, user_acc, user, next, &1, fun)}
end
View codepoints.exs
defmodule Codepoints do
defstruct str: ""
def new(str) when is_list(str) do
new(to_string(str))
end
def new(str) when is_binary(str) do
%__MODULE__{str: str}
end
@padde
padde / lpmap.exs
Last active Apr 10, 2016
Limited parallel map in Elixir
View lpmap.exs
defmodule Parallel do
def run(funs, max) do
tasks = for fun <- funs, do: {make_ref, fun}
results = do_run(tasks, max, 0, %{})
for {ref, _} <- tasks, do: results[ref]
end
defp do_run([], _max, 0, results) do
results
end
@padde
padde / shellscript.sh
Created Mar 18, 2016
Shell Script Template
View shellscript.sh
#!/usr/bin/env bash
set -euo pipefail
IFS=$'\n\t'