Permalink
Browse files

Merge pull request #116 from rafaelfranca/enum-times

Enum.times start form 1
  • Loading branch information...
2 parents d88b981 + eb5e247 commit 8e9b50dd2c73580671885d108f0e98fb74a3aac3 @josevalim josevalim committed Feb 11, 2012
Showing with 16 additions and 16 deletions.
  1. +13 −13 lib/enum.ex
  2. +3 −3 test/elixir/enum_test.exs
View
@@ -285,37 +285,37 @@ defmodule Enum do
do_mapfoldl(iterator.(collection), iterator, acc, fun)
end
- # Iterates the given function n times, passing values from zero
- # to n - 1.
+ # Iterates the given function n times, passing values from 1
+ # to n.
#
# ## Examples
#
# Enum.times 3, fn(x, do: IO.puts x)
- # 0
# 1
# 2
+ # 3
#
def times(times, function) when times >= 0 do
case is_function(function, 0) do
match: true
- do_times_0(times, 0, function)
+ do_times_0(times, 1, function)
else:
- do_times_1(times, 0, function)
+ do_times_1(times, 1, function)
end
times
end
- # Iterates the given function n times, passing values from zero
- # to n - 1. Also has an accumulator similar to fold to store the
+ # Iterates the given function n times, passing values from 1
+ # to n. Also has an accumulator similar to fold to store the
# value between computations.
#
# ## Examples
#
# Enum.times 5, 0, fn(x, acc, do: acc + x)
- # #=> 10
+ # #=> 15
#
def times(times, acc, function) when times >= 0 do
- do_times_2(times, 0, function, acc)
+ do_times_2(times, 1, function, acc)
end
## Implementations
@@ -473,23 +473,23 @@ defmodule Enum do
## times
- defp do_times_0(limit, limit, _function) do
+ defp do_times_0(limit, counter, _function) when counter > limit do
end
defp do_times_0(limit, counter, function) do
function.()
do_times_0(limit, 1 + counter, function)
end
- defp do_times_1(limit, limit, _function) do
+ defp do_times_1(limit, counter, _function) when counter > limit do
end
defp do_times_1(limit, counter, function) do
function.(counter)
do_times_1(limit, 1 + counter, function)
end
- defp do_times_2(limit, limit, _function, acc) do
+ defp do_times_2(limit, counter, _function, acc) when counter > limit do
acc
end
@@ -535,4 +535,4 @@ defimpl Enum::Iterator, for: List do
defp iterate([]) do
__STOP_ITERATOR__
end
-end
+end
@@ -93,13 +93,13 @@ defmodule EnumTest do
def test_times_with_arity_1 do
5 = Enum.times 5, fn(x, do: Process.put(:times_with_arity, x))
- 4 = Process.get(:times_with_arity)
+ 5 = Process.get(:times_with_arity)
after:
Process.erase(:times_with_arity)
end
def test_times_with_arity_2 do
- 10 = Enum.times 5, 0, fn(acc, x) -> acc + x end
+ 15 = Enum.times 5, 0, fn(acc, x) -> acc + x end
end
def test_enum_for do
@@ -113,4 +113,4 @@ defmodule EnumTest do
[{1, 3}, {1, 4}, {2, 3}, {2, 4}] = for {x,_} in lists, y in [3,4], do: {x,y}
[{1, 3}, {1, 4}] = for {x,_} in lists, y in [3,4], x == 1, do: {x,y}
end
-end
+end

0 comments on commit 8e9b50d

Please sign in to comment.