Re:2048 in Erlang
by lostcolony on Monday June 09, 2014 @05:17PM
(#7870579)
Some style stuff, for terseness, you can do things like change -
move(left, State) ->
move(getVector(left), State);
move(right, State) ->
move(getVector(right), State);
move(up, State) ->
move(getVector(up), State);
move(down, State) ->
move(getVector(down), State);
move(Vector, State) ->
...
to move(Direction, State) when is_atom(Direction) ->
move(getVector(Direction), State);
move(Vector, State) ->
...
Essentially, it's what allows application:start(erl2048) to work. The {mod, {erl2048_app, []}} param is what indicates you want to run the code in erl2048_app.erl (though obviously the compiled beam file equivalent) as an application; the application behavior says that starting an application calls the equivalent start/# function.
So basically the order of startup when you call application:start(erl2048) is
Erlang looks for the munged erl2048.app.src, and loads that metadata.
The mod argument specifies a module to start.
The start function in that module is called (as it is expected to adhere to the application behavior). That function should start up the top level supervisor, which in turn is in charge of starting your entire supervisor heirarchy, and by extension, your actual worker processes.