Skip to content

Instantly share code, notes, and snippets.

@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);
}
@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 / 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 / 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 / returning_funs.erl
Created July 16, 2017 06:07
Week 3 - Functions as Results
-module(returning_funs).
-compile(export_all).
-type unary() :: fun((Term) -> Term).
% composes a function onto another F(G(X))
-spec compose(unary(), unary()) -> unary().
compose(F, G) ->
fun (X) -> F(G(X)) end.
@kylethebaker
kylethebaker / assignment2.erl
Last active July 10, 2017 06:18
Functional Erlang Week 2 Assignment
-module(assignment2).
-export([print_indexes/1]).
-export([get_indexes/1, get_top_n_words/2, get_alpha_indexes/1]).
-define(SPLIT_TOKENS, " .,'\"()[]{}!?-\\").
% Gets all of the indexes.
get_indexes(Filename) ->
maps:to_list(get_indexes_ranges(Filename)).
@kylethebaker
kylethebaker / more_functions_over_lists.erl
Created July 10, 2017 01:20
Week 2 - More functions over lists
-module(more_functions_over_lists).
-export([join/2, concat/1]).
-export([reverse/1]).
-export([member/2]).
-export([merge_sort/1, quick_sort/1, insertion_sort/1]).
-export([permutations/1]).
join([], Ys) -> Ys;
join([X | Xs], Ys) ->
def encode(key, pt):
"""Encodes the plaintext using key."""
return "".join([encode_single(x, y) for x, y in get_pairs(key, pt)])
def decode(key, ct):
"""Decodes the ciphertext using key."""
return "".join([decode_single(x, y) for x, y in get_pairs(key, ct)])
@kylethebaker
kylethebaker / functions_over_list.erl
Created June 28, 2017 01:24
Week 2 - Functions Over Lists
-module(functions_over_lists).
-export([product/1]).
-export([maximum/1]).
-export([product_fold/1]).
product(X) -> product(X, 1).
product([], Product) -> Product;
product([X | Xs], Product) -> product(Xs, X * Product).
@kylethebaker
kylethebaker / assignment1.erl
Last active June 27, 2017 09:13
Function Erlang Week 1 Assignment
-module(assignment1).
-export([area/1]).
-export([perimeter/1]).
-export([enclose/1]).
-export([tail_bits/1]).
-export([direct_bits/1]).
% Define shape types
% coordinate pair