Skip to content

Collection of methods to limit potentially infinite loops

License

Notifications You must be signed in to change notification settings

murphyne/IterationLimits

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

40 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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<bool> to limit based on condition.
  • IEnumerator<T> to limit enumerator.
  • IEnumerable<T> 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<DateTime> by providing argument of this type.
  • Use the value of custom Func<TimeSpan> by providing argument of this type.

Examples

Note

More examples are available in IterationLimitsExamples project.

LimitCount(Func<bool> condition, int limit)

Func<bool> condition = () => true;
Func<bool> conditionLimited = Limits.LimitCount(condition, 10);

while (conditionLimited.Invoke()) {}

LimitTime<T>(IEnumerator<T> enumerator, TimeSpan limit)

IEnumerator<int> enumerator = GetEnumerator();
IEnumerator<int> enumeratorLimited = Limits.LimitTime(enumerator, TimeSpan.FromSeconds(1));

while (enumeratorLimited.MoveNext()) {}

LimitTime<T>(IEnumerable<T> enumerable, TimeSpan limit, Func<DateTime> measure)

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

foreach (var item in enumerableLimited) {}

About

Collection of methods to limit potentially infinite loops

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages