Giter Club home page Giter Club logo

clss.extensionmethods.ilist.getloopingelementat's Introduction

CLSS.ExtensionMethods.IList.GetLoopingElementAt

Problem

Looping a collection indefinitely is an operation that maps well to many use cases (such as AI behavior phases, sequentially looping slideshow, infinite scrolling background, etc...). And while the CLSS package IEnumerator.LoopNext provided a solution for this problem with wide compatibility, some limitation has to be in place for the sake of compatibility. It can only loop an enumerator in the ascending order, as is the nature of enumeration.

With IList and IList<T> types, we have more information to work with. Use cases for looping in descending order emerge and are not addressed by IEnumerator.LoopNext. Writing descending loop by hand is not as obvious as it may sound and can stump your train of thought.

using CLSS;

var numbers = new int[] { 0, 1, 2, 3 };
// Take arbitrary number in a descending loop
int takeNumber = 10;
foreach (int i = 0; i > -takeNumber; --i)
{
  // this is a correct solution, but may stump you just by reading
  int resolvedIndex = ((i % numbers.Length) + numbers.Length) % numbers.Length;
  DoSomethingWith(numbers[resolvedIndex]);
}

Solution

GetLoopingElementAt is an extension method for all Ilist and IList<T> types that resolves a integer number it takes in as a cyclic index number and returns the element at the resulting index. By spelling out what it's doing on the method name, it gives you a better code comprehension.

using CLSS;

var numbers = new int[] { 0, 1, 2, 3 };
numbers.GetLoopingElementAt(-1); // returns 3
numbers.GetLoopingElementAt(5); // returns 1

GetLoopingElementAt resolves positive and negative numbers into a valid index. Looping over a list is as simple as picking a starting point and go as low or high as you want. Increment to loop the list in ascending order, decrement to loop the list in descending order.

This package also comes with a ResolveLoopingIndex extension method to only receive the result of cyclically resolving a number into a valid index of the source list.

Note: GetLoopingElementAt and ResolveLoopingIndex work on all types implementing the IList<T> interface, including raw C# array.

This package is a part of the C# Language Syntactic Sugar suite.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.