Skip to content

Instantly share code, notes, and snippets.

View vinoski's full-sized avatar

Steve Vinoski vinoski

View GitHub Profile
@vinoski
vinoski / x.erl
Last active October 18, 2016 19:06
bitwise xor of bit lists
-module(x).
-export([lxor/2]).
%%% see http://stackoverflow.com/a/40098595/409228 for context
lxor(L1,L2) when is_list(L1), is_list(L2) ->
%% convert the input lists to binaries, then call lxor/2 on the results
lxor(list2bin(L1),list2bin(L2));
lxor(B1,B2) when is_binary(B1), is_binary(B2) ->
%% decode unsigned integers from the input binaries, then call lxor/2
@vinoski
vinoski / gs.erl
Created April 6, 2016 19:00
example gen_server
-module(gs).
-behaviour(gen_server).
-export([start/0, stop/0]).
-export([init/1, handle_call/3, handle_cast/2, handle_info/2,
terminate/2, code_change/3]).
-record(state, {count}).
start() ->
@vinoski
vinoski / riak-core-patch-for-17
Created December 1, 2014 04:26
unofficial riak_core changes for Erlang/OTP 17
diff --git a/include/riak_core_handoff.hrl b/include/riak_core_handoff.hrl
index 9956205..e34c4c8 100644
--- a/include/riak_core_handoff.hrl
+++ b/include/riak_core_handoff.hrl
@@ -30,7 +30,7 @@
transport_mon :: reference(),
timestamp :: tuple(),
status :: any(),
- stats :: dict(),
+ stats :: dict:dict(),
@vinoski
vinoski / .bashrc-kerl
Last active September 13, 2016 12:52
Auto-generate bash functions for selecting kerl-installed Erlang versions. Functions are named based on the kerl-installed versions; for example, if you have R16B03 installed as r16b03, your shell will have a function named r16b03 for selecting that version. If you install a new version, run kerl_gen in your shell to regenerate the version selec…
export MYKERLVSN=''
no_kerl()
{
kerl_deactivate
unset _KERL_SAVED_AGNER_BIN _KERL_SAVED_AGNER_EXACT_PREFIX _KERL_SAVED_REBAR_PLT_DIR AGNER_BIN AGNER_EXACT_PREFIX
MYKERLVSN=''
}
kerl_gen()
@vinoski
vinoski / sws.erl
Last active January 12, 2024 14:59
An example of a very simple HTTP 1.0 web server in Erlang.
%% Simple web server.
-module(sws).
-author('Steve Vinoski <vinoski@ieee.org>').
-export([start/1, start/2]).
%% start/1 takes a handler function and starts the web server on port 8000.
%% start/2 takes a handler function and a port number. The handler function
%% takes two arguments: a TCP socket and request data. The request data is
%% a property list indicating the invoked HTTP method, the target URI, the
@vinoski
vinoski / fd_setsize_on_osx.md
Last active November 7, 2017 06:20
How to raise the maximum number of file descriptors when building Erlang/OTP on OS X.

When you build Erlang/OTP on OS X, it unfortunately defaults to handling a maximum of 1024 file descriptors. You can get around this limitation with the right combination of configuration options and manual changes to a generated config file.

First, go into your unpacked Erlang/OTP source directory and run the following command, replacing the value 10000 with whatever value you want for max file descriptors:

perl -i -pe 's/(define\s+FD_SETSIZE\s+)\d+/\1 10000/' erts/config.h.in 

Next, when you run configure in your Erlang/OTP source directory, be sure to include the right CFLAGS setting, as shown below:

CFLAGS='-DREDEFINE_FD_SETSIZE -DFD_SETSIZE=15000 -D_DARWIN_UNLIMITED_SELECT' ./configure --enable-kernel-poll <other options>
@vinoski
vinoski / get_all_bindings2.erl
Created January 27, 2013 14:35
Another version of the get_all_bindings parse transform (see https://gist.github.com/4643721), but using erl_syntax and erl_syntax_lib.
-module(get_all_bindings2).
-export([parse_transform/2]).
-author('Steve Vinoski <vinoski@ieee.org>').
%% This parse transform looks for a plain call to get_all_bindings/1 within
%% each function body within the module being processed and if found
%% replaces its argument with a list of {variable name, value} tuples, one
%% for each variable used in the function body up to the call point. The
%% module must contain its own suitable definition for the
%% get_all_bindings/1 function.
@vinoski
vinoski / get_all_bindings.erl
Last active December 11, 2015 18:48
This parse transform looks for a plain call to get_all_bindings/1 within each function body within the module being processed and if found replaces its argument with a list of {variable name, value} tuples, one for each variable used in the function body up to the call point. The module must contain its own suitable definition for the get_all_bi…
-module(get_all_bindings).
-export([parse_transform/2]).
-author('Steve Vinoski <vinoski@ieee.org>').
%% This parse transform looks for a plain call to get_all_bindings/1 within
%% each function body within the module being processed and if found
%% replaces its argument with a list of {variable name, value} tuples, one
%% for each variable used in the function body up to the call point. The
%% module must contain its own suitable definition for the
%% get_all_bindings/1 function.
@vinoski
vinoski / pt_async.erl
Created December 11, 2012 20:37
parse transform to make exported functions call internal async versions
-module(pt_async).
-export([parse_transform/2]).
parse_transform(AST, _Options) ->
asyncify_funs(AST, []).
%% Search for functions whose bodies simply return the atom "async" (minus
%% the quotes of course). Replace that body with a call to the async
%% version of the called function, passing a ref as well. The async function
%% (assuming it succeeds) passes the result back as a message, which the