Skip to content

Instantly share code, notes, and snippets.

View colefichter's full-sized avatar
😄

Coach Cole colefichter

😄
View GitHub Profile
@colefichter
colefichter / SimpleMapper.cs
Created February 5, 2016 20:29
Simple property mapper
public class SimpleMapper<TDestination> where TDestination : class, new()
{
public SimpleMapper() { }
public virtual List<TDestination> CreateFrom<TSource>(List<TSource> source) where TSource : class
{
return source.ConvertAll<TDestination>(x => CreateFrom<TSource>(x));
}
public virtual TDestination CreateFrom<TSource>(TSource source) where TSource : class
@colefichter
colefichter / set.erl
Created July 23, 2015 18:00
An insert-only distributed set as a Conflict-Free Replicated Data Type (CRDT).
-module(set).
% An insert-only, distributed set that functions as a CRDT. By not allowing removal of elements in the set, we can
% provide Strong Eventual Consistency without a quorum algorithm. Requests can be served by any server without
% waiting for acknowledgment. Replication conflicts are impossible because all operations are commutative,
% associative, and idempotent.
% I think something like this should provide consistency even with up to N-1 failures. (Is this correct?)
% Note that this is very similar to the multi-master counter, except that the merge function is different.
-behaviour(gen_server).
@colefichter
colefichter / mmc.erl
Created July 22, 2015 21:10
A multi-master (AKA distributed) counter implemented as a Conflict-free Replicated Data Type.
-module(mmc).
% A multi-master counter implemented as a CRDT. Each server holds an array of counters (implemented as a dict here...)
% and Strong Eventual Consistency is acheived by using commutive increments on each of the counters. The current
% value read at any server is the sum of all the counter values (again, one per server) stored on that machine.
% As updates are synced between servers, all servers will eventually converge on a counter sum value with no
% conflicts possible. Also note that this does not use any consensus algorithm, so it remains fast as the number of
% servers scales. See http://research.microsoft.com/apps/video/default.aspx?id=153540&r=1
-behaviour(gen_server).
@colefichter
colefichter / numeric_helper.erl
Created June 14, 2015 04:11
Convert any number to string and back
% If you have a string that contains a numeric value, but you don't know if it is a float or an integer, then the
% standard conversion functions list_to_integer/1 and list_to_float/1 are a pain.
% Using these functions is easier, since they check the types and convert correctly for you:
numeric_to_list(X) when is_float(X) -> float_to_list(X, [{decimals, 9}, compact]);
numeric_to_list(X) when is_integer(X) -> integer_to_list(X).
list_to_numeric(L) when is_list(L) ->
Float = (catch erlang:list_to_float(L)),
Int = (catch erlang:list_to_integer(L)),
@colefichter
colefichter / rr_queue.erl
Last active August 29, 2015 14:22
A round-robin queue for Erlang systems.
%%%-------------------------------------------------------------------
%%% @author Cole Fichter, www.colefichter.ca
%%% @copyright (C) 2014,
%%% @doc
%%% This module implements a round-robin priority queue with a client API similar to the standard Erlang queue.
%%% Items are added to the queue with a key (an integer, usually some kind of id). When requested to dequeue an
%%% item, the queue first picks a sub-queue by the key, then dequeues the first item under that key. The 'last key'
%%% is incremented, and the next dequeued item will come from the sub-queue for the updated key.
%%%
%%% Example use-case: preventing starvation while crawling multiple web sites. Suppose we're crawling three
@colefichter
colefichter / stringhash.erl
Created March 28, 2015 01:26
Need a stringified hash of some data in erlang? Try this!
hash(Data) when is_atom(Data) -> hash(atom_to_list(Data));
hash(Data) ->
Binary160 = crypto:hash(sha, Data),
hexstring(Binary160).
% Convert a 160-bit binary hash to a hex string.
hexstring(<<X:160/big-unsigned-integer>>) ->
lists:flatten(io_lib:format("~40.16.0b", [X])).
@colefichter
colefichter / ring.erl
Last active August 29, 2015 14:17
A Process Ring in Erlang
-module(ring).
% A simple process ring made up of a doubly-linked ring of processes.
% A message can be passed in either direction around the ring.
%
% Example usage in the Erlang shell:
% > c(ring).
% > First = ring:start(5).
% > ring:send_anti_cw("Hi", First, 10).
% > {relay,anti_clockwise,10,1,"Hi"}
% > <0.38.0> Relaying 1 "Hi"
@colefichter
colefichter / kv.erl
Created March 6, 2015 19:24
A very simple Key-Value memory store, written in Erlang
-module(kv).
-export([start/0, stop/0, store/1, lookup/1, delete/1]).
-export([server_loop/1]).
%---------------------------------------------------------------------------------------------
% Client API
%---------------------------------------------------------------------------------------------
@colefichter
colefichter / ErlangRebar.sublime-build
Created December 29, 2014 03:20
Simple Sublime Text Custom Build System for Elang/Rebar projects
{
"cmd": ["./rebar compile eunit"],
"shell": true,
"working_dir": "${file_path}/../"
}
@colefichter
colefichter / diffie_hellman.html
Created May 3, 2013 20:35
An interactive demonstration of Diffie-Hellman Key Exchange.
<html>
<head>
<title>Diffie-Hellman Key Exchange</title>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<style>
.secret {
background-color: #ffa9a9;
}
th {
background-color: lightgray;