Git Product home page Git Product logo

clss.extensionmethods.ilist.firstlastindex's Introduction

CLSS.ExtensionMethods.IList.FirstLastIndex

Problem

Sometimes, you have to find the index of an element that matches a condition within in a collection. This is done like this:

int firstMatchingIndex;
for (int i = 0; i < collection.Count; ++i)
{
  if (!predicate(collection[i]) continue;
  firstMatchingIndex = i; break;
}

This takes a minimum of 6 lines of code that take some time to decode their purpose.

List<T> has a FindIndex method that shortens this syntax to a single line:

int firstMatchingIndex = collection.FindIndex(e => { /* predicate */ });

But this method is only available to List<T> and does not support finding the last matching index.

Solution

This package provides FirstIndex and LastIndex to all IList<T> types to find the first and last index of an element matching a condition, respectively. Due to using manual index counting in its implementation, FirstIndex is also available to all IEnumerable<T> collections.

Example:

using CLSS;

var numbers = new int[] { 6, 4, -3, 15, -18, 9, 23 };
var firstNegativeIdx = numbers.FirstIndex(n => n < 0); // 2
var lastSingleDigitIdx = numbers.LastIndex(n => n > -10 && n < 10); // 5

Note: FirstIndex and LastIndex 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.

clss.extensionmethods.ilist.firstlastindex's People

Contributors

tonygiang avatar

Watchers

 avatar

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.