Skip to content

Instantly share code, notes, and snippets.

@gausby
Last active January 19, 2017 20:56
Show Gist options
  • Save gausby/29716572b113dbdbec63a0abdd8bb88b to your computer and use it in GitHub Desktop.
Save gausby/29716572b113dbdbec63a0abdd8bb88b to your computer and use it in GitHub Desktop.
Probably not the best we can come up with!
defmodule Dice do
@moduledoc """
The best I could come up with...
iex> import Dice
...> ~d'1d6'+3
"""
def sigil_d(input, []) do
[n, eyes] =
String.split(input, "d", parts: 2)
|> Enum.map(&String.to_integer/1)
# Baby needs a new pair of shoes!
Stream.repeatedly(fn -> :rand.uniform(eyes) end)
|> Enum.take(n)
|> Enum.sum()
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment