Skip to content

Instantly share code, notes, and snippets.

@kevsmith
Created April 17, 2013 15:56
Show Gist options
  • Select an option

  • Save kevsmith/5405449 to your computer and use it in GitHub Desktop.

Select an option

Save kevsmith/5405449 to your computer and use it in GitHub Desktop.
Automating common record operations
Erlang R16B (erts-5.10.1) [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:true] [dtrace]
Eshell V5.10.1 (abort with ^G)
1> record_demo:record_names().
[foo,bar,baz]
2> A = [{<<"a">>, 1}, {<<"b">>, 2}, {<<"c">>, 3}].
[{<<"a">>,1},{<<"b">>,2},{<<"c">>,3}]
3> B = [{"g", 5}, {"h", 7}, {"i", 9}].
[{"g",5},{"h",7},{"i",9}]
4> C = [{d, 11}, {e, 13}, {f, 15}].
[{d,11},{e,13},{f,15}]
5> {ok, R1} = record_demo:record_from_proplist(A, foo, binary).
{ok,{foo,1,2,3}}
6> RecordName = baz.
baz
7> record_demo:record_from_proplist(B, RecordName, string).
{ok,{baz,5,7,9}}
8> f(RecordName).
ok
9> RecordName = bar.
bar
10> record_demo:record_from_proplist(C, RecordName, atom).
{ok,{bar,11,13,15}}
11> Validator = fun({bar, D, E, F}=R) -> if D < 12 -> {error, bad_record}; true -> R end end.
#Fun<erl_eval.6.17052888>
12> record_demo:record_from_proplist(C, RecordName, atom, Validator).
{error,bad_record}
13> record_demo:record_to_proplist(R1).
[{a,1},{b,2},{c,3}]
-record(foo, {a,
b,
c}).
-record(bar, {d,
e,
f}).
-record(baz, {g,
h,
i}).
-module(record_demo).
-include("rec1.hrl").
-include("rec2.hrl").
%% Enables parse transform
-include("dynrecord.hrl").
@adamhjk

adamhjk commented Apr 17, 2013

Copy link
Copy Markdown

Pretty dope.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment