Skip to content

Instantly share code, notes, and snippets.

@narrowtux
Last active June 22, 2016 14:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save narrowtux/6daf3c232d165cdf8d2dc95241c8fa60 to your computer and use it in GitHub Desktop.
Save narrowtux/6daf3c232d165cdf8d2dc95241c8fa60 to your computer and use it in GitHub Desktop.
defmodule Range
def range(num, min_value, max_value) do
num
|> min(max_value)
|> max(min_value)
end
end
@lucidstack
Copy link

An alternative version with guards... 😀

defmodule Range do
  def range(num, min_value, max_value) when num < min_value, do: min_value
  def range(num, min_value, max_value) when num > max_value, do: max_value
  def range(num, min_value, max_value), do: num
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment