Skip to content

Instantly share code, notes, and snippets.

@unix1
unix1 / cloudformation-fastmail-mx-spf-dkim.yml
Created June 14, 2021 14:09
Cloudformation template for fastmail MX/SPF/DKIM records described on https://www.fastmail.com/help/receive/domains-setup-mxonly.html
Parameters:
Domain:
Description: Domain for hosted zone, e.g. example.com
Type: String
Resources:
WebHostedZone:
Type: AWS::Route53::HostedZone
@unix1
unix1 / rle.lisp
Last active February 26, 2016 14:57
Implementation of Run-length encoding algorithm in LISP. Both encode and decode functions are provided. For more info see https://en.wikipedia.org/wiki/Run-length_encoding and http://rosettacode.org/wiki/Run-length_encoding - the latter has the loop implementation, but I like the tail call optimized recursive implementation better (but I'm still…
(defun rle-encode (lst)
(reverse (rle-lib-encode lst nil)))
(defun rle-lib-encode (lst acc)
(if (null lst)
acc
(let ((curr (car lst))
(prev (car acc))
(cnt (car (cdr acc))))
(if (equal curr prev)
@unix1
unix1 / rle.erl
Created February 25, 2016 06:23
Implementation of Run-length encoding algorithm in Erlang. Both encode and decode functions are provided. For more info see https://en.wikipedia.org/wiki/Run-length_encoding and http://rosettacode.org/wiki/Run-length_encoding - the latter has a couple of implementations in Erlang, but I like using lists:foldr/3 this way.
-module(rle).
-export([encode/1]).
-export([decode/1]).
encode(List) ->
lists:flatten(lists:foldr(
fun (Prev, [[Count, Prev]|Rest]) ->
[[Count + 1, Prev]|Rest];
(Current, Acc) ->
@unix1
unix1 / perftest.erl
Created August 10, 2014 16:44
Measure average execution time of a function. I don't claim any credit, I took http://erlangcentral.org/wiki/index.php/Measuring_Function_Execution_Time and put it in a module for convenience.
-module(perftest).
-export([test_avg/4]).
test_avg(M, F, A, N) when N > 0 ->
L = test_loop(M, F, A, N, []),
Length = length(L),
Min = lists:min(L),
Max = lists:max(L),
Med = lists:nth(round((Length / 2)), lists:sort(L)),
@unix1
unix1 / binary2.erl
Last active August 29, 2015 14:04
Erlang stdlib provides string:join/2 function but not binary:join/2. Below are few options how to do binary:join/2. Guess which one is the fastest.
-module(binary2).
-export([joinl/2]).
-export([joinfl/2]).
-export([joinfr/2]).
% use list comprehension
joinl([], Sep) when is_binary(Sep) ->
<<>>;
joinl([H|T], Sep) ->
@unix1
unix1 / test-matrix-invert.php
Last active September 18, 2022 13:09
A sample matrix inversion in PHP using Gauss-Jordan elimination. This is implementation is meant for clarity, not for performance, memory usage, or numerical stability. You can optionally turn on the debug flag to output matrix state after every iteration. See https://en.wikipedia.org/wiki/Gauss-Jordan_elimination for more info. Enjoy!
<?php
/**
* Inverts a given matrix
*
* @param array $A matrix to invert
* @param boolean $debug whether to print out debug info
*
* @return array inverted matrix
*/
@unix1
unix1 / test-peb-session.php
Last active December 18, 2015 18:59
Sample page to illustrate and test PHP session storage in Erlang Mnesia via mypeb PHP extension and kvstore application. Full instructions are here: https://unix0.wordpress.com/2013/06/22/php-sessions-in-erlang-mnesia/
<html>
<body>
<h1>Test Peb Page</h1>
<p>This page starts a session and keeps a counter in a session variable.</p>
<p>It uses kvstore application to store PHP sessions in Erlang Mnesia.</p>
<div><a href="test-peb-session.php">Reload this page</a> to increment the counter.</div>
<?php
interface SessionHandlerInterface {}