Skip to content
Closed
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
56 changes: 56 additions & 0 deletions src/gleam/iterator.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -1627,3 +1627,59 @@ pub fn each(over iterator: Iterator(a), with f: fn(a) -> b) -> Nil {
pub fn yield(element: a, next: fn() -> Iterator(a)) -> Iterator(a) {
Iterator(fn() { Continue(element, fn() { next().continuation() }) })
}

/// Returns an iterator of sliding windows.
///
/// ## Examples
///
/// ```gleam
/// from_list([1,2,3,4,5])
/// |> window(3)
/// |> map(to_list)
/// |> to_list
/// // -> [[1, 2, 3], [2, 3, 4], [3, 4, 5]]
/// ```
///
/// ```gleam
/// from_list([1,2])
/// |> window(4)
/// |> map(to_list)
/// |> to_list
/// // -> []
/// ```
///
pub fn window(i: Iterator(a), by n: Int) -> Iterator(Iterator(a)) {
let yield = fn(iterator) {
let window = take(iterator, n)

case length(window) == n {
True -> Next(window, drop(iterator, 1))
False -> Done
}
}

case n > 0 {
True -> unfold(from: i, with: yield)
False -> empty()
}
}

/// Returns an iterator of tuples containing two contiguous elements.
///
/// ## Examples
///
/// ```gleam
/// window_by_2([1,2,3,4])
/// |> to_list
/// // -> [#(1, 2), #(2, 3), #(3, 4)]
/// ```
///
/// ```gleam
/// window_by_2([1])
/// |> to_list
/// // -> []
/// ```
///
pub fn window_by_2(i: Iterator(a)) -> Iterator(#(a, a)) {
zip(i, drop(i, 1))
}
31 changes: 31 additions & 0 deletions test/gleam/iterator_test.gleam
Original file line number Diff line number Diff line change
Expand Up @@ -730,3 +730,34 @@ pub fn yield_computes_only_necessary_values_test() {
|> iterator.to_list
|> should.equal([1, 2, 3])
}

pub fn window_test() {
let testcase = fn(subject, n, expected) {
subject
|> iterator.from_list
|> iterator.window(n)
|> iterator.map(iterator.to_list)
|> iterator.to_list
|> should.equal(expected)
}

testcase([1, 2, 3], 2, [[1, 2], [2, 3]])
testcase([1, 2, 3], 3, [[1, 2, 3]])
testcase([1, 2, 3], 4, [])
testcase([1, 2, 3, 4, 5], 3, [[1, 2, 3], [2, 3, 4], [3, 4, 5]])
testcase([1, 2, 3], 0, [])
testcase([1, 2, 3], -1, [])
}

pub fn window_by_2_test() {
let testcase = fn(subject, expected) {
subject
|> iterator.from_list
|> iterator.window_by_2
|> iterator.to_list
|> should.equal(expected)
}

testcase([1, 2, 3, 4], [#(1, 2), #(2, 3), #(3, 4)])
testcase([1], [])
}