rust-lodash 0.1.0

A high-performance, type-safe Rust implementation of Lodash collection methods with zero-cost abstractions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
/*!

Query methods for Lodash-RS.

This module provides query methods like `find`, `includes`, `every`, `some`, etc.
These methods are used to search and test elements in collections.
*/

use crate::collection::Collection;
// Note: These imports are kept for future use in error handling and type constraints
// use crate::utils::{LodashError, Result, Predicate};

/// Iterate over elements of collection, returning the first element
/// the predicate returns truthy for.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::find;
/// 
/// let numbers = vec![1, 2, 3, 4, 5];
/// let first_even = find(&numbers, |x| x % 2 == 0);
/// assert_eq!(first_even, Some(&2));
/// 
/// let not_found = find(&numbers, |x| *x > 10);
/// assert_eq!(not_found, None);
/// ```
pub fn find<T, F>(collection: &[T], predicate: F) -> Option<&T>
where
    F: Fn(&T) -> bool,
{
    collection.iter().find(|item| predicate(item))
}

/// This method is like `find` except that it iterates over elements of
/// collection from right to left.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::find_last;
/// 
/// let numbers = vec![1, 2, 3, 4, 5];
/// let last_even = find_last(&numbers, |x| x % 2 == 0);
/// assert_eq!(last_even, Some(&4));
/// ```
pub fn find_last<T, F>(collection: &[T], predicate: F) -> Option<&T>
where
    F: Fn(&T) -> bool,
{
    collection.iter().rev().find(|item| predicate(item))
}

/// Check if value is in collection.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::includes;
/// 
/// let numbers = vec![1, 2, 3, 4, 5];
/// assert!(includes(&numbers, &3));
/// assert!(!includes(&numbers, &6));
/// ```
pub fn includes<T>(collection: &[T], value: &T) -> bool
where
    T: PartialEq,
{
    collection.contains(value)
}

/// Check if predicate returns truthy for all elements of collection.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::every;
/// 
/// let numbers = vec![2, 4, 6, 8];
/// assert!(every(&numbers, |x| x % 2 == 0));
/// 
/// let mixed = vec![2, 4, 5, 8];
/// assert!(!every(&mixed, |x| x % 2 == 0));
/// ```
pub fn every<T, F>(collection: &[T], predicate: F) -> bool
where
    F: Fn(&T) -> bool,
{
    collection.iter().all(predicate)
}

/// Check if predicate returns truthy for any element of collection.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::some;
/// 
/// let numbers = vec![1, 2, 3, 4, 5];
/// assert!(some(&numbers, |x| x % 2 == 0));
/// 
/// let odds = vec![1, 3, 5, 7];
/// assert!(!some(&odds, |x| x % 2 == 0));
/// ```
pub fn some<T, F>(collection: &[T], predicate: F) -> bool
where
    F: Fn(&T) -> bool,
{
    collection.iter().any(predicate)
}

/// Create an object composed of keys generated from the results of running
/// each element of collection through iteratee. The corresponding value of
/// each key is the number of times the key was returned by iteratee.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::count_by;
/// use std::collections::HashMap;
/// 
/// let numbers = vec![6.1, 4.2, 6.3];
/// let counts = count_by(&numbers, |x| (*x as f64).floor() as i32);
/// assert_eq!(counts.get(&6), Some(&2));
/// assert_eq!(counts.get(&4), Some(&1));
/// ```
pub fn count_by<T, K, F>(collection: &[T], iteratee: F) -> std::collections::HashMap<K, usize>
where
    K: std::hash::Hash + Eq,
    F: Fn(&T) -> K,
{
    let mut counts = std::collections::HashMap::new();
    for item in collection {
        let key = iteratee(item);
        *counts.entry(key).or_insert(0) += 1;
    }
    counts
}

