Skip to content

Instantly share code, notes, and snippets.

@aaronlelevier
Created July 4, 2019 02:58
Show Gist options
  • Save aaronlelevier/ae9de743265ffe6c23ddc9f513062c4b to your computer and use it in GitHub Desktop.
Save aaronlelevier/ae9de743265ffe6c23ddc9f513062c4b to your computer and use it in GitHub Desktop.
Erlang Codejam
Erlang/OTP 20 [erts-9.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]
Eshell V9.2.1 (abort with ^G)
1> bob.
bob
2> % ok let's go.
2> ab.
ab
3> bob.
bob
4> Map = #{first_name := "aaron"}.
* 1: only association operators '=>' are allowed in map construction
5> Map = #{first_name => "aaron"}.
#{first_name => "aaron"}
6> FullNameMap = Map#{last_name => "lelevier"}.
#{first_name => "aaron",last_name => "lelevier"}
7> Attrs = maps:keys(FullNameMap).
[first_name,last_name]
8> Values = maps:values(FullNameMap).
["aaron","lelevier"]
9> tuple_to_list({Attrs, Values}).
[[first_name,last_name],["aaron","lelevier"]]
10> lists:flat(Attrs, Values).
** exception error: undefined function lists:flat/2
11> lists:flat(Attrs).
** exception error: undefined function lists:flat/1
12> lists:flatten(Attrs).
[first_name,last_name]
13> lists:flatten(Attrs, Values).
[first_name,last_name,"aaron","lelevier"]
14> P = fun(X) -> X * 2 end.
#Fun<erl_eval.6.99386804>
15> lists:fold
foldl/3 foldr/3
15> lists:foldl(P, void, [1,2,3]).
** exception error: interpreted function with arity 1 called with two arguments
in function lists:foldl/3 (lists.erl, line 1263)
16> P1 = fun(X, y) -> {X,Y} end.
* 1: variable 'Y' is unbound
17> P1 = fun(X, Y) -> {X,Y} end.
#Fun<erl_eval.12.99386804>
18> lists:foldl(P1, void, [1,2,3]).
{3,{2,{1,void}}}
19> lists:foldl(P1, 9, [1,2,3]).
{3,{2,{1,9}}}
20> P2 = fun(X, Y) -> {X*7,Y*2} end.
#Fun<erl_eval.12.99386804>
21> lists:foldl(P2, 1, [1,2,3]).
** exception error: an error occurred when evaluating an arithmetic expression
in operator */2
called as {7,2} * 2
in call from erl_eval:do_apply/6 (erl_eval.erl, line 674)
in call from erl_eval:expr_list/6 (erl_eval.erl, line 878)
in call from erl_eval:expr/5 (erl_eval.erl, line 236)
in call from lists:foldl/3 (lists.erl, line 1263)
22> P2 = fun(X, Y) -> X*Y end.
** exception error: no match of right hand side value #Fun<erl_eval.12.99386804>
23> P3 = fun(X, Y) -> X*Y end.
#Fun<erl_eval.12.99386804>
24> lists:foldl(P3, 1, [1,2,3]).
6
25> list_to_tuple([1,2]).
{1,2}
26> list_to_tuple([1,2, "three", four]).
{1,2,"three",four}
27> Tup = list_to_tuple([1,2, "three", four]).
{1,2,"three",four}
28> Bin = term_to_binary(Tup).
<<131,104,4,97,1,97,2,107,0,5,116,104,114,101,101,100,0,4,
102,111,117,114>>
29> StatusCode = 200.
200
30> Packet = <<StatusCode:4, Bin/binary>>.
<<136,54,128,70,16,22,16,38,176,0,87,70,135,38,86,86,64,0,
70,102,247,87,2:4>>
31> <<RecHeader:4, RecBin/binary>> = Packet.
<<136,54,128,70,16,22,16,38,176,0,87,70,135,38,86,86,64,0,
70,102,247,87,2:4>>
32>
32> RecHeader.
8
33> c(utils).
{ok,utils}
34> utils:type(RecHeader).
integer
35> Packet2 = <<StatusCode:32, Bin/binary>>.
<<0,0,0,200,131,104,4,97,1,97,2,107,0,5,116,104,114,101,
101,100,0,4,102,111,117,114>>
36> <<RecHeader2:32, RecBin2/binary>> = Packet2.
<<0,0,0,200,131,104,4,97,1,97,2,107,0,5,116,104,114,101,
101,100,0,4,102,111,117,114>>
37>
37> RecHeader2.
200
38> RecBin2.
<<131,104,4,97,1,97,2,107,0,5,116,104,114,101,101,100,0,4,
102,111,117,114>>
39> binary_to_term(RecBin2).
{1,2,"three",four}
40> Values.
["aaron","lelevier"]
41> lists:zip(Attrs, Values).
[{first_name,"aaron"},{last_name,"lelevier"}]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment