cognis-core 0.2.1

Core traits and types for the Cognis LLM framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//! Iteration utilities.

/// Batch an iterator into chunks of a given size.
pub fn batch_iterate<T>(size: usize, iterable: impl IntoIterator<Item = T>) -> Vec<Vec<T>> {
    let mut batches = Vec::new();
    let mut current_batch = Vec::with_capacity(size);
    for item in iterable {
        current_batch.push(item);
        if current_batch.len() == size {
            batches.push(current_batch);
            current_batch = Vec::with_capacity(size);
        }
    }
    if !current_batch.is_empty() {
        batches.push(current_batch);
    }
    batches
}