Skip to content

Instantly share code, notes, and snippets.

@LeebDeveloper
Last active August 29, 2015 13:57
Show Gist options
  • Save LeebDeveloper/9437243 to your computer and use it in GitHub Desktop.
Save LeebDeveloper/9437243 to your computer and use it in GitHub Desktop.
Different ways to apply '+'
-module(plustest).
-compile(export_all).
plus1(A, B) -> A + B.
plus2(A, B) -> erlang:'+'(A, B).
plus3(A, B) ->
F = fun erlang:'+'/2,
apply(F, [A, B]).
plus31(A, B) ->
F = fun erlang:'+'/2,
F(A, B).
plus4(A, B) ->
P = '+',
F = fun erlang:P/2,
apply(F, [A, B]).
plus5(A, B) -> apply21('+', A, B).
apply21(F, A, B) -> erlang:F(A, B).
plus6(A, B) -> apply22(A, B, '+').
apply22(A, B, F) -> erlang:F(A, B).
{module, plustest}. %% version = 0
{exports, [{apply21,3},
{apply22,3},
{module_info,0},
{module_info,1},
{plus1,2},
{plus2,2},
{plus3,2},
{plus31,2},
{plus4,2},
{plus5,2},
{plus6,2}]}.
{attributes, []}.
{labels, 23}.
{function, plus1, 2, 2}.
{label,1}.
{line,[{location,"plustest.erl",4}]}.
{func_info,{atom,plustest},{atom,plus1},2}.
{label,2}.
{line,[{location,"plustest.erl",4}]}.
{gc_bif,'+',{f,0},2,[{x,0},{x,1}],{x,0}}.
return.
{function, plus2, 2, 4}.
{label,3}.
{line,[{location,"plustest.erl",6}]}.
{func_info,{atom,plustest},{atom,plus2},2}.
{label,4}.
{line,[{location,"plustest.erl",6}]}.
{gc_bif,'+',{f,0},2,[{x,0},{x,1}],{x,0}}.
return.
{function, plus3, 2, 6}.
{label,5}.
{line,[{location,"plustest.erl",8}]}.
{func_info,{atom,plustest},{atom,plus3},2}.
{label,6}.
{allocate,2,2}.
{move,{x,0},{y,1}}.
{move,{x,1},{y,0}}.
{move,{atom,'+'},{x,1}}.
{move,{integer,2},{x,2}}.
{move,{atom,erlang},{x,0}}.
{line,[{location,"plustest.erl",9}]}.
{call_ext,3,{extfunc,erlang,make_fun,3}}.
{test_heap,4,1}.
{put_list,{y,0},nil,{x,2}}.
{put_list,{y,1},{x,2},{x,1}}.
{line,[{location,"plustest.erl",10}]}.
{call_ext_last,2,{extfunc,erlang,apply,2},2}.
{function, plus31, 2, 8}.
{label,7}.
{line,[{location,"plustest.erl",12}]}.
{func_info,{atom,plustest},{atom,plus31},2}.
{label,8}.
{allocate,2,2}.
{move,{x,0},{y,1}}.
{move,{x,1},{y,0}}.
{move,{atom,'+'},{x,1}}.
{move,{integer,2},{x,2}}.
{move,{atom,erlang},{x,0}}.
{line,[{location,"plustest.erl",13}]}.
{call_ext,3,{extfunc,erlang,make_fun,3}}.
{move,{y,0},{x,1}}.
{move,{x,0},{x,2}}.
{move,{y,1},{x,0}}.
{line,[{location,"plustest.erl",14}]}.
{call_fun,2}.
{deallocate,2}.
return.
{function, plus4, 2, 10}.
{label,9}.
{line,[{location,"plustest.erl",16}]}.
{func_info,{atom,plustest},{atom,plus4},2}.
{label,10}.
{allocate,2,2}.
{move,{x,0},{y,1}}.
{move,{x,1},{y,0}}.
{move,{atom,'+'},{x,1}}.
{move,{integer,2},{x,2}}.
{move,{atom,erlang},{x,0}}.
{line,[{location,"plustest.erl",18}]}.
{call_ext,3,{extfunc,erlang,make_fun,3}}.
{test_heap,4,1}.
{put_list,{y,0},nil,{x,2}}.
{put_list,{y,1},{x,2},{x,1}}.
{line,[{location,"plustest.erl",19}]}.
{call_ext_last,2,{extfunc,erlang,apply,2},2}.
{function, plus5, 2, 12}.
{label,11}.
{line,[{location,"plustest.erl",21}]}.
{func_info,{atom,plustest},{atom,plus5},2}.
{label,12}.
{move,{x,1},{x,2}}.
{move,{x,0},{x,1}}.
{move,{atom,'+'},{x,0}}.
{call_only,3,{f,14}}.
{function, apply21, 3, 14}.
{label,13}.
{line,[{location,"plustest.erl",22}]}.
{func_info,{atom,plustest},{atom,apply21},3}.
{label,14}.
{allocate,0,3}.
{move,{x,0},{x,3}}.
{move,{x,1},{x,0}}.
{move,{x,2},{x,1}}.
{move,{atom,erlang},{x,2}}.
{line,[{location,"plustest.erl",22}]}.
{apply_last,2,0}.
{function, plus6, 2, 16}.
{label,15}.
{line,[{location,"plustest.erl",24}]}.
{func_info,{atom,plustest},{atom,plus6},2}.
{label,16}.
{move,{atom,'+'},{x,2}}.
{call_only,3,{f,18}}.
{function, apply22, 3, 18}.
{label,17}.
{line,[{location,"plustest.erl",25}]}.
{func_info,{atom,plustest},{atom,apply22},3}.
{label,18}.
{allocate,0,3}.
{move,{x,2},{x,3}}.
{move,{atom,erlang},{x,2}}.
{line,[{location,"plustest.erl",25}]}.
{apply_last,2,0}.
{function, module_info, 0, 20}.
{label,19}.
{line,[]}.
{func_info,{atom,plustest},{atom,module_info},0}.
{label,20}.
{move,{atom,plustest},{x,0}}.
{line,[]}.
{call_ext_only,1,{extfunc,erlang,get_module_info,1}}.
{function, module_info, 1, 22}.
{label,21}.
{line,[]}.
{func_info,{atom,plustest},{atom,module_info},1}.
{label,22}.
{move,{x,0},{x,1}}.
{move,{atom,plustest},{x,0}}.
{line,[]}.
{call_ext_only,2,{extfunc,erlang,get_module_info,2}}.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment