Skip to content

Instantly share code, notes, and snippets.

View asonge's full-sized avatar

Alexander Songe asonge

View GitHub Profile
@asonge
asonge / leaky_bucket.ex
Last active July 12, 2017 16:14
GenStateMachine Leaky Bucket example
defmodule LeakyBucket do
@moduledoc """
A state machine implementation of the leaky bucket rate limiting algorithm
The leaky bucket algorithm is a commonly used rate limiting algorithm. You
start out with a number of drops in the bucket, and drops "drip" out of the
bucket for each action taken. Drops drip into the bucket based on a fixed
timer. This allows for some burst capacity.
This version obviously doesn't scale very highly, but most leaky buckets out
// See http://brunch.io for documentation.
exports.files = {
javascripts: {
entryPoints: {
'game.js': 'brunchtest.js'
}
},
};
exports.paths = {
Sanshiro Sugata (1943)
The Most Beautiful (1944)
Sanshiro Sugata Part Two (1945)
The Men Who Tread on the Tiger's Tail (1945)
No Regrets for Our Youth (1946)
One Wonderful Sunday (1947)
Drunken Angel (1948)
Stray Dog (1949)
Scandal (1950)
Rashomon (1950)
@asonge
asonge / resolveLines
Created May 22, 2016 01:25 — forked from cutecycle/resolveLines
Array.find crash at length = 1447
var look = function(lineid) {
var find;
find = (lines_json.find( function(value) {
return (value.lineid === lineid);
}));
if(find.speakerid !== "" || (typeof find.speakerid === "undefined")) {
find.retrievedSpeaker = line(entity(find.speakerid).entityname);
}
lines_out.push(find);
if(find.advance_lineid !== "" || (typeof find.advance_lineid === "undefined")) {
-module(chat_server).
-export([start/1]).
handle_socket(Router, Client) ->
case gen_tcp:recv(Client, 0) of
{ok, "quit!} ->
gen_tcp:close(Client);
{ok, Data} ->
Router ! {msg, Client, Data}
# In case anyone is wondering how to easily use the :crypto hash or hmac API's from elixir-land, here you go.
File.stream!(filename, [:read, {:read_ahead, 65535}], 65535)
|> Enum.reduce(:crypto.hash_init(:sha), &:crypto.hash_update(&2, &1))
|> :crypto.hash_final
|> Base.encode16
# Line 3 opens up a stream, and we're reading in 64KB chunks from the FS which is roughly pretty efficient
# In line 4, we initialize our hash (which returns a context) and put that as the default value in the reducer
# The hash_update in the reducer returns the modified context. Remember arg1 is the item, and arg2 is the accumulator
@asonge
asonge / behaviour.ex
Last active August 29, 2015 14:25 — forked from anonymous/behaviour.ex
defmodule Datastore do
use Behaviour
@type key :: any
@type value :: any
defcallback start_link() :: Agent.t
defcallback get(Agent.t, key) :: value
defcallback put(Agent.t, key, value) :: :ok
Insertion into Elixir.Map. 10 entries
-------------------------------------
Range: from 14 to 4480µs
Median: 16µs
Average: 462.2µs
Insertion into Elixir.Map. 100 entries
--------------------------------------
defmodule HTMLCharacters do
def sanitize(phrase) do
chars = %{"&quot;" => "\"", "&#39;" => "'", "&amp;" => "&", "&lt;" => "<", "&gt;" => ">"}
cond do
String.match?(phrase, ~r/&\#?.+;/) ->
[code] = Regex.run(~r/&\#?.+;/, phrase)
Regex.replace(Regex.compile!(code), phrase, chars[code])
true -> phrase
-module(inotifywait).
-include("api.hrl").
-export(?API).
find_executable() -> os:find_executable("inotifywait").
known_events() -> [renamed, closed, modified, isdir, undefined].
start_port(Path, Cwd) ->
Path1 = filename:absname(Path),
Args = ["-c", "inotifywait $0 $@ & PID=$!; read a; kill $PID",