Skip to content

Commit

Permalink
Merge pull request #3 from ck3g/agent_robot
Browse files Browse the repository at this point in the history
Use an agent to keep the robot's state
  • Loading branch information
ck3g committed Dec 24, 2017
2 parents fb5d167 + a38f3cb commit e72f8ae
Showing 1 changed file with 17 additions and 0 deletions.
17 changes: 17 additions & 0 deletions lib/agent_robot.ex
@@ -0,0 +1,17 @@
defmodule ToyRobot.AgentRobot do
def place do
{:ok, state} = ToyRobot.place
{:ok, pid} = Agent.start(fn -> state end, name: __MODULE__)
pid
end

def move, do: Agent.update(__MODULE__, &(ToyRobot.move(&1)))

def left, do: Agent.update(__MODULE__, &(ToyRobot.left(&1)))

def right, do: Agent.update(__MODULE__, &(ToyRobot.right(&1)))

def report do
Agent.get(__MODULE__, &(&1)) |> ToyRobot.report
end
end

0 comments on commit e72f8ae

Please sign in to comment.