Skip to content

Instantly share code, notes, and snippets.

@macintux
macintux / thunk.erl
Created January 11, 2017 22:00
Implement in Erlang the lazy evaluation/streaming approach taught by Nathan Dotz at CodeMash 2017.
-module(thunk).
-compile(export_all).
%% > thunk:take(3, thunk:forever_zero()).
%% [0,0,0]
%% > thunk:take(3, thunk:fib()).
%% [1,1,2]
%% > thunk:take(18, thunk:fib()).
%% [1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987,1597,2584]
@macintux
macintux / gist:2e17371bd581a2fd629237931f552e50
Created August 31, 2016 20:06
Pragmatic Functional Programming With Erlang
For many developers, the thought of learning functional programming is intimidating. Parentheses, lambda calculus, type theory, and you can't even modify a variable!
In this talk we'll look at code that does useful work without requiring a Ph.D. in greybeard. Talk to software on another computer without opening a socket. Iterate over your data without setting up an iterator class hierarchy. Disassemble a network protocol header with this one cool line of code!
We'll talk about ideas that are being adopted by languages you probably already use. No functional programming experience required, just a willingness to see what can happen when you think outside the OO box.
@macintux
macintux / erlang_dates.erl
Created June 22, 2016 20:06
Illustrate key Erlang date/time structures and conversions.
%% Date formats in Erlang are a bit tricky to keep straight,
%% especially since the `calendar' library leverages Gregorian seconds
%% (seconds since year 0) rather than the more traditional UNIX epoch
%% seconds.
%%
%% This module illustrates the structures and conversions.
-module(erlang_dates).
-compile(export_all).
@macintux
macintux / beam_decompile.erl
Created November 20, 2015 15:36 — forked from andelf/beam_decompile.erl
Erlang BEAM file decompile to .erl file
#!/usr/bin/env escript
% -*- mode: erlang -*-
main([BeamFile]) ->
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(BeamFile,[abstract_code]),
io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
@macintux
macintux / errors.erl
Created August 4, 2015 13:02
cuttlefish/lager errors
=ERROR REPORT==== 4-Aug-2015::09:01:01 ===
** gen_event handler lager_stderr_backend crashed.
** Was installed in lager_event
** Last event was: {log,{lager_msg,[],
[{application,cuttlefish},
{module,cuttlefish_conf},
{function,generate_element},
{line,122},
{pid,"<0.719.0>"},
{node,nonode@nohost}],
@macintux
macintux / replace_list_item.erl
Created February 20, 2015 15:31
Handy function for replacing an element in a list
%% Replace the item at `Index' in `List' with `Element'.
%% Zero-based indexing. Deal with it.
-spec replace_list_element(non_neg_integer(), term(), list()).
replace_list_element(Index, Element, List) ->
{Prefix, Suffix} = lists:split(Index, List),
Prefix ++ [Element] ++ tl(Suffix).
@macintux
macintux / baderlang.md
Created May 27, 2014 15:41
Erlang style gotchas

Name your parameters

Erlang atoms are awesome. Use them.

Counter-example:

    Existing = get_bucket_type(BucketType, undefined, false),

What do undefined and false mean on the other end? Who knows?