# Lowdash

[](https://coveralls.io/github/liberocks/lowdash?branch=master)
[](https://app.codacy.com/gh/liberocks/lowdash/dashboard?utm_source=gh&utm_medium=referral&utm_content=&utm_campaign=Badge_grade)
[](https://github.com/liberocks/lowdash/graphs/contributors)
[](./LICENSE)
**liberocks/lowdash** is a Lodash inspired utility library to manipulate array and object
## ⚠️ Disclaimer
This project is still in heavy development and not ready for production use.
## 🚀 Installation
```bash
cargo add lowdash
```
This library has no dependencies outside the Rust standard library.
## 📚 Documentation
You can find the generated documentation [here](https://docs.rs/lowdash)
Utility functions for array:
- [Lowdash](#lowdash)
- [⚠️ Disclaimer](#️-disclaimer)
- [🚀 Installation](#-installation)
- [📚 Documentation](#-documentation)
- [camel\_case](#camel_case)
- [capitalize](#capitalize)
- [char\_length](#char_length)
- [chunk\_string](#chunk_string)
- [earliest\_by](#earliest_by)
- [earliest](#earliest)
- [ellipsis](#ellipsis)
- [find\_duplicates\_by](#find_duplicates_by)
- [find\_duplicates](#find_duplicates)
- [find\_index\_of](#find_index_of)
- [find\_key\_by](#find_key_by)
- [find\_key](#find_key)
- [find\_last\_index\_of](#find_last_index_of)
- [find\_or\_else](#find_or_else)
- [find\_uniques](#find_uniques)
- [find\_uniques\_by](#find_uniques_by)
- [find](#find)
- [first\_or\_empty](#first_or_empty)
- [first\_or](#first_or)
- [first](#first)
- [index\_of](#index_of)
- [kebab\_case](#kebab_case)
- [last\_index\_of](#last_index_of)
- [last\_or\_empty](#last_or_empty)
- [last\_or](#last_or)
- [last](#last)
- [latest\_by](#latest_by)
- [latest](#latest)
- [max\_by](#max_by)
- [max](#max)
- [min\_by](#min_by)
- [min](#min)
- [nearest\_power\_of\_two](#nearest_power_of_two)
- [nth](#nth)
- [pascal\_case](#pascal_case)
- [random\_string](#random_string)
- [sample](#sample)
- [samples](#samples)
- [snake\_case](#snake_case)
- [substring](#substring)
- [words](#words)
- [reject](#reject)
- [filter](#filter)
- [map](#map)
- [filter\_map](#filter_map)
- [flat\_map](#flat_map)
- [reduce](#reduce)
- [reduce\_right](#reduce_right)
- [foreach](#foreach)
- [foreach\_while](#foreach_while)
- [times](#times)
- [uniq](#uniq)
- [uniq\_by](#uniq_by)
- [group\_by](#group_by)
- [chunk](#chunk)
- [partition\_by](#partition_by)
- [flatten](#flatten)
- [interleave](#interleave)
- [shuffle](#shuffle)
- [reverse](#reverse)
- [fill](#fill)
- [repeat](#repeat)
- [repeat\_by](#repeat_by)
- [key\_by](#key_by)
- [associate](#associate)
- [slice\_to\_map](#slice_to_map)
- [drop](#drop)
- [drop\_right](#drop_right)
- [drop\_while](#drop_while)
- [drop\_right\_while](#drop_right_while)
- [drop\_by\_index](#drop_by_index)
- [reject\_map](#reject_map)
- [filter\_reject](#filter_reject)
- [count](#count)
- [count\_by](#count_by)
- [count\_values](#count_values)
- [count\_values\_by](#count_values_by)
- [subset](#subset)
- [slice](#slice)
- [replace](#replace)
- [replace\_all](#replace_all)
- [compact](#compact)
- [Arguments](#arguments)
- [Type Parameters](#type-parameters)
- [Returns](#returns)
- [Examples](#examples)
- [is\_sorted](#is_sorted)
- [is\_sorted\_by\_key](#is_sorted_by_key)
- [splice](#splice)
- [keys](#keys)
- [uniq\_keys](#uniq_keys)
- [has\_key](#has_key)
- [values](#values)
- [uniq\_values](#uniq_values)
- [value\_or](#value_or)
- [pick\_by](#pick_by)
- [pick\_by\_keys](#pick_by_keys)
- [pick\_by\_values](#pick_by_values)
- [omit\_by](#omit_by)
- [omit\_by\_keys](#omit_by_keys)
- [omit\_by\_values](#omit_by_values)
- [entries](#entries)
- [to\_pairs](#to_pairs)
- [from\_entries](#from_entries)
- [from\_pairs](#from_pairs)
- [invert](#invert)
- [assign](#assign)
- [map\_values](#map_values)
- [map\_keys](#map_keys)
- [map\_entries](#map_entries)
- [map\_to\_slice](#map_to_slice)
- [range](#range)
- [range\_from](#range_from)
- [range\_with\_step](#range_with_step)
- [clamp](#clamp)
- [sum](#sum)
- [sum\_by](#sum_by)
- [product](#product)
- [product\_by](#product_by)
- [mean](#mean)
- [mean\_by](#mean_by)
- [percentile](#percentile)
- [median](#median)
- [interpolate](#interpolate)
- [permutation](#permutation)
- [combination](#combination)
- [duration\_between](#duration_between)
- [🔥 Benchmark (experimental)](#-benchmark-experimental)
- [🫡 Acknowledgement](#-acknowledgement)
Utility functions for string manipulation:
- [camel_case](#camel_case)
- [capitalize](#capitalize)
- [char_length](#char_length)
- [chunk_string](#chunk_string)
- [ellipsis](#ellipsis)
- [kebab_case](#kebab_case)
- [pascal_case](#pascal_case)
- [random_string](#random_string)
- [snake_case](#snake_case)
- [substring](#substring)
- [words](#words)
Utility functions for object manipulation:
- [assign](#assign)
- [entries](#entries)
- [from_entries](#from_entries)
- [from_pairs](#from_pairs)
- [has_key](#has_key)
- [invert](#invert)
- [keys](#keys)
- [map_entries](#map_entries)
- [map_keys](#map_keys)
- [map_to_slice](#map_to_slice)
- [map_values](#map_values)
- [omit_by](#omit_by)
- [omit_by_keys](#omit_by_keys)
- [omit_by_values](#omit_by_values)
- [pick_by](#pick_by)
- [pick_by_keys](#pick_by_keys)
- [pick_by_values](#pick_by_values)
- [to_pairs](#to_pairs)
- [uniq_keys](#uniq_keys)
- [uniq_values](#uniq_values)
- [value_or](#value_or)
- [values](#values)
Utility functions for math:
- [nearest_power_of_two](#nearest_power_of_two)
- [range](#range)
- [range_from](#range_from)
- [range_with_step](#range_with_step)
- [clamp](#clamp)
- [sum](#sum)
- [sum_by](#sum_by)
- [product](#product)
- [product_by](#product_by)
- [mean](#mean)
- [mean_by](#mean_by)
- [percentile](#percentile)
- [median](#median)
- [interpolate](#interpolate)
- [permutation](#permutation)
- [combination](#combination)
- [duration_between](#duration_between)
### camel_case
Converts a string to camelCase.
```rust
use lowdash::camel_case;
assert_eq!(camel_case("hello world"), "helloWorld");
assert_eq!(camel_case("foo-bar"), "fooBar");
assert_eq!(camel_case("lorem_ipsum"), "loremIpsum");
assert_eq!(camel_case("FooBarBazHello"), "fooBarBazHello");
```
### capitalize
Capitalizes the first letter of the input string and converts the rest to lowercase.
```rust
use lowdash::capitalize;
assert_eq!(capitalize("hello"), "Hello");
assert_eq!(capitalize("WORLD"), "World");
assert_eq!(capitalize("rUsT"), "Rust");
```
### char_length
Returns the length of a string in Unicode characters.
```rust
use lowdash::char_length;
assert_eq!(char_length("hello"), 5);
assert_eq!(char_length("🌍world"), 6);
assert_eq!(char_length("こんにちは"), 5);
```
### chunk_string
Splits a string into chunks of specified size.
```rust
use lowdash::chunk_string;
let result = chunk_string("hello", 2);
assert_eq!(result, vec!["he", "ll", "o"]);
```
### earliest_by
Find the earliest item in a collection based on a custom iteratee function.
```rust
use std::time::{SystemTime, Duration};
use lowdash::earliest_by;
let t1 = SystemTime::UNIX_EPOCH;
let t2 = t1 + Duration::new(60, 0);
let t3 = t1 + Duration::new(120, 0);
let times = vec![t2, t1, t3];
#[derive(Debug, PartialEq, Clone)]
struct Event {
name: String,
timestamp: SystemTime,
}
let events = vec![
Event {
name: "Event1".to_string(),
timestamp: t2,
},
Event {
name: "Event2".to_string(),
timestamp: t1,
},
Event {
name: "Event3".to_string(),
timestamp: t3,
},
];
earliest_event,
Some(Event {
name: "Event2".to_string(),
timestamp: t1,
})
);
```
### earliest
Find the earliest time in a collection.
```rust
use std::time::{SystemTime, Duration};
use lowdash::earliest;
let t1 = SystemTime::UNIX_EPOCH;
let t2 = t1 + Duration::new(60, 0);
let t3 = t1 + Duration::new(120, 0);
let times = vec![t2, t1, t3];
let earliest_time = earliest(×);
assert_eq!(earliest_time, Some(t1));
```
### ellipsis
Truncates a string and appends an ellipsis (`"..."`) if it exceeds the specified length.
```rust
use lowdash::ellipsis;
let result = ellipsis("Hello, World!", 10);
assert_eq!(result, "Hello, ...");
let result = ellipsis("Short", 10);
assert_eq!(result, "Short");
let result = ellipsis("ExactLength", 11);
assert_eq!(result, "ExactLength");
let result = ellipsis(" Trimmed ", 6);
assert_eq!(result, "Tri...");
let result = ellipsis("Hi", 2);
assert_eq!(result, "Hi");
```
### find_duplicates_by
Find all duplicate elements in a collection based on a key generated by the iteratee function.
```rust
use lowdash::find_duplicates_by;
let numbers = vec![1, 2, 3, 4];
```
```rust
use lowdash::find_duplicates_by;
#[derive(Debug, Clone, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 25 },
];
Person { name: "Carol".to_string(), age: 25 },
]);
```
### find_duplicates
Find all duplicate elements in a collection (elements that appear more than once).
```rust
use lowdash::find_duplicates;
let numbers = vec![1, 2, 2, 3, 3, 4];
let result = find_duplicates(&numbers);
assert_eq!(result, vec![2, 3]);
```
```rust
use lowdash::find_duplicates;
let words = vec!["apple", "banana", "apple", "cherry", "banana"];
let result = find_duplicates(&words);
assert_eq!(result, vec!["apple", "banana"]);
```
```rust
use lowdash::find_duplicates;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
];
let result = find_duplicates(&people);
assert_eq!(result, vec![Person { name: "Alice".to_string(), age: 25 }]);
```
### find_index_of
Find the first item in a collection that satisfies a predicate and return its index.
```rust
use lowdash::find_index_of;
let numbers = vec![1, 2, 3, 4, 5];
use lowdash::find_index_of;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
```
### find_key_by
Find the key in a map that satisfies a predicate based on both key and value.
```rust
use lowdash::find_key_by;
let mut map = std::collections::HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
Find the key in a map that corresponds to a given value.
```rust
use lowdash::find_key;
let mut map = std::collections::HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = find_key(&map, 2);
assert_eq!(result, Some(&"b"));
```
```rust
use lowdash::find_key;
let mut map = std::collections::HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = find_key(&map, 4);
assert_eq!(result, None);
```
```rust
use lowdash::find_key;
#[derive(Debug, PartialEq, Eq, Hash)]
struct Person {
name: String,
age: u32,
}
let mut map = std::collections::HashMap::new();
map.insert(Person { name: "Alice".to_string(), age: 25 }, "Engineer");
map.insert(Person { name: "Bob".to_string(), age: 30 }, "Manager");
map.insert(Person { name: "Carol".to_string(), age: 35 }, "Director");
let result = find_key(&map, "Manager");
assert_eq!(result, Some(&Person { name: "Bob".to_string(), age: 30 }));
```
### find_last_index_of
Find the last item in a collection that satisfies a predicate and return its index.
```rust
use lowdash::find_last_index_of;
let numbers = vec![1, 2, 3, 4, 5, 3];
use lowdash::find_last_index_of;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 35 },
];
let result = find_last_index_of(&people, |p| p.age > 30);
assert_eq!(result, Some((&Person { name: "Dave".to_string(), age: 35 }, 3)));
```
### find_or_else
Find the first item in a collection that satisfies a predicate.
```rust
use lowdash::find_or_else;
let numbers = vec![1, 2, 3, 4, 5];
use lowdash::find_or_else;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let fallback = Person { name: "Unknown".to_string(), age: 0 };
```
### find_uniques
Find all unique elements in a collection (elements that appear exactly once).
```rust
use lowdash::find_uniques;
let numbers = vec![1, 2, 2, 3, 3, 4];
let result = find_uniques(&numbers);
assert_eq!(result, vec![1, 4]);
```
```rust
use lowdash::find_uniques;
let words = vec!["apple", "banana", "apple", "cherry"];
let result = find_uniques(&words);
assert_eq!(result, vec!["banana", "cherry"]);
```
```rust
use lowdash::find_uniques;
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
];
let result = find_uniques(&people);
assert_eq!(result, vec![Person { name: "Bob".to_string(), age: 30 }]);
```
### find_uniques_by
Find all unique elements in a collection based on a key generated by the iteratee function.
```rust
use lowdash::find_uniques_by;
let numbers = vec![1, 2, 3, 4];
```
```rust
use lowdash::find_uniques_by;
#[derive(Debug, Clone, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 25 },
];
```
### find
Find the first item in a collection that satisfies a predicate.
```rust
use lowdash::find;
let numbers = vec![1, 2, 3, 4, 5];
use lowdash::find;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
```
### first_or_empty
Returns the first item from the collection.
```rust
use lowdash::first_or_empty;
let numbers = vec![1, 2, 3];
let first_num = first_or_empty(&numbers);
assert_eq!(first_num, 1);
let empty: Vec<i32> = vec![];
let first_num = first_or_empty(&empty);
assert_eq!(first_num, 0); // i32::default() is 0
```
```rust
use lowdash::first_or_empty;
#[derive(Debug, PartialEq, Clone, Default)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let first_person = first_or_empty(&people);
assert_eq!(first_person, Person { name: "Alice".to_string(), age: 25 });
let empty_people: Vec<Person> = vec![];
let first_person = first_or_empty(&empty_people);
assert_eq!(first_person, Person::default());
```
### first_or
Returns the first item from the collection.
```rust
use lowdash::first_or;
let numbers = vec![1, 2, 3];
let first_num = first_or(&numbers, 10);
assert_eq!(first_num, 1);
let empty: Vec<i32> = vec![];
let first_num = first_or(&empty, 10);
assert_eq!(first_num, 10);
```
```rust
use lowdash::first_or;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let first_person = first_or(&people, Person { name: "Default".to_string(), age: 0 });
assert_eq!(first_person, Person { name: "Alice".to_string(), age: 25 });
let empty_people: Vec<Person> = vec![];
let first_person = first_or(&empty_people, Person { name: "Default".to_string(), age: 0 });
assert_eq!(first_person, Person { name: "Default".to_string(), age: 0 });
```
### first
Returns the first item from the collection.
```rust
use lowdash::first;
let numbers = vec![1, 2, 3];
let (first_num, exists) = first(&numbers);
assert_eq!(first_num, 1);
assert!(exists);
let empty: Vec<i32> = vec![];
let (first_num, exists) = first(&empty);
assert_eq!(first_num, 0); // i32::default() is 0
assert!(!exists);
```
### index_of
Finds the position of the first occurrence of an element in a collection.
```rust
use lowdash::index_of;
let collection = vec![1, 2, 3, 4, 5];
let index = index_of(&collection, 3);
assert_eq!(index, 2);
```
```rust
use lowdash::index_of;
let collection = vec!["apple", "banana", "cherry"];
let index = index_of(&collection, "banana");
assert_eq!(index, 1);
```
```rust
use lowdash::index_of;
let collection = vec![1, 2, 3, 4, 5];
let index = index_of(&collection, 6);
assert_eq!(index, -1);
```
### kebab_case
Converts a string to kebab-case.
```rust
use lowdash::kebab_case;
assert_eq!(kebab_case("hello world"), "hello-world");
assert_eq!(kebab_case("foo-bar"), "foo-bar");
assert_eq!(kebab_case("lorem_ipsum"), "lorem-ipsum");
assert_eq!(kebab_case("FooBarBazHello"), "foo-bar-baz-hello");
assert_eq!(kebab_case("helloWorld"), "hello-world");
```
### last_index_of
Finds the position of the last occurrence of an element in a collection.
```rust
use lowdash::last_index_of;
let collection = vec![1, 2, 3, 2, 1];
let index = last_index_of(&collection, 2);
assert_eq!(index, 3);
```
```rust
use lowdash::last_index_of;
let collection = vec!["apple", "banana", "cherry", "banana"];
let index = last_index_of(&collection, "banana");
assert_eq!(index, 3);
```
```rust
use lowdash::last_index_of;
let collection = vec![1, 2, 3, 4, 5];
let index = last_index_of(&collection, 6);
assert_eq!(index, -1);
```
### last_or_empty
Returns the last item from the collection.
```rust
use lowdash::last_or_empty;
let numbers = vec![1, 2, 3];
let last_num = last_or_empty(&numbers);
assert_eq!(last_num, 3);
let empty: Vec<i32> = vec![];
let last_num = last_or_empty(&empty);
assert_eq!(last_num, 0); // i32::default() is 0
```
```rust
use lowdash::last_or_empty;
#[derive(Debug, PartialEq, Clone, Default)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let last_person = last_or_empty(&people);
assert_eq!(last_person, Person { name: "Bob".to_string(), age: 30 });
let empty_people: Vec<Person> = vec![];
let last_person = last_or_empty(&empty_people);
assert_eq!(last_person, Person::default());
```
### last_or
Returns the last item from the collection.
```rust
use lowdash::last_or;
let numbers = vec![1, 2, 3];
let last_num = last_or(&numbers, 10);
assert_eq!(last_num, 3);
let empty: Vec<i32> = vec![];
let last_num = last_or(&empty, 10);
assert_eq!(last_num, 10);
```
### last
Returns the last item from the collection.
```rust
use lowdash::last;
let numbers = vec![1, 2, 3];
let (last_num, exists) = last(&numbers);
assert_eq!(last_num, 3);
assert!(exists);
let empty: Vec<i32> = vec![];
let (last_num, exists) = last(&empty);
assert_eq!(last_num, 0); // i32::default() is 0
assert!(!exists);
```
```rust
use lowdash::last;
#[derive(Debug, PartialEq, Clone, Default)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let (last_person, exists) = last(&people);
assert_eq!(last_person, Person { name: "Bob".to_string(), age: 30 });
assert!(exists);
let empty_people: Vec<Person> = vec![];
let (last_person, exists) = last(&empty_people);
assert_eq!(last_person, Person::default());
assert!(!exists);
```
### latest_by
Returns the item from the collection for which the iteratee returns the latest `SystemTime`.
```rust
use std::time::{SystemTime, Duration};
use lowdash::latest_by;
#[derive(Debug, PartialEq, Clone)]
struct Record {
id: u32,
timestamp: SystemTime,
}
impl Default for Record {
fn default() -> Self {
Record {
id: 0,
timestamp: SystemTime::UNIX_EPOCH,
}
}
}
let records = vec![
Record {
id: 1,
timestamp: SystemTime::UNIX_EPOCH + Duration::new(100, 0),
},
Record {
id: 2,
timestamp: SystemTime::UNIX_EPOCH + Duration::new(200, 0),
},
Record {
id: 3,
timestamp: SystemTime::UNIX_EPOCH + Duration::new(150, 0),
},
];
```
### latest
Returns the latest `SystemTime` from the provided arguments.
```rust
use std::time::{SystemTime, Duration};
use lowdash::latest;
let now = SystemTime::now();
let later = now + Duration::new(10, 0);
let latest_time = latest(&[now, later]);
assert_eq!(latest_time, later);
```
### max_by
Find the maximum element in a collection based on a custom comparison function.
```rust
use lowdash::max_by;
#[derive(Debug, PartialEq, Eq, Clone)]
struct Person {
age: u32,
name: String,
}
let people = vec![
Person { age: 25, name: "Alice".to_string() },
Person { age: 30, name: "Bob".to_string() },
Person { age: 20, name: "Carol".to_string() },
];
let result = max_by(&people, |a, b| a.age > b.age);
assert_eq!(
result,
Some(Person { age: 30, name: "Bob".to_string() })
);
```
### max
Find the maximum element in a collection.
```rust
use lowdash::max;
let numbers = vec![5, 3, 8, 1, 4];
let result = max(&numbers);
assert_eq!(result, Some(8));
```
```rust
use lowdash::max;
let strings = vec!["apple", "banana", "cherry"];
let result = max(&strings);
assert_eq!(result, Some("cherry"));
```
```rust
use lowdash::max;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Cia".to_string(), age: 20 },
];
let result = max(&people);
assert_eq!(
result,
Some(Person { name: "Cia".to_string(), age: 20 })
);
```
```rust
use lowdash::max;
let collection = vec![3.14, 2.71, -1.0, 0.0];
let result = max(&collection);
assert_eq!(result, Some(3.14));
```
### min_by
Find the minimum element in a collection based on a custom comparison function.
```rust
use lowdash::min_by;
let numbers = vec![5, 3, 8, 1, 4];
let min = min_by(&numbers, |a, b| a < b);
assert_eq!(min, Some(1));
let strings = vec!["apple", "banana", "cherry"];
let min = min_by(&strings, |a, b| a.len() < b.len());
assert_eq!(min, Some("apple"));
let empty: Vec<i32> = vec![];
let min = min_by(&empty, |a, b| a < b);
assert_eq!(min, None);
```
### min
Find the minimum element in a collection.
```rust
use lowdash::min;
let numbers = vec![5, 3, 8, 1, 4];
let result = min(&numbers);
assert_eq!(result, Some(1));
```
```rust
use lowdash::min;
let strings = vec!["apple", "banana", "cherry"];
let result = min(&strings);
assert_eq!(result, Some("apple"));
```
```rust
use lowdash::min;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 20 },
];
let result = min(&people);
assert_eq!(
result,
Some(Person { name: "Alice".to_string(), age: 25 })
);
```
```rust
use lowdash::min;
let collection = vec![3.14, 2.71, -1.0, 0.0];
let result = min(&collection);
assert_eq!(result, Some(-1.0));
```
### nearest_power_of_two
Calculates the smallest power of two greater than or equal to the given capacity.
```rust
use lowdash::nearest_power_of_two;
assert_eq!(nearest_power_of_two(0), 1);
assert_eq!(nearest_power_of_two(1), 1);
assert_eq!(nearest_power_of_two(5), 8);
assert_eq!(nearest_power_of_two(16), 16);
assert_eq!(nearest_power_of_two(17), 32);
```
### nth
Returns the nth element from the collection.
```rust
use lowdash::nth;
let numbers = vec![1, 2, 3, 4, 5];
let result = nth(&numbers, 2);
assert_eq!(result.unwrap(), &3);
let result = nth(&numbers, -2);
assert_eq!(result.unwrap(), &4);
let result = nth(&numbers, 10);
assert!(result.is_err());
```
### pascal_case
Converts a string to PascalCase.
```rust
use lowdash::pascal_case;
assert_eq!(pascal_case("hello world"), "HelloWorld");
assert_eq!(pascal_case("foo-bar"), "FooBar");
assert_eq!(pascal_case("lorem_ipsum"), "LoremIpsum");
```
### random_string
Generates a random string of a specified size using the provided charset.
```rust
use lowdash::common::ALPHANUMERIC_CHARSET;
use lowdash::random_string;
let charset = ALPHANUMERIC_CHARSET;
let random_str = random_string(10, charset);
assert_eq!(random_str.len(), 10);
for c in random_str.chars() {
assert!(charset.contains(&c));
}
```
### sample
Returns a pseudo-random element from the collection.
```rust
use lowdash::sample;
let numbers = vec![1, 2, 3, 4, 5];
let result = sample(&numbers);
assert!(numbers.contains(&result));
let empty: Vec<i32> = vec![];
let result = sample(&empty);
assert_eq!(result, 0); // i32::default()
```
### samples
Returns a slice of pseudo-randomly selected elements from the collection.
```rust
use lowdash::samples;
let numbers = vec![1, 2, 3, 4, 5];
let result = samples(&numbers, 3);
assert_eq!(result.len(), 3);
### snake_case
Converts a string to snake_case.
```rust
use lowdash::snake_case;
assert_eq!(snake_case("hello world"), "hello_world");
assert_eq!(snake_case("foo-bar"), "foo_bar");
assert_eq!(snake_case("lorem_ipsum"), "lorem_ipsum");
assert_eq!(snake_case("FooBarBazHello"), "foo_bar_baz_hello");
assert_eq!(snake_case("fooBarBazHello"), "foo_bar_baz_hello");
```
### substring
Extracts a substring from the given string based on the specified offset and length.
```rust
use lowdash::substring;
let s = String::from("Hello, World!");
assert_eq!(substring(&s, 7, 5), "World");
let s = String::from("Hello, World!");
assert_eq!(substring(&s, -6, 5), "World");
let s = String::from("Hello, World!");
assert_eq!(substring(&s, 100, 5), "");
let s = String::from("Hello\x00World!");
assert_eq!(substring(&s, 0, 10), "HelloWorld");
```
### words
Splits a string into words based on casing, digits, and separators.
```rust
use lowdash::words;
let result = words("Int8Value");
assert_eq!(result, vec!["Int", "8", "Value"]);
let result = words("hello_world");
assert_eq!(result, vec!["hello", "world"]);
let result = words("fooBarBazHello");
assert_eq!(result, vec!["foo", "Bar", "Baz", "Hello"]);
```
### reject
Reject items from a collection that satisfy a predicate.
```rust
use lowdash::reject;
let numbers = vec![1, 2, 3, 4, 5];
use lowdash::filter;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
use lowdash::map;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
use lowdash::filter_map;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
// Extract names of people older than 28
use lowdash::flat_map;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
hobbies: Vec<String>,
}
let people = vec![
Person {
name: "Alice".to_string(),
hobbies: vec!["Reading".to_string(), "Cycling".to_string()],
},
Person {
name: "Bob".to_string(),
hobbies: vec!["Cooking".to_string()],
},
];
// Extract all hobbies from the list of people
use lowdash::reduce;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
// Concatenate all names
use lowdash::reduce_right;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
// Concatenate all names in reverse order
use lowdash::foreach;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let mut names = Vec::new();
use lowdash::foreach_while;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let mut names = Vec::new();
Generates a collection by invoking the provided function `iteratee` a specified number of times.
```rust
use lowdash::times;
```
```rust
use lowdash::times;
```
### uniq
Remove duplicate elements from a collection, preserving the order of their first occurrence.
```rust
use lowdash::uniq;
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
let unique_numbers = uniq(&numbers);
assert_eq!(unique_numbers, vec![1, 2, 3, 4, 5]);
```
```rust
use lowdash::uniq;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
];
let unique_people = uniq(&people);
assert_eq!(unique_people, vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
]);
```
### uniq_by
Remove duplicate elements from a collection based on a key extracted by a provided function,
preserving the order of their first occurrence.
```rust
use lowdash::uniq_by;
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
Group elements of a collection based on a key extracted by a provided function,
preserving the order of their first occurrence.
```rust
use lowdash::group_by;
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
let grouped = group_by(&numbers, |x| *x % 2 == 0);
assert_eq!(grouped.get(&false), Some(&vec![1, 3, 3, 5]));
assert_eq!(grouped.get(&true), Some(&vec![2, 2, 4]));
```
```rust
use lowdash::group_by;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 25 },
];
let grouped = group_by(&people, |p| p.age);
assert_eq!(grouped.get(&25), Some(&vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 25 },
]));
assert_eq!(grouped.get(&30), Some(&vec![
Person { name: "Bob".to_string(), age: 30 },
]));
```
### chunk
Divide a collection into smaller chunks of a specified size,
preserving the order of elements.
```rust
use lowdash::chunk;
let numbers = vec![1, 2, 3, 4, 5, 6, 7];
let chunks = chunk(&numbers, 3);
assert_eq!(
chunks,
vec![vec![1, 2, 3], vec![4, 5, 6], vec![7]]
);
```
```rust
use lowdash::chunk;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
];
let chunks = chunk(&people, 2);
assert_eq!(
chunks,
vec![
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
],
vec![
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
],
]
);
```
### partition_by
Divide a collection into partitions based on a key extracted by a provided function,
preserving the order of elements and the order of partitions as they first appear.
```rust
use lowdash::partition_by;
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
let partitions = partition_by(&numbers, |x| *x);
assert_eq!(
partitions,
vec![vec![1], vec![2, 2], vec![3, 3], vec![4], vec![5]]
);
```
```rust
use lowdash::partition_by;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
];
let partitions = partition_by(&people, |p| p.age);
assert_eq!(
partitions,
vec![
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Alice".to_string(), age: 25 },
],
vec![
Person { name: "Bob".to_string(), age: 30 },
],
vec![
Person { name: "Carol".to_string(), age: 35 },
],
]
);
```
### flatten
Flatten a collection of slices into a single vector, preserving the order of elements.
```rust
use lowdash::flatten;
let nested = vec![vec![1, 2], vec![3, 4], vec![5]];
let flat = flatten(&nested);
assert_eq!(flat, vec![1, 2, 3, 4, 5]);
```
```rust
use lowdash::flatten;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people_groups = vec![
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
],
vec![
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
],
];
let flat_people = flatten(&people_groups);
assert_eq!(
flat_people,
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
]
);
```
### interleave
Interleave multiple collections into a single vector, preserving the order of elements.
```rust
use lowdash::interleave;
let a = vec![1, 2, 3];
let b = vec![4, 5, 6, 7];
let c = vec![8, 9];
let result = interleave(&[&a[..], &b[..], &c[..]]);
assert_eq!(result, vec![1, 4, 8, 2, 5, 9, 3, 6, 7]);
```
```rust
use lowdash::interleave;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let group1 = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
];
let group2 = vec![
Person { name: "Carol".to_string(), age: 35 },
];
let group3 = vec![
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Eve".to_string(), age: 45 },
Person { name: "Frank".to_string(), age: 50 },
];
let interleaved = interleave(&[&group1[..], &group2[..], &group3[..]]);
assert_eq!(
interleaved,
vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Eve".to_string(), age: 45 },
Person { name: "Frank".to_string(), age: 50 },
]
);
```
### shuffle
Shuffle a collection, returning a new vector with the elements in random order.
```rust
use lowdash::shuffle;
let numbers = vec![1, 2, 3, 4, 5];
let shuffled = shuffle(&numbers);
assert_eq!(shuffled.len(), numbers.len());
assert!(shuffled.contains(&1));
assert!(shuffled.contains(&2));
assert!(shuffled.contains(&3));
assert!(shuffled.contains(&4));
assert!(shuffled.contains(&5));
```
### reverse
Reverse a collection, returning a new vector with the elements in reverse order.
```rust
use lowdash::reverse;
let numbers = vec![1, 2, 3, 4, 5];
let reversed = reverse(&numbers);
assert_eq!(reversed, vec![5, 4, 3, 2, 1]);
```
```rust
use lowdash::reverse;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let reversed_people = reverse(&people);
assert_eq!(reversed_people, vec![
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
]);
```
### fill
Fill a collection with a specified value, returning a new vector with all elements set to the initial value.
```rust
use lowdash::fill;
let numbers = vec![1, 2, 3, 4, 5];
let filled = fill(&numbers, 0);
assert_eq!(filled, vec![0, 0, 0, 0, 0]);
```
```rust
use lowdash::fill;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let filled_people = fill(&people, Person { name: "Dave".to_string(), age: 40 });
assert_eq!(
filled_people,
vec![
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Dave".to_string(), age: 40 },
]
);
```
### repeat
Fill a collection with a specified value, returning a new vector with all elements set to the initial value.
```rust
use lowdash::repeat;
let filled = repeat(5, 0);
assert_eq!(filled, vec![0, 0, 0, 0, 0]);
```
```rust
use lowdash::repeat;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let filled_people = repeat(3, Person { name: "Dave".to_string(), age: 40 });
assert_eq!(
filled_people,
vec![
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Dave".to_string(), age: 40 },
Person { name: "Dave".to_string(), age: 40 },
]
);
```
### repeat_by
Repeat a specified value `count` times by applying a predicate function to each index, returning a new vector with the generated elements.
```rust
use lowdash::repeat_by;
let filled = repeat_by(5, |i| i * 2);
assert_eq!(filled, vec![0, 2, 4, 6, 8]);
```
```rust
use lowdash::repeat_by;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
age: 20 + i as u32 * 5,
});
assert_eq!(
filled_people,
vec![
Person { name: "Person 1".to_string(), age: 20 },
Person { name: "Person 2".to_string(), age: 25 },
Person { name: "Person 3".to_string(), age: 30 },
]
);
```
### key_by
Creates a `HashMap` by mapping each element in a collection to a key using an iteratee function.
```rust
use lowdash::key_by;
use std::collections::HashMap;
let numbers = vec![1, 2, 3, 4, 5];
let map = key_by(&numbers, |&x| x % 2);
let mut expected = HashMap::new();
expected.insert(1, 5); // Last odd number
expected.insert(0, 4); // Last even number
assert_eq!(map, expected);
```
```rust
use lowdash::key_by;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Charlie".to_string(), age: 35 },
];
let map = key_by(&people, |person| person.name.clone());
let mut expected = HashMap::new();
expected.insert("Alice".to_string(), people[0].clone());
expected.insert("Bob".to_string(), people[1].clone());
expected.insert("Charlie".to_string(), people[2].clone());
assert_eq!(map, expected);
```
```rust
use lowdash::key_by;
use std::collections::HashMap;
let strings = vec!["apple", "banana", "apricot", "blueberry"];
let map = key_by(&strings, |s| s.chars().next().unwrap());
let mut expected = HashMap::new();
expected.insert('a', "apricot");
expected.insert('b', "blueberry");
assert_eq!(map, expected);
```
### associate
Creates a `HashMap` by transforming each element in a collection into a key-value pair using a provided function.
```rust
use lowdash::associate;
use std::collections::HashMap;
let numbers = vec![1, 2, 3, 4, 5];
expected.insert(1, 1);
expected.insert(2, 4);
expected.insert(3, 9);
expected.insert(4, 16);
expected.insert(5, 25);
assert_eq!(map, expected);
```
```rust
use lowdash::associate;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Charlie".to_string(), age: 35 },
];
expected.insert("Alice".to_string(), 25);
expected.insert("Bob".to_string(), 30);
expected.insert("Charlie".to_string(), 35);
assert_eq!(map, expected);
```
```rust
use lowdash::associate;
use std::collections::HashMap;
let strings = vec!["apple", "banana", "apricot", "blueberry"];
expected.insert('a', 7); // "apricot" has 7 characters
expected.insert('b', 9); // "blueberry" has 9 characters
assert_eq!(map, expected);
```
### slice_to_map
Transforms a slice of items into a `HashMap` by applying a provided function to each item.
```rust
use lowdash::slice_to_map;
use std::collections::HashMap;
let numbers = vec![1, 2, 3, 4, 5];
expected.insert(1, 1);
expected.insert(2, 4);
expected.insert(3, 9);
expected.insert(4, 16);
expected.insert(5, 25);
assert_eq!(map, expected);
```
```rust
use lowdash::slice_to_map;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Charlie".to_string(), age: 35 },
];
expected.insert("Alice".to_string(), 25);
expected.insert("Bob".to_string(), 30);
expected.insert("Charlie".to_string(), 35);
assert_eq!(map, expected);
```
```rust
use lowdash::slice_to_map;
use std::collections::HashMap;
let strings = vec!["apple", "banana", "apricot", "blueberry"];
expected.insert('a', 7); // "apricot" has 7 characters
expected.insert('b', 9); // "blueberry" has 9 characters
assert_eq!(map, expected);
```
### drop
Removes the first `n` elements from a collection and returns the remaining elements.
```rust
use lowdash::drop;
let numbers = vec![1, 2, 3, 4, 5];
let result = drop(&numbers, 2);
assert_eq!(result, vec![3, 4, 5]);
```
```rust
use lowdash::drop;
let letters = vec!['a', 'b', 'c', 'd'];
let result = drop(&letters, 10);
assert_eq!(result, vec![]);
```
### drop_right
Removes the last `n` elements from a collection and returns the remaining elements.
```rust
use lowdash::drop_right;
let numbers = vec![1, 2, 3, 4, 5];
let result = drop_right(&numbers, 2);
assert_eq!(result, vec![1, 2, 3]);
```
```rust
use lowdash::drop_right;
let letters = vec!['a', 'b', 'c', 'd'];
let result = drop_right(&letters, 10);
assert_eq!(result, vec![]);
```
### drop_while
Removes elements from the beginning of a collection as long as a predicate returns `true`, and returns the remaining elements. As soon as the predicate returns `false`, the function stops dropping elements.
```rust
use lowdash::drop_while;
let numbers = vec![1, 2, 3, 4, 5];
let result = drop_while(&numbers, |&x| x < 3);
assert_eq!(result, vec![3, 4, 5]);
```
```rust
use lowdash::drop_while;
let letters = vec!['a', 'b', 'c', 'd'];
let result = drop_while(&letters, |&c| c < 'c');
assert_eq!(result, vec!['c', 'd']);
```
### drop_right_while
Removes elements from the end of a collection as long as a predicate returns `true`, and returns the remaining elements. As soon as the predicate returns `false`, the function stops dropping elements.
```rust
use lowdash::drop_right_while;
let numbers = vec![1, 2, 3, 4, 5];
```
```rust
use lowdash::drop_right_while;
let letters = vec!['a', 'b', 'c', 'd', 'e'];
```
### drop_by_index
Removes elements from a collection at the specified indices.
Supports negative indices which count from the end of the collection.
Indices that are out of bounds are ignored.
```rust
use lowdash::drop_by_index;
let numbers = vec![1, 2, 3, 4, 5];
let result = drop_by_index(&numbers, &[1, 3]);
assert_eq!(result, vec![1, 3, 5]);
```
```rust
use lowdash::drop_by_index;
let letters = vec!['a', 'b', 'c', 'd', 'e'];
let result = drop_by_index(&letters, &[0, -1]);
assert_eq!(result, vec!['b', 'c', 'd']);
```
### reject_map
Applies a callback function to each item in a collection along with its index and collects the results where the callback returns `false`.
```rust
use lowdash::reject_map;
let numbers = vec![1, 2, 3, 4, 5];
// Collect squares of odd numbers
Filters a collection into two separate vectors based on a predicate function.
```rust
use lowdash::filter_reject;
let numbers = vec![1, 2, 3, 4, 5];
// Separate even and odd numbers
Counts the number of occurrences of a specific value in a collection.
```rust
use lowdash::count;
let numbers = vec![1, 2, 2, 3, 4, 2];
let result = count(&numbers, 2);
assert_eq!(result, 3);
```
```rust
use lowdash::count;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
];
let count_alice = count(&people, Person { name: "Alice".to_string(), age: 25 });
assert_eq!(count_alice, 2);
```
### count_by
Counts the number of elements in a collection that satisfy a given predicate.
```rust
use lowdash::count_by;
let numbers = vec![1, 2, 3, 4, 5];
// Count even numbers
let result = count_by(&numbers, |&x| x % 2 == 0);
assert_eq!(result, 2);
```
```rust
use lowdash::count_by;
#[derive(Debug, PartialEq)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 30 },
];
// Count people who are at least 30 years old
```
### count_values
Counts the number of occurrences of each value in a collection.
```rust
use lowdash::count_values;
use std::collections::HashMap;
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
let result = count_values(&numbers);
let mut expected = HashMap::new();
expected.insert(1, 1);
expected.insert(2, 2);
expected.insert(3, 2);
expected.insert(4, 1);
expected.insert(5, 1);
assert_eq!(result, expected);
```
```rust
use lowdash::count_values;
use std::collections::HashMap;
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
];
let result = count_values(&people);
let mut expected = HashMap::new();
expected.insert(
Person { name: "Alice".to_string(), age: 25 },
2
);
expected.insert(
Person { name: "Bob".to_string(), age: 30 },
1
);
expected.insert(
Person { name: "Carol".to_string(), age: 35 },
1
);
assert_eq!(result, expected);
```
### count_values_by
Counts the number of occurrences of each value in a collection after applying a mapper function.
```rust
use lowdash::count_values_by;
use std::collections::HashMap;
let chars = vec!['a', 'b', 'a', 'c', 'b', 'd'];
expected.insert('a', 2);
expected.insert('b', 2);
expected.insert('c', 1);
expected.insert('d', 1);
assert_eq!(result, expected);
```
```rust
use lowdash::count_values_by;
use std::collections::HashMap;
let numbers = vec![1, 2, 2, 3, 4, 3, 5];
Returns a subset of the collection based on the provided start and end indices.
```rust
use lowdash::slice;
let numbers = vec![1, 2, 3, 4, 5];
let result = slice(&numbers, 1, 3);
assert_eq!(result, vec![2, 3]);
```
```rust
use lowdash::slice;
let numbers = vec![1, 2, 3, 4, 5];
let result = slice(&numbers, -3, -1);
assert_eq!(result, vec![3, 4]);
```
```rust
use lowdash::slice;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
Person { name: "Dave".to_string(), age: 40 },
];
let result = slice(&people, 1, 3);
assert_eq!(
result,
vec![
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
]
);
```
### replace
Replaces occurrences of a specified value in a collection with a new value, up to a maximum number of replacements.
```rust
use lowdash::replace;
let numbers = vec![1, 2, 2, 3, 4, 2, 5];
let result = replace(&numbers, 2, 9, 2);
assert_eq!(result, vec![1, 9, 9, 3, 4, 2, 5]);
```
```rust
use lowdash::replace;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
];
let new_person = Person { name: "Dave".to_string(), age: 40 };
let result = replace(&people, people[0].clone(), new_person.clone(), 1);
assert_eq!(result, vec![new_person, people[1].clone(), people[2].clone(), people[3].clone()]);
```
### replace_all
Replaces all occurrences of a specified value in a collection with a new value.
```rust
use lowdash::replace_all;
let numbers = vec![1, 2, 2, 3, 4, 2, 5];
let result = replace_all(&numbers, 2, 9);
assert_eq!(result, vec![1, 9, 9, 3, 4, 9, 5]);
```
```rust
use lowdash::replace_all;
#[derive(Debug, PartialEq, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Carol".to_string(), age: 35 },
];
let dave = Person { name: "Dave".to_string(), age: 40 };
let result = replace_all(&people, people[0].clone(), dave.clone());
assert_eq!(
result,
vec![
dave.clone(),
people[1].clone(),
dave.clone(),
people[3].clone(),
]
);
```
### compact
Removes all zero-valued elements from a collection, preserving the order of non-zero elements.
This function iterates over a slice of items, removing each element that is equal to the zero value.
The zero value is determined by the `Default` trait implementation for the type `T`.
The function preserves the order of the remaining elements and does not modify the original collection.
**Time Complexity:** O(n), where n is the number of elements in the collection.
# Arguments
* `collection` - A slice of items from which to remove zero-valued elements.
# Type Parameters
* `T` - The type of elements in the collection. Must implement `PartialEq`, `Clone`, and `Default`.
# Returns
* `Vec<T>` - A new vector containing only the non-zero elements from the input collection.
# Examples
```rust
use lowdash::compact;
let numbers = vec![0, 1, 0, 2, 3, 0, 4];
let compacted = compact(&numbers);
assert_eq!(compacted, vec![1, 2, 3, 4]);
```
```rust
use lowdash::compact;
#[derive(Debug, PartialEq, Clone, Default)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "".to_string(), age: 0 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "".to_string(), age: 0 },
Person { name: "Dave".to_string(), age: 40 },
];
let compacted = compact(&people);
assert_eq!(
compacted,
vec![
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Dave".to_string(), age: 40 },
]
);
```
```rust
use lowdash::compact;
let floats = vec![0.0, 1.1, 0.0, 2.2, 3.3, 0.0, 4.4];
let compacted = compact(&floats);
assert_eq!(compacted, vec![1.1, 2.2, 3.3, 4.4]);
```
### is_sorted
Determines if a collection is sorted in ascending order.
```rust
use lowdash::is_sorted;
let numbers = vec![1, 2, 3, 4, 5];
assert_eq!(is_sorted(&numbers), true);
```
```rust
use lowdash::is_sorted;
let numbers = vec![5, 4, 3, 2, 1];
assert_eq!(is_sorted(&numbers), false);
```
```rust
use lowdash::is_sorted;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
assert_eq!(is_sorted(&people), true);
```
```rust
use lowdash::is_sorted;
let floats = vec![1.1, 2.2, 3.3, 4.4];
assert_eq!(is_sorted(&floats), true);
```
```rust
use lowdash::is_sorted;
let floats = vec![1.1, std::f64::NAN, 3.3];
// Note: Any comparison with NaN returns false, so the slice is not considered sorted.
assert_eq!(is_sorted(&floats), false);
```
### is_sorted_by_key
Determines if a collection is sorted in ascending order based on a specified key.
```rust
use lowdash::is_sorted_by_key;
let numbers = vec![
(1, "a"),
(2, "b"),
(3, "c"),
(4, "d"),
];
let result = is_sorted_by_key(&numbers, |item| item.0);
assert_eq!(result, true);
```
```rust
use lowdash::is_sorted_by_key;
let numbers = vec![
(1, "a"),
(3, "b"),
(2, "c"),
(4, "d"),
];
let result = is_sorted_by_key(&numbers, |item| item.0);
assert_eq!(result, false);
```
```rust
use lowdash::is_sorted_by_key;
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Clone)]
struct Person {
name: String,
age: u32,
}
let people = vec![
Person { name: "Alice".to_string(), age: 25 },
Person { name: "Bob".to_string(), age: 30 },
Person { name: "Carol".to_string(), age: 35 },
];
let result = is_sorted_by_key(&people, |p| p.age);
assert_eq!(result, true);
```
```rust
use lowdash::is_sorted_by_key;
let floats = vec![
(1.1, "apple"),
(2.2, "banana"),
(3.3, "cherry"),
(4.4, "date"),
];
let result = is_sorted_by_key(&floats, |item| item.0);
assert_eq!(result, true);
```
### splice
Inserts elements into a collection at a specified index, handling negative indices and overflow.
```rust
use lowdash::splice;
let numbers = vec![1, 2, 3, 4, 5];
let elements = vec![99, 100];
let result = splice(&numbers, 2, &elements);
assert_eq!(result, vec![1, 2, 99, 100, 3, 4, 5]);
```
```rust
use lowdash::splice;
let numbers = vec![1, 2, 3, 4, 5];
let elements = vec![99, 100];
// Insert at the end
let result = splice(&numbers, 10, &elements);
assert_eq!(result, vec![1, 2, 3, 4, 5, 99, 100]);
```
```rust
use lowdash::splice;
let numbers = vec![1, 2, 3, 4, 5];
let elements = vec![99];
// Insert at negative index (-2 means len - 2 = 3)
let result = splice(&numbers, -2, &elements);
assert_eq!(result, vec![1, 2, 3, 99, 4, 5]);
```
```rust
use lowdash::splice;
let numbers = vec![1, 2, 3, 4, 5];
let elements = vec![99];
// Negative index beyond the start, insert at beginning
let result = splice(&numbers, -10, &elements);
assert_eq!(result, vec![99, 1, 2, 3, 4, 5]);
```
### keys
Collects all keys from one or more maps into a single vector.
```rust
use lowdash::keys;
use std::collections::HashMap;
let mut map1 = HashMap::new();
map1.insert(1, "a");
map1.insert(2, "b");
let mut map2 = HashMap::new();
map2.insert(3, "c");
map2.insert(4, "d");
let result = keys(&[&map1, &map2]);
assert_eq!(result.len(), 4);
assert!(result.contains(&1));
assert!(result.contains(&2));
assert!(result.contains(&3));
assert!(result.contains(&4));
```
### uniq_keys
Collects all unique keys from one or more maps into a single vector.
```rust
use lowdash::uniq_keys;
use std::collections::HashMap;
let mut map1 = HashMap::new();
map1.insert("a", 1);
map1.insert("b", 2);
let mut map2 = HashMap::new();
map2.insert("b", 3);
map2.insert("c", 4);
let result = uniq_keys(&[&map1, &map2]);
assert_eq!(result.len(), 3);
assert!(result.contains(&"a"));
assert!(result.contains(&"b"));
assert!(result.contains(&"c"));
```
### has_key
Checks if a map contains a specific key.
```rust
use lowdash::has_key;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
assert!(has_key(&map, &"a"));
assert!(!has_key(&map, &"c"));
```
### values
Collects all values from one or more maps into a single vector.
```rust
use lowdash::values;
use std::collections::HashMap;
let mut map1 = HashMap::new();
map1.insert("a", 1);
map1.insert("b", 2);
let mut map2 = HashMap::new();
map2.insert("c", 3);
map2.insert("d", 4);
let result = values(&[&map1, &map2]);
assert_eq!(result.len(), 4);
assert!(result.contains(&1));
assert!(result.contains(&2));
assert!(result.contains(&3));
assert!(result.contains(&4));
```
### uniq_values
Collects all unique values from one or more maps into a single vector.
```rust
use lowdash::uniq_values;
use std::collections::HashMap;
let mut map1 = HashMap::new();
map1.insert("a", 1);
map1.insert("b", 2);
let mut map2 = HashMap::new();
map2.insert("c", 2);
map2.insert("d", 3);
let result = uniq_values(&[&map1, &map2]);
assert_eq!(result.len(), 3);
assert!(result.contains(&1));
assert!(result.contains(&2));
assert!(result.contains(&3));
```
### value_or
Returns a value from a map for a given key, or a fallback value if the key doesn't exist.
```rust
use lowdash::value_or;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
assert_eq!(value_or(&map, &"a", 42), 1);
assert_eq!(value_or(&map, &"b", 42), 42);
```
### pick_by
Filters a map by applying a predicate to its key-value pairs.
```rust
use lowdash::pick_by;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
Filters a map by selecting only the specified values.
```rust
use lowdash::pick_by_values;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let values = vec![1, 3];
let result = pick_by_values(&map, &values);
assert_eq!(result.len(), 2);
assert!(result.contains_key("a"));
assert!(result.contains_key("c"));
```
### omit_by
Filters a map by omitting key-value pairs that satisfy a predicate.
```rust
use lowdash::omit_by;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
Filters a map by omitting key-value pairs that have values present in the provided values slice.
```rust
use lowdash::omit_by_values;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let values = vec![2, 4];
let result = omit_by_values(&map, &values);
assert_eq!(result.len(), 2);
assert!(result.contains_key("a"));
assert!(result.contains_key("c"));
assert!(!result.contains_key("b"));
```
### entries
Collects all entries from a map into a vector of `Entry` structs.
```rust
use lowdash::{Entry, entries};
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
let result = entries(&map);
let expected = vec![
Entry { key: "a", value: 1 },
Entry { key: "b", value: 2 },
];
let mut sorted_result = result.clone();
let mut sorted_expected = expected.clone();
sorted_expected.sort_by(|a, b| a.key.cmp(&b.key));
assert_eq!(sorted_result, sorted_expected);
```
### to_pairs
Collects all entries from a map into a vector of `Entry` structs.
```rust
use lowdash::{Entry, to_pairs};
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
let result = to_pairs(&map);
let expected = vec![
Entry { key: "a", value: 1 },
Entry { key: "b", value: 2 },
];
let mut sorted_result = result.clone();
let mut sorted_expected = expected.clone();
sorted_expected.sort_by(|a, b| a.key.cmp(&b.key));
assert_eq!(sorted_result, sorted_expected);
```
### from_entries
Constructs a `HashMap` from a slice of `Entry` structs.
```rust
use lowdash::{Entry, from_entries};
use std::collections::HashMap;
let entries = vec![
Entry { key: "a", value: 1 },
Entry { key: "b", value: 2 },
];
let result = from_entries(&entries);
let mut expected = HashMap::new();
expected.insert("a", 1);
expected.insert("b", 2);
assert_eq!(result.len(), expected.len());
for (key, value) in &expected {
assert_eq!(result.get(key), Some(value));
}
```
### from_pairs
Constructs a `HashMap` from a slice of `Entry` structs.
```rust
use lowdash::{Entry, from_pairs};
use std::collections::HashMap;
let entries = vec![
Entry { key: "a", value: 1 },
Entry { key: "b", value: 2 },
];
let result = from_pairs(&entries);
let mut expected = HashMap::new();
expected.insert("a", 1);
expected.insert("b", 2);
assert_eq!(result.len(), expected.len());
for (key, value) in &expected {
assert_eq!(result.get(key), Some(value));
}
```
### invert
Constructs a `HashMap` by inverting the keys and values of the input map.
```rust
use lowdash::{invert, Entry};
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
map.insert("c", 3);
let result = invert(&map);
let mut expected = HashMap::new();
expected.insert(1, "a");
expected.insert(2, "b");
expected.insert(3, "c");
assert_eq!(result.len(), expected.len());
for (key, value) in &expected {
assert_eq!(result.get(key), Some(value));
}
```
### assign
Merges multiple maps into a single map. If the same key exists in multiple maps, the value from the last map is used.
```rust
use lowdash::assign;
use std::collections::HashMap;
let mut map1 = HashMap::new();
map1.insert("a", 1);
let mut map2 = HashMap::new();
map2.insert("b", 2);
let merged = assign(&[map1, map2]);
assert_eq!(merged.get("a"), Some(&1));
assert_eq!(merged.get("b"), Some(&2));
```
### map_values
Transforms the values of a map using a provided function.
```
use lowdash::map_values;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
Transforms the entries of a map using a provided function.
```rust
use lowdash::map_entries;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
let transformed = map_entries(&map, |k, v| (k.to_uppercase(), v * 10));
assert_eq!(transformed.get("A"), Some(&10));
assert_eq!(transformed.get("B"), Some(&20));
```
### map_to_slice
Transforms the entries of a map into a slice using a provided function.
```rust
use lowdash::map_to_slice;
use std::collections::HashMap;
let mut map = HashMap::new();
map.insert("a", 1);
map.insert("b", 2);
assert!(transformed.contains(&"b:2".to_string()));
```
### range
Generate a range of integers from 0 to `element_num` (exclusive).
If `element_num` is negative, generate a range from 0 to `-element_num` (exclusive) with a step of -1.
```rust
use lowdash::range;
let result = range(5);
assert_eq!(result, vec![0, 1, 2, 3, 4]);
```
```rust
use lowdash::range;
let result = range(-5);
assert_eq!(result, vec![0, -1, -2, -3, -4]);
```
### range_from
Generate a range of numbers starting from a given value.
If `element_num` is negative, generate a range with a step of -1.
```rust
use lowdash::range_from;
let result = range_from(5, 3);
assert_eq!(result, vec![5, 6, 7]);
```
```rust
use lowdash::range_from;
let result = range_from(5, -3);
assert_eq!(result, vec![5, 4, 3]);
```
### range_with_step
Generate a range of numbers from start to end (exclusive) with a specified step.
```rust
use lowdash::range_with_steps;
let result = range_with_steps(1, 5, 1);
assert_eq!(result, vec![1, 2, 3, 4]);
```
```rust
use lowdash::range_with_steps;
let result = range_with_steps(5.0, 2.0, -1.0);
assert_eq!(result, vec![5.0, 4.0, 3.0]);
```
```rust
use lowdash::range_with_steps;
let result = range_with_steps(1, 1, 1); // Empty range
assert_eq!(result, Vec::<i32>::new());
```
### clamp
Clamps a value between a minimum and maximum value.
If the value is less than the minimum, returns the minimum.
If the value is greater than the maximum, returns the maximum.
Otherwise, returns the value unchanged.
```rust
use lowdash::clamp;
assert_eq!(clamp(5, 0, 10), 5); // Value within range
assert_eq!(clamp(-5, 0, 10), 0); // Value below minimum
assert_eq!(clamp(15, 0, 10), 10); // Value above maximum
```
### sum
Calculates the sum of all elements in a collection.
```rust
use lowdash::sum;
// Integer sum
assert_eq!(sum(&[1, 2, 3, 4, 5]), 15);
// Float sum
assert_eq!(sum(&[1.1, 2.2, 3.3]), 6.6);
```
### sum_by
Calculates the sum of values obtained by applying a function to each element in a collection.
```rust
use lowdash::sum_by;
let numbers = vec![1, 2, 3, 4];
let result = sum_by(&numbers, |x| x * 2);
assert_eq!(result, 20); // (1*2 + 2*2 + 3*2 + 4*2)
```
### product
Calculate the product of all elements in a collection.
If the collection is empty, returns 1 (multiplicative identity).
```rust
use lowdash::product;
// Integer product
let numbers = vec![1, 2, 3, 4, 5];
assert_eq!(product(&numbers), 120);
```
```rust
use lowdash::product;
// Float product
let numbers = vec![1.5, 2.0, 3.0];
assert_eq!(product(&numbers), 9.0);
```
```rust
use lowdash::product;
// Empty collection returns 1
let empty: Vec<i32> = vec![];
assert_eq!(product(&empty), 1);
```
### product_by
Calculate the product of values obtained by applying a function to each element in a collection.
If the collection is empty, returns 1 (multiplicative identity).
```rust
use lowdash::product_by;
let numbers = vec![1, 2, 3, 4];
let result = product_by(&numbers, |x| x * 2);
assert_eq!(result, 384); // (1*2) * (2*2) * (3*2) * (4*2)
```
```rust
use lowdash::product_by;
#[derive(Debug)]
struct Rectangle {
width: f64,
height: f64,
}
let rectangles = vec![
Rectangle { width: 2.0, height: 3.0 },
Rectangle { width: 4.0, height: 5.0 },
];
```
### mean
Calculates the arithmetic mean of a collection of numbers.
If the collection is empty, returns zero.
```rust
use lowdash::mean;
let numbers = vec![1.0, 2.0, 3.0, 4.0, 5.0];
let result = mean(&numbers);
assert_eq!(result, 3.0);
```
```rust
use lowdash::mean;
let numbers = vec![1, 2, 3, 4, 5];
let result = mean(&numbers);
assert_eq!(result, 3);
```
```rust
use lowdash::mean;
let empty: Vec<f64> = vec![];
let result = mean(&empty);
assert_eq!(result, 0.0);
```
### mean_by
Calculates the mean value of a collection after applying a transformation function to each element.
```rust
use lowdash::mean_by;
let objects = vec![(1, 4), (2, 6), (3, 8)];
let mean_y = mean_by(&objects, |&(_, y)| y as f64);
assert!((mean_y - 6.0).abs() < f64::EPSILON);
```
### percentile
Calculates the specified percentile of a collection.
The percentile should be a value between 0 and 100.
The collection will be sorted before calculation.
Uses linear interpolation between closest ranks for non-integer results.
```rust
use lowdash::percentile;
let numbers = vec![1, 2, 3, 4, 5];
let result = percentile(&numbers, 50.0);
assert!((result.unwrap() - 3.0).abs() < f64::EPSILON);
```
```rust
use lowdash::percentile;
let numbers = vec![1, 2, 3, 4];
let result = percentile(&numbers, 75.0);
assert!((result.unwrap() - 3.25).abs() < f64::EPSILON);
```
### median
Calculate the median value of a collection.
The median is the 50th percentile of a collection.
For collections with an even number of elements, the median is the average of the two middle values. The collection will be sorted before calculation.
```rust
use lowdash::median;
let numbers = vec![1, 3, 5, 2, 4];
let result = median(&numbers);
assert!((result.unwrap() - 3.0).abs() < f64::EPSILON);
```
```rust
use lowdash::median;
let numbers = vec![1, 2, 3, 4];
let result = median(&numbers);
assert!((result.unwrap() - 2.5).abs() < f64::EPSILON);
```
### interpolate
Performs linear interpolation between two values.
```rust
use lowdash::interpolate;
let lerp = interpolate(0.0, 10.0);
assert_eq!(lerp(0.5), 5.0);
```
```rust
use lowdash::interpolate;
let lerp = interpolate(-10.0, 10.0);
assert_eq!(lerp(0.25), -5.0);
assert_eq!(lerp(0.75), 5.0);
```
### permutation
Finds all permutation of a collection.
```rust
use lowdash::permutation;
let items = vec![1, 2, 3];
let result = permutation(&items);
assert_eq!(result.len(), 6);
// Possible permutation: [2, 1, 3]
assert!(result.contains(&vec![2, 1, 3]));
```
### combination
Finds all combinations of k elements from a collection.
```rust
use lowdash::combination;
let items = vec![1, 2, 3, 4];
let result = combination(&items, 2);
assert_eq!(result.len(), 6);
// One possible combination: [2, 3]
assert!(result.contains(&vec![2, 3]));
```
### duration_between
Returns the absolute difference between two dates in the specified unit.
```rust
use std::time::{SystemTime, Duration};
use lowdash::{duration_between, DurationUnit};
let epoch = SystemTime::UNIX_EPOCH;
let one_year = Duration::from_secs(31_557_600);
let later = epoch + one_year;
// Difference in years
assert_eq!(duration_between(epoch, later, DurationUnit::Years), 1);
let one_day = Duration::from_secs(86_400);
let day_later = epoch + one_day;
// Difference in days
assert_eq!(duration_between(epoch, day_later, DurationUnit::Days), 1);
```
## 🔥 Benchmark (experimental)
You can find the benchmark result in `report` directory. All the benchmark was generated in Macbook Air M2 with 16GB RAM.
## 🫡 Acknowledgement
This project is inspired by [lodash](https://lodash.com/) and [lo](https://github.com/samber/lo)