mirror of
https://github.com/typst/typst
synced 2025-05-13 20:46:23 +08:00
Add windows
method to array (#4136)
Co-authored-by: Laurenz <laurmaedje@gmail.com>
This commit is contained in:
parent
6c9bcd83ae
commit
b0306785d5
@ -790,6 +790,26 @@ impl Array {
|
||||
}
|
||||
}
|
||||
|
||||
/// Returns sliding windows of `window-size` elements over an array.
|
||||
///
|
||||
/// If the array length is less than `window-size`, this will return an empty array.
|
||||
///
|
||||
/// ```example
|
||||
/// #let array = (1, 2, 3, 4, 5, 6, 7, 8)
|
||||
/// #array.windows(5)
|
||||
/// ```
|
||||
#[func]
|
||||
pub fn windows(
|
||||
self,
|
||||
/// How many elements each window will contain.
|
||||
window_size: NonZeroUsize,
|
||||
) -> Array {
|
||||
self.0
|
||||
.windows(window_size.get())
|
||||
.map(|window| Array::from(window).into_value())
|
||||
.collect()
|
||||
}
|
||||
|
||||
/// Return a sorted version of this array, optionally by a given key
|
||||
/// function. The sorting algorithm used is stable.
|
||||
///
|
||||
|
@ -328,6 +328,21 @@
|
||||
// Error: 19-21 number must be positive
|
||||
#(1, 2, 3).chunks(-5)
|
||||
|
||||
--- array-windows ---
|
||||
// Test the `windows` method.
|
||||
#test(().windows(5), ())
|
||||
#test((1, 2, 3).windows(5), ())
|
||||
#test((1, 2, 3, 4, 5).windows(3), ((1, 2, 3), (2, 3, 4), (3, 4, 5)))
|
||||
#test((1, 2, 3, 4, 5, 6, 7, 8).windows(5), ((1, 2, 3, 4, 5), (2, 3, 4, 5, 6), (3, 4, 5, 6, 7), (4, 5, 6, 7, 8)))
|
||||
|
||||
--- array-windows-size-zero ---
|
||||
// Error: 20-21 number must be positive
|
||||
#(1, 2, 3).windows(0)
|
||||
|
||||
--- array-windows-size-negative ---
|
||||
// Error: 20-22 number must be positive
|
||||
#(1, 2, 3).windows(-5)
|
||||
|
||||
--- array-sorted ---
|
||||
// Test the `sorted` method.
|
||||
#test(().sorted(), ())
|
||||
|
Loading…
x
Reference in New Issue
Block a user