<commit_before># Only Run Once

A C# `IEnumerable<T>` extension method that ensures that the enumerable is only evaluated once.

If you just want to grab the extension and don't care about the rest of project, it's all in one place: [RunOnceEnumerableExtensions.cs](https://github.com/Creou/AsRunOnce/blob/master/Creou.AsRunOnce/RunOnceEnumerableExtensions.cs)

Usage:

    var willOnlyRunOnce = somethingIEnumerable.OnlyRunOnce();

Calling `OnlyRunOnce()` on an `IEnumerable<T>` will return the enumerable wrapped by an `IRunOnceEnumerable<T>` that will only ever be evaluated once regardless of how many times you enumerate it.

Useful when you have some Linq statements that you want to be evaluated lazily, but you also want to prevent later code from repeated evaluating them unnecessarily. 

For example:

    List<string> listOfStrings = new List<string>() { "1", "2", "3" };
    var enumerableOfInts = listOfStrings.Select(s => int.Parse(s))
                                        .OnlyRunOnce();
    
    // No matter how many times you enumerate the ints now, the enumerable will only be evaulated once.
    foreach (var item in enumerableOfInts)
    {
        Console.WriteLine(item);
    }
    foreach (var item in enumerableOfInts)
    {
        Console.WriteLine(item);
    }
 <commit_msg>Updated readme for concurrent version.<commit_after># Only Run Once

A C# `IEnumerable<T>` extension method that ensures that the enumerable is only evaluated once.

If you just want to grab the extension and don't care about the rest of project, it's all in one place: [RunOnceEnumerableExtensions.cs](https://github.com/Creou/OnlyRunOnce/blob/master/Creou.OnlyRunOnce/RunOnceEnumerableExtensions.cs)

Usage:

    var willOnlyRunOnce = somethingIEnumerable.OnlyRunOnce();
    var willOnlyRunOnce = somethingIEnumerable.OnlyRunOnceConcurrentSafe();

Calling `OnlyRunOnce()` on an `IEnumerable<T>` will return the enumerable wrapped by an `IRunOnceEnumerable<T>` that will only ever be evaluated once regardless of how many times you enumerate it.

Useful when you have some Linq statements that you want to be evaluated lazily, but you also want to prevent later code from repeated evaluating them unnecessarily. 

For example:

    List<string> listOfStrings = new List<string>() { "1", "2", "3" };
    var enumerableOfInts = listOfStrings.Select(s => int.Parse(s))
                                        .OnlyRunOnce();
    
    // No matter how many times you enumerate the ints now, the enumerable will only be evaulated once.
    foreach (var item in enumerableOfInts)
    {
        Console.WriteLine(item);
    }
    foreach (var item in enumerableOfInts)
    {
        Console.WriteLine(item);
    }
 
There is also a concurrent version that is entirely thread safe:

    List<string> listOfStrings = new List<string>() { "1", "2", "3" };
    var enumerableOfInts = listOfStrings.Select(s => int.Parse(s))
                                        .OnlyRunOnceConcurrentSafe();

    // No matter how many times you enumerate the ints even on multiple threads, the enumerable will only be evaulated once.
    await Task.WhenAll(
        Task.Run(() =>
        {
            foreach (var item in enumerableOfInts)
            {
                Console.WriteLine(item);
            }
        }),
        Task.Run(() =>
        {
            foreach (var item in enumerableOfInts)
            {
                Console.WriteLine(item);
            }
        }));
{'fraction_non_alphanumeric': 0.08333333333333333, 'fraction_numerical': 0.0025951557093425604, 'mean_word_length': 2.910935738444194, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 20, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 3, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6283203', 'n_tokens_mistral': 951, 'n_tokens_neox': 912, 'n_words': 350}