From 09e2c7ee3950b0fa22d9ca1b1b68d71cc811d018 Mon Sep 17 00:00:00 2001 From: Gioele Bucci Date: Tue, 28 May 2024 23:30:33 +0200 Subject: Fix infinite recursion loop in `list.window` when `by` is 0 The function would enter an endless loop if 0 was passed as second argument, for e.g `list.window([1, 2], 0) // endless loop!` --- src/gleam/list.gleam | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/gleam/list.gleam b/src/gleam/list.gleam index e782b45..83870b4 100644 --- a/src/gleam/list.gleam +++ b/src/gleam/list.gleam @@ -1913,8 +1913,10 @@ fn do_window(acc: List(List(a)), l: List(a), n: Int) -> List(List(a)) { /// ``` /// pub fn window(l: List(a), by n: Int) -> List(List(a)) { - do_window([], l, n) - |> reverse + case n <= 0 { + True -> [] + False -> do_window([], l, n) |> reverse + } } /// Returns a list of tuples containing two contiguous elements. -- cgit v1.2.3