Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/murphyne/iterationlimits

Collection of methods to limit potentially infinite loops
https://github.com/murphyne/iterationlimits

infinite-loop

Last synced: 2 days ago
JSON representation

Collection of methods to limit potentially infinite loops

Awesome Lists containing this project

README

        

# IterationLimits

This is a small library to help you limit potentially infinite loops in your code.

## Features

The `Limits` class provides two ways to limit iterations:

- `LimitCount` to limit the number of iterations.
- `LimitTime` to limit the time duration of iterations.

The `Limits` class supports three types of iterations:

- `Func` to limit based on condition.
- `IEnumerator` to limit enumerator.
- `IEnumerable` to limit enumerable.

The `LimitTime` method provides three overloads to measure time:

- Use the value of `DateTime.Now` by omitting the 3rd argument.
- Use the value of custom `Func` by providing argument of this type.
- Use the value of custom `Func` by providing argument of this type.

## Examples

> [!NOTE]
> More examples are available in `IterationLimitsExamples` project.

`LimitCount(Func condition, int limit)`

```csharp
Func condition = () => true;
Func conditionLimited = Limits.LimitCount(condition, 10);

while (conditionLimited.Invoke()) {}
```

`LimitTime(IEnumerator enumerator, TimeSpan limit)`

```csharp
IEnumerator enumerator = GetEnumerator();
IEnumerator enumeratorLimited = Limits.LimitTime(enumerator, TimeSpan.FromSeconds(1));

while (enumeratorLimited.MoveNext()) {}
```

`LimitTime(IEnumerable enumerable, TimeSpan limit, Func measure)`

```csharp
IEnumerable enumerable = GetEnumerable();
IEnumerable enumerableLimited = Limits.LimitTime(enumerable, TimeSpan.FromSeconds(1), () => DateTime.Now);

foreach (var item in enumerableLimited) {}
```