Crates.io | array_stat_functions |
lib.rs | array_stat_functions |
version | 0.1.1 |
source | src |
created_at | 2024-10-26 05:53:00.783781 |
updated_at | 2024-10-26 06:00:30.225517 |
description | Array and Statistics functions in rust |
homepage | |
repository | |
max_upload_size | |
id | 1423564 |
size | 8,856 |
A Rust crate providing useful array manipulation utilities and statistical functions for everyday programming needs.
Add this to your Cargo.toml
:
[dependencies]
array_stat_functions = "0.1.0" # Replace with your current version
use array_stat_functions::{chunk_vec, remove_duplicates, find_pairs_with_sum};
fn main() {
// Chunking a vector
let numbers = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
let chunks = chunk_vec(&numbers, 3).unwrap();
println!("Chunks: {:?}", chunks);
// Output: [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
// Removing duplicates
let duplicates = vec![1, 2, 3, 2, 4, 3, 5];
let unique = remove_duplicates(&duplicates);
println!("Unique elements: {:?}", unique);
// Output: [1, 2, 3, 4, 5]
// Finding pairs with sum
let numbers = vec![1, 5, 7, 1, 5, 3, 2];
let pairs = find_pairs_with_sum(&numbers, 6);
println!("Pairs that sum to 6: {:?}", pairs);
// Output: [(1, 5), (1, 5), (3, 3)]
}
use array_stat_functions::{mean, median, mode};
fn main() {
let dataset = vec![1.0, 2.0, 3.0, 4.0, 5.0, 5.0];
// Calculate mean
let avg = mean(&dataset);
println!("Mean: {}", avg);
// Calculate median
let med = median(&dataset);
println!("Median: {}", med);
// Calculate mode (works with integers)
let integers = vec![1, 2, 2, 3, 4, 4, 4, 5];
let mod_value = mode(&integers);
println!("Mode: {}", mod_value);
}
chunk_vec<T: Clone>(vec: &[T], chunk_size: usize) -> Result<Vec<Vec<T>>, &'static str>
remove_duplicates<T: Clone + PartialEq>(vec: &[T]) -> Vec<T>
find_pairs_with_sum<T: Copy + Hash + Eq + Sub<Output = T>>(arr: &[T], target: T) -> Vec<(T, T)>
mean(vec: &[f64]) -> f64
median(vec: &[f64]) -> f64
mode(vec: &[i32]) -> i32
Contributions are welcome! Please feel free to submit a Pull Request.
This project is licensed under the MIT License - see the LICENSE file for details.
This crate was created as a learning project to better understand Rust and contribute to the Rust ecosystem.