Skip to content

Instantly share code, notes, and snippets.

@kylethebaker
kylethebaker / super.erl
Created September 3, 2017 10:15
Kent - Concurrent Programming in Erlang - Week 2
-module(super).
-export([start/0, init/0]).
%% Starts the supervisor in it's own process
start() ->
spawn(super, init, []).
%% Initializes the supervisor with it's children
init() ->
process_flag(trap_exit, true),
@kylethebaker
kylethebaker / polling_log_reader.ex
Created September 16, 2017 00:08
Example polling a log file
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# Log Reader - polls log file and sends new lines to channel
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
defmodule LogReader do
use GenServer
@log_file "priv/some_log.log"
@poll_interval 5 * 1000 # 5 seconds
def run_test() do
@kylethebaker
kylethebaker / map_difference.ex
Last active September 26, 2017 06:23
Map diffs, golfed
# Compares two maps and generates a list of actions that can be applied to the
# first map to transform it into the second.
#
# diff = difference(m1, m2)
# apply(m1, diff) === m2
defmodule MapDiffGolf do
def difference(%{} = m1, %{} = m2), do:
for {k, v} <- m2,
@kylethebaker
kylethebaker / hof-golfed.js
Created October 8, 2017 10:55
Code golfing map/reduce/filter
//----------------------------------------------------------------------------
// recursive golfed reduce
//----------------------------------------------------------------------------
let r = (f, a, [h, ...t]) => !t.length ? f(a, h) : r(f, f(a, h), t)
function reduce(fn, acc, [x, ...xs]) {
return !xs.length
? fn(acc, x)
: reduce(fn, fn(acc, x), xs);
}