/// Create an array of elements split into two groups, the first of which
/// contains elements the predicate returns truthy for, while the second
/// contains elements the predicate returns falsy for.
/// 
/// # Examples
/// 
/// ```
/// use rust_lodash::collection::query::partition;
/// 
/// let numbers = vec![1, 2, 3, 4, 5];
/// let (evens, odds) = partition(&numbers, |x| x % 2 == 0);
/// assert_eq!(evens, vec![2, 4]);
/// assert_eq!(odds, vec![1, 3, 5]);
/// ```
pub fn partition<T, F>(collection: &[T], predicate: F) -> (Vec<T>, Vec<T>)
where
    T: Clone,
    F: Fn(&T) -> bool,
{
    let mut truthy = Vec::new();
    let mut falsy = Vec::new();
    
    for item in collection {
        if predicate(item) {
            truthy.push(item.clone());
        } else {
            falsy.push(item.clone());
        }
    }
    
    (truthy, falsy)
}

/// Collection methods that work on the `Collection` type.
impl<T> Collection<T> {
    /// Iterate over elements, returning the first element
    /// the predicate returns truthy for.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// 
    /// let collection = Collection::new(vec![1, 2, 3, 4, 5]);
    /// let first_even = collection.find(|x| x % 2 == 0);
    /// assert_eq!(first_even, Some(&2));
    /// ```
    pub fn find<F>(&self, predicate: F) -> Option<&T>
    where
        F: Fn(&T) -> bool,
    {
        find(&self.data, predicate)
    }

    /// This method is like `find` except that it iterates from right to left.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// 
    /// let collection = Collection::new(vec![1, 2, 3, 4, 5]);
    /// let last_even = collection.find_last(|x| x % 2 == 0);
    /// assert_eq!(last_even, Some(&4));
    /// ```
    pub fn find_last<F>(&self, predicate: F) -> Option<&T>
    where
        F: Fn(&T) -> bool,
    {
        find_last(&self.data, predicate)
    }

    /// Check if value is in the collection.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// 
    /// let collection = Collection::new(vec![1, 2, 3, 4, 5]);
    /// assert!(collection.includes(&3));
    /// assert!(!collection.includes(&6));
    /// ```
    pub fn includes(&self, value: &T) -> bool
    where
        T: PartialEq,
    {
        includes(&self.data, value)
    }

    /// Check if predicate returns truthy for all elements.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// 
    /// let collection = Collection::new(vec![2, 4, 6, 8]);
    /// assert!(collection.every(|x| x % 2 == 0));
    /// ```
    pub fn every<F>(&self, predicate: F) -> bool
    where
        F: Fn(&T) -> bool,
    {
        every(&self.data, predicate)
    }

    /// Check if predicate returns truthy for any element.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// 
    /// let collection = Collection::new(vec![1, 2, 3, 4, 5]);
    /// assert!(collection.some(|x| x % 2 == 0));
    /// ```
    pub fn some<F>(&self, predicate: F) -> bool
    where
        F: Fn(&T) -> bool,
    {
        some(&self.data, predicate)
    }

    /// Create an object composed of keys generated from the results of running
    /// each element through iteratee.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// use std::collections::HashMap;
    /// 
    /// let collection = Collection::new(vec![6.1, 4.2, 6.3]);
    /// let counts = collection.count_by(|x| (*x as f64).floor() as i32);
    /// assert_eq!(counts.get(&6), Some(&2));
    /// ```
    pub fn count_by<K, F>(&self, iteratee: F) -> std::collections::HashMap<K, usize>
    where
        K: std::hash::Hash + Eq,
        F: Fn(&T) -> K,
    {
        count_by(&self.data, iteratee)
    }

