Skip to content

Instantly share code, notes, and snippets.

@alexbowe
Created April 2, 2011 06:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexbowe/899298 to your computer and use it in GitHub Desktop.
Save alexbowe/899298 to your computer and use it in GitHub Desktop.
Provides some idiomatic tools to help when programming using Erlang's Binary primitive type.
-module(my_binary).
-export([foreach/3]).
-export([fold/4]).
break(Blocksize, Bin) ->
<<Head:Blocksize/bits, Rest/bits>> = Bin,
{Head, Rest}.
foreach(_, _, <<>>) -> ok;
foreach(F, Blocksize, Bin) ->
{Head, Rest} = break(Blocksize, Bin),
F(Head),
foreach(F, Blocksize, Rest).
fold(_, Result, _, <<>>) -> Result;
fold(F, Initial, Blocksize, Bin) ->
{Head, Rest} = break(Blocksize, Bin),
Temp = F(Initial, Head),
fold(F, Temp, Blocksize, Rest).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment