#count #iterator #where

count_where

library that adds a count_where method to Iterator, to easily count items matching a condition

2 releases

0.1.1 Jun 7, 2021
0.1.0 Jun 6, 2021

#3 in #where

29 downloads per month

MIT license

7KB

=count_where=

'''Author:''' Bryce Campbell

'''Description:''' A single trait library that adds an iterator method allowing one to easily count up elements matching a particular condition.

'''License:''' See LICENSE

==Version History==

  • 0.1.1

    Released June 7, 2021

    Allows comparison booleans in closure.

  • 0.1

    Released June 6, 2021

    Initial release.

==Notes==

This library has only been tested with Rust 1.51.0 on macOS Big Sur, but should work on any platform.

===Why Create this Library?===

I like dabbling in programming from time to time, and one day I tried to make a Rust equivalent of this project, to help me gain more experience with Rust.

While I could figure out most things, I had a bit of trouble with how I was implementing things, such as a counting method to determine how many times a number occurred.

Even though I could create a function that took the data and a number to look for, it seemed to be not quite as nice as the extension I wrote for the Swift variant, which worked for all Sequences that had items that conformed to Equatable.

As a result, I decided to look into creating an extension on Iterator in Rust, so that I could do something like this in Rust:

sequence.count { $0 == number }

Not only did I kind of succeed when writing the code for the library, but it gave me a good chance to see how to make a ttrait extension and creating a library right in Rust.

While this library does not provide the same simplicity as the Swift code seen above, it still provides the same convenience, which is what I needed.

===Usage===

To use this library, add the following to the Cargo.toml file:

[dependencies]
count_where = "0.1.0"

Afterwards, it is as simple as putting the following in the source file needing the method:

use count_where::CountWhere;

You can then use code like this to do your counting:

fn main() {
    let numbers = [5, 5, 5, 2, 1];

    let number = 5

    println!("{} appears {} times", number, numbers.iter().count_where(|n| **n == number));
}

Because n is of type &&T, it needs to be dereferenced as seen above, in order to make the comparison.

This could change in the future, which I would have liked to do, but because I am not as experienced with Rust as I am with Swift, this will be something I need to live with.

===Contributing===

If you would like to contribute to this library, feel free to create a fork and send your modifications.

One thing that I would really like is for the provided function to usable in a way lie the filter method, which is utilitized like this:

fn main() {
    let numbers = [5, 5, 5, 2, 1];
    let filtered_numbers = numbers.iter().filter(|n| n == 5);

    println!("{}", filtered_numbers);
}

===Support===

Due to my experience with Rust being limited, in comparison to Swift, I cannot provide that much support, so expect to be on your own.

However, I can try to check things out, so feel free contact me at the email below.

tonyhawk2100@gmail.com

No runtime deps