Skip to content

Commit

Permalink
Small improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
simonewebdesign committed Jun 4, 2017
1 parent 449580e commit af8fd35
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions lib/primes.ex
Expand Up @@ -62,13 +62,11 @@ defmodule Primes do
def first(0), do: []
def first(n) when n > 0 do
2
|> Stream.iterate(increment())
|> Stream.filter(&prime?(&1))
|> Stream.iterate(&(&1 + 1))
|> Stream.filter(&prime?/1)
|> Stream.take(n)
end

defp increment, do: &(&1 + 1)

@doc """
Returns the nth prime number.
Expand All @@ -86,7 +84,7 @@ defmodule Primes do
@spec prime(pos_integer()) :: pos_integer()
def prime(n) when n <= 0, do: raise ArgumentError, message: "not a positive integer"
def prime(n) do
first(n) |> Enum.at(-1)
Enum.at(first(n), -1)
end

@doc """
Expand Down

0 comments on commit af8fd35

Please sign in to comment.