Skip to content

Instantly share code, notes, and snippets.

@macintux
macintux / helperfuns.erl
Created January 13, 2014 03:37
Utility functions for Erlang-based Riak demos. These ignore error handling and use strings instead of binaries for readability. Using the anonymous functions makes for a cleaner read.
%% Useful in Erlang shell. These assume an already-defined handle named "Riak"
Get = fun(X, Y) -> helpers:get(Riak, X, Y) end.
RawGet = fun(X, Y) -> helpers:raw_get(Riak, X, Y) end.
Put = fun(X) -> helpers:put(Riak, X) end.
Update = fun(X, Y, Z) -> helpers:update(Riak, X, Y, Z) end.
Resolve = fun(X, Y) -> helpers:update(Riak, X, Y) end.
New = fun(X, Y, Z) -> helpers:new(X, Y, Z) end.
%% Add an object to Riak and retrieve it:
@macintux
macintux / joinpdfs.md
Created December 24, 2013 22:57
Consolidating PDFs from the command line in MacOS X

Assuming you've created a text file named "ordered" with the PDFs in the sequence you'd like to create:

$ cp /System/Library/Automator/Combine\ PDF\ Pages.action/Contents/Resources/join.py /usr/local/bin/join-pdfs

$ perl -e '$first = <STDIN>; chomp $first; while(<STDIN>) { $index++; $intmdt = "foo$index.pdf"; chomp; system("join-pdfs -o $intmdt $first $_"); $first = $intmdt; }' < ordered

Now you'll have a host of foo.pdf files; the last one is the one you want to rename to something useful, then rm foo*.pdf.

@macintux
macintux / wtf-erlang.md
Last active December 30, 2015 15:59
A rabbit hole of Erlang docs

I started working on some missing documentation in the Erlang stdlib, and found one particularly confusing rabbit hole.

gen_server.xml

In the documentation for gen_server, I found debug options for start_link which lacked type information:

    Dbg = trace | log | statistics | {logfile,FileName} | {install,{Func,FuncState}}</v>
        SOpts = [term()]

FileName isn't defined, Func and FuncState aren't defined, so I decided to dig into the source to document their types.

@macintux
macintux / example-erlang-types.md
Last active December 22, 2015 03:18
Erlang type examples, because I can never remember how they work and I utterly fail at reading docs

Doc: http://www.erlang.org/doc/reference_manual/typespec.html

-type square() :: tuple(pos_integer(), pos_integer()).
-type side() :: 'white'|'black'.
-type movefun() :: fun((square(), square(), side()) -> tuple(square(), list(square()))).
-type movedef() :: tuple(atom(), movefun()).

-record(move, { piece,
 start,
curl -XPOST http://localhost:8098/mapred
-H"Content-Type: application/json"
-d '{"inputs":"notifications","query":[{"map":{"language":"erlang","source":"fun(O,_,_) when element(1,O) == r_object -> S=byte_size(term_to_binary(riak_object:get_values(O))), if (S>1048576) -> {ok,C}=riak:local_client(),C:delete(riak_object:bucket(O),riak_object:key(O)),[riak_object:key(O)]; true -> [] end;(_,_,_) -> [] end."}}]}'
@macintux
macintux / 2i-more.py
Created August 21, 2013 16:07
Perform secondary index (2i) searches on Twitter hashtags stored in Riak and count all hashtags in the matching tweets.
#!/usr/local/bin/python
import riak
Riak = riak.RiakClient(pb_port=10017, protocol='pbc')
TweetsBucket = Riak.bucket('tweets')
results = TweetsBucket.get_index("hashtags_bin", "android",
"androie", return_terms=False)
@macintux
macintux / 2i.py
Created August 21, 2013 16:06
Perform secondary index (2i) searches on Twitter hashtags stored in Riak and count the matching tags.
#!/usr/local/bin/python
import riak
Riak = riak.RiakClient(pb_port=10017, protocol='pbc')
TweetsBucket = Riak.bucket('tweets')
results = TweetsBucket.get_index("hashtags_bin", "android",
"androie", return_terms=True)