    /// Create an array of elements split into two groups.
    /// 
    /// # Examples
    /// 
    /// ```
    /// use rust_lodash::collection::Collection;
    /// 
    /// let collection = Collection::new(vec![1, 2, 3, 4, 5]);
    /// let (evens, odds) = collection.partition(|x| x % 2 == 0);
    /// assert_eq!(evens, vec![2, 4]);
    /// assert_eq!(odds, vec![1, 3, 5]);
    /// ```
    pub fn partition<F>(&self, predicate: F) -> (Vec<T>, Vec<T>)
    where
        T: Clone,
        F: Fn(&T) -> bool,
    {
        partition(&self.data, predicate)
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    // use std::collections::HashMap; // For future use in advanced query operations

    #[test]
    fn test_find() {
        let numbers = vec![1, 2, 3, 4, 5];
        let first_even = find(&numbers, |x| x % 2 == 0);
        assert_eq!(first_even, Some(&2));

        let not_found = find(&numbers, |x| *x > 10);
        assert_eq!(not_found, None);
    }

    #[test]
    fn test_find_last() {
        let numbers = vec![1, 2, 3, 4, 5];
        let last_even = find_last(&numbers, |x| x % 2 == 0);
        assert_eq!(last_even, Some(&4));
    }

    #[test]
    fn test_includes() {
        let numbers = vec![1, 2, 3, 4, 5];
        assert!(includes(&numbers, &3));
        assert!(!includes(&numbers, &6));
    }

    #[test]
    fn test_every() {
        let numbers = vec![2, 4, 6, 8];
        assert!(every(&numbers, |x| x % 2 == 0));

        let mixed = vec![2, 4, 5, 8];
        assert!(!every(&mixed, |x| x % 2 == 0));
    }

    #[test]
    fn test_some() {
        let numbers = vec![1, 2, 3, 4, 5];
        assert!(some(&numbers, |x| x % 2 == 0));

        let odds = vec![1, 3, 5, 7];
        assert!(!some(&odds, |x| x % 2 == 0));
    }

    #[test]
    fn test_count_by() {
        let numbers = vec![6.1, 4.2, 6.3];
        let counts = count_by(&numbers, |x| {
            #[allow(clippy::cast_possible_truncation, clippy::unnecessary_cast)]
            {
                (*x as f64).floor() as i32
            }
        });
        assert_eq!(counts.get(&6), Some(&2));
        assert_eq!(counts.get(&4), Some(&1));
    }

    #[test]
    fn test_partition() {
        let numbers = vec![1, 2, 3, 4, 5];
        let (evens, odds) = partition(&numbers, |x| x % 2 == 0);
        assert_eq!(evens, vec![2, 4]);
        assert_eq!(odds, vec![1, 3, 5]);
    }

    #[test]
    fn test_collection_find() {
        let collection = Collection::new(vec![1, 2, 3, 4, 5]);
        let first_even = collection.find(|x| x % 2 == 0);
        assert_eq!(first_even, Some(&2));
    }

    #[test]
    fn test_collection_find_last() {
        let collection = Collection::new(vec![1, 2, 3, 4, 5]);
        let last_even = collection.find_last(|x| x % 2 == 0);
        assert_eq!(last_even, Some(&4));
    }

    #[test]
    fn test_collection_includes() {
        let collection = Collection::new(vec![1, 2, 3, 4, 5]);
        assert!(collection.includes(&3));
        assert!(!collection.includes(&6));
    }

    #[test]
    fn test_collection_every() {
        let collection = Collection::new(vec![2, 4, 6, 8]);
        assert!(collection.every(|x| x % 2 == 0));
    }

    #[test]
    fn test_collection_some() {
        let collection = Collection::new(vec![1, 2, 3, 4, 5]);
        assert!(collection.some(|x| x % 2 == 0));
        
        let odds_collection = Collection::new(vec![1, 3, 5, 7]);
        assert!(!odds_collection.some(|x| x % 2 == 0));
    }

    #[test]
    fn test_collection_count_by() {
        let collection = Collection::new(vec![6.1, 4.2, 6.3]);
        let counts = collection.count_by(|x| {
            #[allow(clippy::cast_possible_truncation, clippy::unnecessary_cast)]
            {
                (*x as f64).floor() as i32
            }
        });
        assert_eq!(counts.get(&6), Some(&2));
    }

    #[test]
    fn test_collection_partition() {
        let collection = Collection::new(vec![1, 2, 3, 4, 5]);
        let (evens, odds) = collection.partition(|x| x % 2 == 0);
        assert_eq!(evens, vec![2, 4]);
        assert_eq!(odds, vec![1, 3, 5]);
    }

    #[test]
    fn test_empty_collection() {
        let empty: Vec<i32> = vec![];
        assert_eq!(find(&empty, |x| x % 2 == 0), None);
        assert!(every(&empty, |x| x % 2 == 0)); // vacuous truth
        assert!(!some(&empty, |x| x % 2 == 0)); // vacuous false
        assert!(!includes(&empty, &1));
    }
}