Skip to content

Instantly share code, notes, and snippets.

@andrewsardone andrewsardone/gist:6969549
Last active Dec 25, 2015

Embed
What would you like to do?
Elixir doctests
defmodule AndrewEnum do
@moduledoc """
A reimplementation of the standard `Enum` module.
"""
@doc """
Invokes the given `fun` for each item in the `collection`.
Returns `:ok`.
## Examples
iex> AndrewEnum.each([], fn -> end)
:ok
iex> AndrewEnum.each([1, 2, 3], fn(x) -> Process.put(:andrew_enum_test_each, x * 2) end)
iex> Process.get(:andrew_enum_test_each)
6
Note that the above example as a doctest is a bit contrived, but I
need *some* kind of side effect to assert on for AndrewEnum.each. I
stole the idea of stuffing state into the process dictionary via the
`Process` module from `elixir/enum_test.exs`.
Another note, but this doesn't *completely* replicate Enum.each since
it only accepts a list and not *any* `Enumerable` collection.
"""
def each([], _), do: :ok
def each([head|tail], fun) do
fun.(head)
each(tail, fun)
end
end
defmodule AndrewEnum.Test do
use ExUnit.Case
doctest AndrewEnum
end
# ❯ mix test
# ..
#
# Finished in 0.06 seconds (0.06s on load, 0.00s on tests)
# 2 tests, 0 failures
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.