You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
defmoduleAccdodefnew(s)whenis_bitstring(s)doString.to_integer(s)enddefnew(int)whenis_integer(int)dointenddefadd(acc,i)whenis_integer(i)doacc+ienddefinc(acc)doadd(acc,1)enddefdec(acc)doadd(acc,-1)enddefshow(acc)do"The ants arr is #{acc}"endend
defmoduleServicedodefstart(input)docount=input|>Acc.new()spawn(fn->loop(count)end)enddefinc(pid)dosend(pid,:inc)enddefdec(pid)dosend(pid,:dec)enddefshow(pid)dosend(pid,{:show,self()})receivedom->mendenddefloop(count)docount|>listen()|>loop()enddeflisten(count)doreceivedo:inc->Acc.inc(count):dec->Acc.dec(count){:show,from_pid}-># send back the count to the usermessage=Acc.show(count)send(from_pid,message)# return the current count so our recursion doesn't blow upcountendendend
defmoduleServerdouseGenServer# clientdefstart_link(input)doGenServer.start_link(Server,input)enddefinc(pid)doGenServer.cast(pid,:inc)enddefdec(pid)doGenServer.cast(pid,:dec)enddefshow(pid)doGenServer.call(pid,:show)end# api stuff will all go here# server# construct@impltruedefinit(input)doinitial_state=Acc.new(input){:ok,initial_state}end# reduce@impltruedefhandle_cast(:inc,count)donew_state=Acc.inc(count){:noreply,new_state}end@impltruedefhandle_cast(:dec,count)donew_state=Acc.dec(count){:noreply,new_state}end# convert@impltruedefhandle_call(:show,_from_pid,count)domessage=Acc.show(count){:reply,message,count}endend