Created
October 1, 2018 12:16
-
-
Save shoz-f/9d0c9929e05d0e6034097df062e160b9 to your computer and use it in GitHub Desktop.
Agentで実装
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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