Created
December 12, 2023 11:42
-
-
Save RobinBoers/f4a9e72fa8eeda3926078517339fd019 to your computer and use it in GitHub Desktop.
This file contains 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 Model do | |
@doc """ | |
A function that given the current value calculates the | |
difference between the current and next value. | |
""" | |
@type t :: (value() -> dvalue()) | |
@doc """ | |
The value the model will mutate. | |
""" | |
@type value :: integer() | |
@doc """ | |
The difference in the value returned by `t:t()`. | |
""" | |
@type dvalue :: value() | |
@type points :: [value()] | |
@type run_opts :: [ | |
start_value: integer(), | |
start_at: integer(), | |
max_t: integer(), | |
dt: integer() | |
] | |
@spec run(t(), run_opts()) :: value() | |
def run(model, opts \\ []) do | |
start_value = Keyword.get(opts, :start_value, 0) | |
start_at = Keyword.get(opts, :start_at, 0) | |
run(model, start_value, start_at, opts) | |
end | |
@spec run(t(), value(), integer(), run_opts()) :: value() | |
def run(model, value, i, opts) do | |
max_t = Keyword.get(opts, :max_t, 100) | |
dt = Keyword.get(opts, :dt, 1) | |
if dt <= max_t do | |
value = value + model(value) * dt | |
run(model, value, i + dt, opts) | |
else | |
value | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment