@@ -3009,13 +3009,16 @@ defmodule Enum do
3009
3009
3010
3010
if first < count and amount > 0 do
3011
3011
amount = Kernel . min ( amount , count - first )
3012
- amount = if step == 1 , do: amount , else: div ( amount - 1 , step ) + 1
3012
+ amount = amount_with_step ( amount , step )
3013
3013
fun . ( first , amount , step )
3014
3014
else
3015
3015
[ ]
3016
3016
end
3017
3017
end
3018
3018
3019
+ defp amount_with_step ( amount , 1 ) , do: amount
3020
+ defp amount_with_step ( amount , step ) , do: div ( amount - 1 , step ) + 1
3021
+
3019
3022
@ doc """
3020
3023
Returns a subset list of the given `enumerable`, from `start_index` (zero-based)
3021
3024
with `amount` number of elements if available.
@@ -4440,15 +4443,15 @@ defmodule Enum do
4440
4443
4441
4444
if start >= 0 do
4442
4445
amount = Kernel . min ( amount , count - start )
4443
- amount = if step == 1 , do: amount , else: div ( amount - 1 , step ) + 1
4446
+ amount = amount_with_step ( amount , step )
4444
4447
fun . ( start , amount , step )
4445
4448
else
4446
4449
[ ]
4447
4450
end
4448
4451
end
4449
4452
4450
4453
defp slice_forward ( list , start , amount , step ) when is_list ( list ) do
4451
- amount = if step == 1 , do: amount , else: div ( amount - 1 , step ) + 1
4454
+ amount = amount_with_step ( amount , step )
4452
4455
slice_list ( list , start , amount , step )
4453
4456
end
4454
4457
@@ -4458,7 +4461,7 @@ defmodule Enum do
4458
4461
[ ]
4459
4462
4460
4463
{ :ok , count , fun } when is_function ( fun , 1 ) ->
4461
- amount = Kernel . min ( amount , count - start )
4464
+ amount = Kernel . min ( amount , count - start ) |> amount_with_step ( step )
4462
4465
enumerable |> fun . ( ) |> slice_exact ( start , amount , step , count )
4463
4466
4464
4467
# TODO: Deprecate me in Elixir v1.18.
@@ -4473,8 +4476,7 @@ defmodule Enum do
4473
4476
end
4474
4477
4475
4478
{ :ok , count , fun } when is_function ( fun , 3 ) ->
4476
- amount = Kernel . min ( amount , count - start )
4477
- amount = if step == 1 , do: amount , else: div ( amount - 1 , step ) + 1
4479
+ amount = Kernel . min ( amount , count - start ) |> amount_with_step ( step )
4478
4480
fun . ( start , amount , step )
4479
4481
4480
4482
{ :error , module } ->
0 commit comments