Skip to content

Instantly share code, notes, and snippets.

View henrik's full-sized avatar

Henrik Nyh henrik

View GitHub Profile
@henrik
henrik / sky_tp_link.md
Created April 6, 2016 07:15
"Self-assigned IP" error with TP-link and Sky on OS X. #googlefood
# 238 chars (newlines count as one char)
c=&" #{&1} bottle#{&1>1&&"s"||""} of Elixir"
o=" on the wall"
for n<-99..1,do: Task.await Task.async fn->IO.puts [inspect(self),c.(n),o,?,,c.(n),".
",n<2&&"Go code some more,"<>c.(99)||"Take one down and pass it around,"<>c.(n-1),o,?.]end
@henrik
henrik / _README.md
Created March 3, 2016 22:57
"Send on Facebook Messenger" command for Alfred.app.

Send on Messenger

Usage

Assumes you have node.js and npm.

Once, on install

Create a file ~/.facebook-auth containing your login email and password in the format [email protected]:my-password.

@henrik
henrik / danish_organization_number.rb
Last active May 15, 2020 07:26
Validate Danish organization numbers (CVR). Uses https://github.com/barsoom/attr_extras.
# Danish "CVR-nummer".
#
# A 7 digit serial number followed by a mod-11 check digit:
# https://erhvervsstyrelsen.dk/modulus-11-kontrol
#
# Search for real CVRs, to see examples: https://datacvr.virk.dk/data/
# E.g. 35408002, 30715063.
class DanishOrganizationNumber
MOD_11_WEIGHTS = [ 2, 7, 6, 5, 4, 3, 2 ]
@henrik
henrik / danish_identity_number.rb
Last active February 2, 2016 10:23
Simple Ruby code to validate Danish identity numbers (CPR-nummer/personnummer). Uses https://github.com/barsoom/attr_extras.
# Danish "CPR-nummer" or "personnummer".
#
# http://en.wikipedia.org/wiki/Personal_identification_number_(Denmark)
# https://sv.wikipedia.org/wiki/Personnummer#Danmark
#
# > It is a ten-digit number with the format DDMMYY-SSSS, where DDMMYY is the date of birth and SSSS is a sequence number. The first digit of the sequence number encodes the century of birth (so that centenarians are distinguished from infants), and the last digit of the sequence number is odd for males and even for females.
# >
# > … since October 2007 personal identification numbers do not always validate using the check digit.
class DanishIdentityNumber
defmodule Example do
def unquote(:"foo bar")() do
IO.puts "hi!"
end
def unquote(:"täst")() do
IO.puts "hä!"
end
end
@henrik
henrik / a_macro_from_slack.exs
Last active January 10, 2016 18:46
As discussed on Slack.
defmodule MyMacro do
defmacro a(value, do_block) do
[{:a, value}|parse_do_block(do_block)]
end
defp parse_do_block([do: {:__block__, _, list}]) do
list |> Enum.map(&parse_name_and_value/1)
end
defp parse_name_and_value({name, _, [value]}) do
@henrik
henrik / say_when_real_guards.exs
Created December 31, 2015 16:43
Example of using real guard clauses in your own code with Elixir macros. Inspired by `plug :foo when action in [:create, :update]`. Also see https://gist.github.com/henrik/d21251bcf5d569e16ca9 for "fake" guard clauses that allow any condition/function to be used.
defmodule Lab do
defmacro say({:when, _, [message, condition]}) do
{result, _} = Code.eval_quoted(quote do
case true do
true when unquote(condition) -> true
true -> false
end
end)
if result do
@henrik
henrik / say_when.exs
Last active September 7, 2016 09:03
Example of using "guard clauses" in your own code with Elixir macros. Inspired by `plug :foo when action in [:create, :update]`. This implementation accepts any types of conditions, not just the subset allowed in actual guard clauses. But also see https://gist.github.com/henrik/68f136f9916165e0defb for an implementation with real guard clauses.
defmodule Lab do
defmacro say({:when, _, [message, condition]}) do
{result, _} = Code.eval_quoted(condition)
if result do
quote do
IO.puts unquote(message)
end
end
end
@henrik
henrik / dnloader.rb
Last active December 19, 2015 23:35
Dagens Nyheter (DN) PDF downloader for paying subscribers.
# Ruby script for DN subscribers to download the latest DN as a PDF – because it beats their iPad app.
# By Henrik Nyh 2015-12-08 under the MIT license.
#
# INSTRUCTIONS (for advanced users)
#
# Fetch the script dependencies:
#
# (sudo) gem install mechanize
#
# Put your username and password in a ~/.dnloader file separarated by a ":", e.g.: