Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/elixir/lib/calendar/date_range.ex
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ defmodule Date.Range do
[date_from_iso_days(current, calendar)]
end

defp slice(current, step, remaining, calendar) do
defp slice(current, step, remaining, calendar) when remaining > 1 do
[
date_from_iso_days(current, calendar)
| slice(current + step, step, remaining - 1, calendar)
Expand Down
18 changes: 13 additions & 5 deletions lib/elixir/lib/enum.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3674,9 +3674,14 @@ defmodule Enum do
end

def take(enumerable, amount) when is_integer(amount) and amount < 0 do
{count, fun} = slice_count_and_fun(enumerable, 1)
first = Kernel.max(amount + count, 0)
fun.(first, count - first, 1)
case slice_count_and_fun(enumerable, 1) do
{0, _fun} ->
[]

{count, fun} ->
first = Kernel.max(amount + count, 0)
fun.(first, count - first, 1)
end
end

@doc """
Expand Down Expand Up @@ -5181,6 +5186,9 @@ defimpl Enumerable, for: Range do
slice(Map.put(range, :step, step))
end

defp slice(_current, _step, 0), do: []
defp slice(current, step, remaining), do: [current | slice(current + step, step, remaining - 1)]
defp slice(current, _step, 1), do: [current]

defp slice(current, step, remaining) when remaining > 1 do
[current | slice(current + step, step, remaining - 1)]
end
end
4 changes: 4 additions & 0 deletions lib/elixir/test/elixir/calendar/date_range_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,10 @@ defmodule Date.RangeTest do
end
end

test "Enum.take/1 for empty range with negative step" do
assert Enum.take(@empty_range, -1) == []
end

test "works with date-like structs" do
range = Date.range(~N[2000-01-01 09:00:00], ~U[2000-01-02 09:00:00Z])
assert range.first == ~D[2000-01-01]
Expand Down