Skip to content

Instantly share code, notes, and snippets.

@shoz-f
Created October 1, 2018 12:16
Show Gist options
  • Select an option

  • Save shoz-f/9d0c9929e05d0e6034097df062e160b9 to your computer and use it in GitHub Desktop.

Select an option

Save shoz-f/9d0c9929e05d0e6034097df062e160b9 to your computer and use it in GitHub Desktop.
Agentで実装
defmodule StopwatchD do
use Agent
import System, only: [ monotonic_time: 1 ]
@gclock __MODULE__
def activate() do
now = monotonic_time(100)
Agent.start_link(fn -> now end, name: @gclock)
:ok
end
def restart() do
Agent.get_and_update(@gclock, fn last_time ->
now = monotonic_time(100)
{now - last_time, now}
end)
end
def lap() do
Agent.get(@gclock, &(monotonic_time(100) - &1))
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment