XMPP JavaScript Library

Stack implementation in erlang

Erlang, Sachin Puri, 2019-12-04 23:06:34

%% @author Sachin Puri

-module(stack).
-behaviour(gen_server).
-compile(export_all).

%% ====================================================================
%% API functions
%% ====================================================================

start()->
    gen_server:start({local, stack}, ?MODULE, [], []).

push(Data)->
    gen_server:call(?MODULE, {push, Data}).

pop()->
    gen_server:call(?MODULE, {pop}).

ls()->
    gen_server:call(?MODULE, {ls}).
    
%% ====================================================================
%% Internal functions
%% ====================================================================

init(_Args)->
    {ok, []}.

handle_call({push, Data},_From, State)->
    NewState = [Data|State],
    {reply, NewState, NewState};

handle_call({pop},_From, [H|T])->
    NewState = T,
    {reply, H, NewState};

handle_call({pop},_From, [])->
    {reply, [], []};

handle_call({ls},_From, State)->
    {reply, State, State}.

handle_cast(_Request, State) ->
  {noreply, State}.

handle_info(_Info, State) ->
  {noreply, State}.

terminate(_Reason, _State) ->
  ok.

code_change(_OldVsn, State, _Extra) ->
  {ok, State}.

 

Add Your Comment
   
    Yes! I want to receive all comments by email

No Comments Posted Yet. Be the first one to post comment