Skip to content

Instantly share code, notes, and snippets.

View talentdeficit's full-sized avatar

alisdair sullivan talentdeficit

View GitHub Profile
-module(json).
-export([json_to_term/2]).
%% extract the encoding from the options to pass to the parser, using auto if no encoding is
%% specified
json_to_term(JSON, Opts) ->
Encoding = proplists:get_value(encoding, Opts, auto),
P = jsx:parser([{encoding, Encoding}]),
collect(P(JSON), none, Opts).
-module(jsx_ex).
-export([simple_decode/1]).
simple_decode(JSON) when is_binary(JSON) ->
P = jsx:parser(),
decode(P(JSON), []).
decode({event, end_json, _Next}, Acc) ->
lists:reverse(Acc);
application:start({application, a, [{mod, {gen_app, [a]}}]}).
{"Kernel pid terminated",application_controller,{{badmatch,false},[{application_controller,handle_application_started,3},{gen_server,handle_msg,5}]}}
case X of
one -> 1
; two -> 2
; _ -> more
end.
lists:foldl(fun add_to_record/2, #record{}, List).
add_to_record({first_name, Value}, Rec) ->
Rec#record{first_name = Value};
add_to_record({last_name, Value}, Rec) ->
Rec#record{last_name = Value};
add_to_record({age, Value}, Rec) ->
Rec#record{age = Value};
...
function(...) ->
SomeVariable = value(),
AnotherVariable = another_value(),
X = x(),
...
5> 9007199254740991 / 404804506614621236704990693437834614099113299528284236713802716054860679135990693783920767402874248990374155728633623822779617474771586953734026799881477019843034848553132722728933815484186432682479535356945490137124014966849385397236206711298319112681620113024717539104666829230461005064372655017292012526615415482186989568.
** exception error: bad argument in an arithmetic expression
in operator '/'/2
called as 9007199254740991 / 404804506614621236704990693437834614099113299528284236713802716054860679135990693783920767402874248990374155728633623822779617474771586953734026799881477019843034848553132722728933815484186432682479535356945490137124014966849385397236206711298319112681620113024717539104666829230461005064372655017292012526615415482186989568
json_to_term(JSON) ->
try json_to_term(JSON, [])
catch Type:Error -> erlang:Type(Error)
end.
15> Z = fun(X) -> X = true end.
#Fun<erl_eval.6.13229925>
16> Z(false).
** exception error: no match of right hand side value true
17> try Z(false) catch error:badmatch -> ok end.
** exception error: no match of right hand side value true
18> try Z(false) catch error:Type -> Type end.
{badmatch,true}
loop([end_json], Acc) -> lists:reverse(Acc);
loop([Event|Events], Acc) -> loop(Events, [Event] ++ Acc);
loop(_, _) -> {error, badjson}.