LINQ method that selects an item based on previous one

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
4
down vote

favorite
1












my requirement is to implement a way to filter a collection of elements based on a value of previous one.
For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing, so I decided to create one.



This is a code I have so far:



 public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

if (collection == null) throw new ArgumentNullException(nameof(collection));
if (predicate == null) throw new ArgumentNullException(nameof(predicate));

T previous = default(T);
bool firstIteration = true;

foreach (var item in collection)

if (firstIteration)

previous = item;
firstIteration = false;
yield return item;

else

if (predicate(previous, item))

yield return item;


previous = item;





Are there any ways to improve my code in terms of performance, readability or more generalization to make?



For example I'm not sure if it's clear to the caller of the method what the predicate inputs actually are







share|improve this question

























    up vote
    4
    down vote

    favorite
    1












    my requirement is to implement a way to filter a collection of elements based on a value of previous one.
    For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing, so I decided to create one.



    This is a code I have so far:



     public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

    if (collection == null) throw new ArgumentNullException(nameof(collection));
    if (predicate == null) throw new ArgumentNullException(nameof(predicate));

    T previous = default(T);
    bool firstIteration = true;

    foreach (var item in collection)

    if (firstIteration)

    previous = item;
    firstIteration = false;
    yield return item;

    else

    if (predicate(previous, item))

    yield return item;


    previous = item;





    Are there any ways to improve my code in terms of performance, readability or more generalization to make?



    For example I'm not sure if it's clear to the caller of the method what the predicate inputs actually are







    share|improve this question





















      up vote
      4
      down vote

      favorite
      1









      up vote
      4
      down vote

      favorite
      1






      1





      my requirement is to implement a way to filter a collection of elements based on a value of previous one.
      For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing, so I decided to create one.



      This is a code I have so far:



       public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

      if (collection == null) throw new ArgumentNullException(nameof(collection));
      if (predicate == null) throw new ArgumentNullException(nameof(predicate));

      T previous = default(T);
      bool firstIteration = true;

      foreach (var item in collection)

      if (firstIteration)

      previous = item;
      firstIteration = false;
      yield return item;

      else

      if (predicate(previous, item))

      yield return item;


      previous = item;





      Are there any ways to improve my code in terms of performance, readability or more generalization to make?



      For example I'm not sure if it's clear to the caller of the method what the predicate inputs actually are







      share|improve this question











      my requirement is to implement a way to filter a collection of elements based on a value of previous one.
      For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing, so I decided to create one.



      This is a code I have so far:



       public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

      if (collection == null) throw new ArgumentNullException(nameof(collection));
      if (predicate == null) throw new ArgumentNullException(nameof(predicate));

      T previous = default(T);
      bool firstIteration = true;

      foreach (var item in collection)

      if (firstIteration)

      previous = item;
      firstIteration = false;
      yield return item;

      else

      if (predicate(previous, item))

      yield return item;


      previous = item;





      Are there any ways to improve my code in terms of performance, readability or more generalization to make?



      For example I'm not sure if it's clear to the caller of the method what the predicate inputs actually are









      share|improve this question










      share|improve this question




      share|improve this question









      asked Feb 9 at 0:01









      LadislavBohm

      283




      283




















          3 Answers
          3






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          Linq Zip came to mind in order to map the items in pairs and then apply the predicate.



          public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
          if (collection == null) throw new ArgumentNullException(nameof(collection));
          if (predicate == null) throw new ArgumentNullException(nameof(predicate));
          if(!collection.Any()) yield break;

          yield return collection.First();

          var items = collection
          .Zip(collection.Skip(1), (previous, item) => (previous, item))
          .Where(zip => predicate(zip.previous, zip.item))
          .Select(zip => zip.item);

          foreach(var item in items)
          yield return item;



          The following test was used to demonstrate the example given in the original question.



          [TestClass]
          public class FilterTest
          [TestMethod]
          public void WherePreviousTest()
          //Arrange
          var numbers = new 1, 5, 8, 7, 12, 8, 5 ;
          var expected = new 1, 5, 8, 12 ;

          //Act
          var actual = numbers.WherePrevious((first, second) => second > first).ToArray();

          //Assert
          actual.ShouldAllBeEquivalentTo(expected);




          The body of the created extension method could have been done on its own, but creating the extension method allowed for a cleaner approach.



          While I would say it is easier to read IMO, technically you will be enumerating the list more than once, which makes this not as efficient as your one pass through.



          That then lead me to rethink my approach. After reviewing the source code for Enumerable.Zip and realizing your approach had the right idea I refactored the code to use the enumerator of the collection.



          public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
          if (collection == null) throw new ArgumentNullException(nameof(collection));
          if (predicate == null) throw new ArgumentNullException(nameof(predicate));
          return WherePreviousIterator(collection, predicate);


          private static IEnumerable<T> WherePreviousIterator<T>(IEnumerable<T> collection, Func<T, T, bool> predicate)
          using (var e = collection.GetEnumerator())
          if (e.MoveNext())
          var previous = e.Current;
          yield return previous;
          while (e.MoveNext())
          var item = e.Current;
          if (predicate(previous, item))
          yield return item;
          previous = item;


          yield break;




          Which actually performed not that much better than the previous suggestion using Zip when run against the same test above.






          share|improve this answer























          • I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
            – t3chb0t
            Feb 9 at 5:18











          • @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
            – Nkosi
            Feb 9 at 5:25











          • I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
            – t3chb0t
            Feb 9 at 5:30







          • 1




            @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
            – Nkosi
            Feb 9 at 5:42






          • 1




            For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
            – LadislavBohm
            Feb 9 at 11:43


















          up vote
          2
          down vote













          First I've tried to make your extension a little clearer by avoiding the firstIteration flag:



          public static IEnumerable<T> WherePreviousReview<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

          if (collection == null) throw new ArgumentNullException(nameof(collection));
          if (predicate == null) throw new ArgumentNullException(nameof(predicate));
          if (!collection.Any())
          yield break;

          T previous = collection.First();

          yield return previous;

          foreach (var item in collection.Skip(1))

          if (predicate(previous, item))

          yield return item;


          previous = item;





          This is a version that uses the power of the Where extension:



          public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

          if (collection == null) throw new ArgumentNullException(nameof(collection));
          if (predicate == null) throw new ArgumentNullException(nameof(predicate));
          T previous = default(T);
          return collection.Where((current, i) =>

          bool result = i == 0 );




          This is a version that uses the Aggregate extension:



          public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

          if (collection == null) throw new ArgumentNullException(nameof(collection));
          if (predicate == null) throw new ArgumentNullException(nameof(predicate));
          if (!collection.Any())
          return new List<T>();

          return collection.Aggregate((0, default(T), new List<T>()), (acc, cur) =>

          if (acc.Item1 == 0 ).Item3;



          The latter is not meant as a better choice, just an example of another approach. The caveat is the inner list object.




          Update



          Aggregate version with named tuples:



          public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

          if (collection == null) throw new ArgumentNullException(nameof(collection));
          if (predicate == null) throw new ArgumentNullException(nameof(predicate));
          if (!collection.Any())
          return new T[0];

          (int index, T prev, List<T> list) seed = (0, default(T), new List<T>());

          return collection.Aggregate(seed, ((int index, T prev, List<T> list) acc, T cur) =>
          ).list;






          share|improve this answer























          • You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
            – t3chb0t
            Feb 9 at 8:09







          • 1




            @t3chb0t: I totally agree - and updated my answer.
            – Henrik Hansen
            Feb 9 at 8:35










          • The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
            – LadislavBohm
            Feb 9 at 11:47







          • 1




            @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
            – Henrik Hansen
            Feb 9 at 14:24

















          up vote
          1
          down vote














          For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing




          Actually there is a LINQ extension which allows you to do that. the Where extension has an overload that uses the item value and the item's index:



          List<int> test1 = new List<int>()

          23,
          45,
          16,
          8,
          90,
          25
          ;
          var test2 = test1.Where((x, i) => (i > 0)?x > test1[i - 1]:false);


          One caveat to this is, it will only work for indexed collections of types that support comparison operators.






          share|improve this answer























            Your Answer




            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "196"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: false,
            noModals: false,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            bindNavPrevention: true,
            postfix: "",
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );








             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187135%2flinq-method-that-selects-an-item-based-on-previous-one%23new-answer', 'question_page');

            );

            Post as a guest






























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            3
            down vote



            accepted










            Linq Zip came to mind in order to map the items in pairs and then apply the predicate.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if(!collection.Any()) yield break;

            yield return collection.First();

            var items = collection
            .Zip(collection.Skip(1), (previous, item) => (previous, item))
            .Where(zip => predicate(zip.previous, zip.item))
            .Select(zip => zip.item);

            foreach(var item in items)
            yield return item;



            The following test was used to demonstrate the example given in the original question.



            [TestClass]
            public class FilterTest
            [TestMethod]
            public void WherePreviousTest()
            //Arrange
            var numbers = new 1, 5, 8, 7, 12, 8, 5 ;
            var expected = new 1, 5, 8, 12 ;

            //Act
            var actual = numbers.WherePrevious((first, second) => second > first).ToArray();

            //Assert
            actual.ShouldAllBeEquivalentTo(expected);




            The body of the created extension method could have been done on its own, but creating the extension method allowed for a cleaner approach.



            While I would say it is easier to read IMO, technically you will be enumerating the list more than once, which makes this not as efficient as your one pass through.



            That then lead me to rethink my approach. After reviewing the source code for Enumerable.Zip and realizing your approach had the right idea I refactored the code to use the enumerator of the collection.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            return WherePreviousIterator(collection, predicate);


            private static IEnumerable<T> WherePreviousIterator<T>(IEnumerable<T> collection, Func<T, T, bool> predicate)
            using (var e = collection.GetEnumerator())
            if (e.MoveNext())
            var previous = e.Current;
            yield return previous;
            while (e.MoveNext())
            var item = e.Current;
            if (predicate(previous, item))
            yield return item;
            previous = item;


            yield break;




            Which actually performed not that much better than the previous suggestion using Zip when run against the same test above.






            share|improve this answer























            • I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
              – t3chb0t
              Feb 9 at 5:18











            • @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
              – Nkosi
              Feb 9 at 5:25











            • I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
              – t3chb0t
              Feb 9 at 5:30







            • 1




              @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
              – Nkosi
              Feb 9 at 5:42






            • 1




              For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
              – LadislavBohm
              Feb 9 at 11:43















            up vote
            3
            down vote



            accepted










            Linq Zip came to mind in order to map the items in pairs and then apply the predicate.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if(!collection.Any()) yield break;

            yield return collection.First();

            var items = collection
            .Zip(collection.Skip(1), (previous, item) => (previous, item))
            .Where(zip => predicate(zip.previous, zip.item))
            .Select(zip => zip.item);

            foreach(var item in items)
            yield return item;



            The following test was used to demonstrate the example given in the original question.



            [TestClass]
            public class FilterTest
            [TestMethod]
            public void WherePreviousTest()
            //Arrange
            var numbers = new 1, 5, 8, 7, 12, 8, 5 ;
            var expected = new 1, 5, 8, 12 ;

            //Act
            var actual = numbers.WherePrevious((first, second) => second > first).ToArray();

            //Assert
            actual.ShouldAllBeEquivalentTo(expected);




            The body of the created extension method could have been done on its own, but creating the extension method allowed for a cleaner approach.



            While I would say it is easier to read IMO, technically you will be enumerating the list more than once, which makes this not as efficient as your one pass through.



            That then lead me to rethink my approach. After reviewing the source code for Enumerable.Zip and realizing your approach had the right idea I refactored the code to use the enumerator of the collection.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            return WherePreviousIterator(collection, predicate);


            private static IEnumerable<T> WherePreviousIterator<T>(IEnumerable<T> collection, Func<T, T, bool> predicate)
            using (var e = collection.GetEnumerator())
            if (e.MoveNext())
            var previous = e.Current;
            yield return previous;
            while (e.MoveNext())
            var item = e.Current;
            if (predicate(previous, item))
            yield return item;
            previous = item;


            yield break;




            Which actually performed not that much better than the previous suggestion using Zip when run against the same test above.






            share|improve this answer























            • I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
              – t3chb0t
              Feb 9 at 5:18











            • @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
              – Nkosi
              Feb 9 at 5:25











            • I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
              – t3chb0t
              Feb 9 at 5:30







            • 1




              @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
              – Nkosi
              Feb 9 at 5:42






            • 1




              For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
              – LadislavBohm
              Feb 9 at 11:43













            up vote
            3
            down vote



            accepted







            up vote
            3
            down vote



            accepted






            Linq Zip came to mind in order to map the items in pairs and then apply the predicate.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if(!collection.Any()) yield break;

            yield return collection.First();

            var items = collection
            .Zip(collection.Skip(1), (previous, item) => (previous, item))
            .Where(zip => predicate(zip.previous, zip.item))
            .Select(zip => zip.item);

            foreach(var item in items)
            yield return item;



            The following test was used to demonstrate the example given in the original question.



            [TestClass]
            public class FilterTest
            [TestMethod]
            public void WherePreviousTest()
            //Arrange
            var numbers = new 1, 5, 8, 7, 12, 8, 5 ;
            var expected = new 1, 5, 8, 12 ;

            //Act
            var actual = numbers.WherePrevious((first, second) => second > first).ToArray();

            //Assert
            actual.ShouldAllBeEquivalentTo(expected);




            The body of the created extension method could have been done on its own, but creating the extension method allowed for a cleaner approach.



            While I would say it is easier to read IMO, technically you will be enumerating the list more than once, which makes this not as efficient as your one pass through.



            That then lead me to rethink my approach. After reviewing the source code for Enumerable.Zip and realizing your approach had the right idea I refactored the code to use the enumerator of the collection.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            return WherePreviousIterator(collection, predicate);


            private static IEnumerable<T> WherePreviousIterator<T>(IEnumerable<T> collection, Func<T, T, bool> predicate)
            using (var e = collection.GetEnumerator())
            if (e.MoveNext())
            var previous = e.Current;
            yield return previous;
            while (e.MoveNext())
            var item = e.Current;
            if (predicate(previous, item))
            yield return item;
            previous = item;


            yield break;




            Which actually performed not that much better than the previous suggestion using Zip when run against the same test above.






            share|improve this answer















            Linq Zip came to mind in order to map the items in pairs and then apply the predicate.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if(!collection.Any()) yield break;

            yield return collection.First();

            var items = collection
            .Zip(collection.Skip(1), (previous, item) => (previous, item))
            .Where(zip => predicate(zip.previous, zip.item))
            .Select(zip => zip.item);

            foreach(var item in items)
            yield return item;



            The following test was used to demonstrate the example given in the original question.



            [TestClass]
            public class FilterTest
            [TestMethod]
            public void WherePreviousTest()
            //Arrange
            var numbers = new 1, 5, 8, 7, 12, 8, 5 ;
            var expected = new 1, 5, 8, 12 ;

            //Act
            var actual = numbers.WherePrevious((first, second) => second > first).ToArray();

            //Assert
            actual.ShouldAllBeEquivalentTo(expected);




            The body of the created extension method could have been done on its own, but creating the extension method allowed for a cleaner approach.



            While I would say it is easier to read IMO, technically you will be enumerating the list more than once, which makes this not as efficient as your one pass through.



            That then lead me to rethink my approach. After reviewing the source code for Enumerable.Zip and realizing your approach had the right idea I refactored the code to use the enumerator of the collection.



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate) 
            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            return WherePreviousIterator(collection, predicate);


            private static IEnumerable<T> WherePreviousIterator<T>(IEnumerable<T> collection, Func<T, T, bool> predicate)
            using (var e = collection.GetEnumerator())
            if (e.MoveNext())
            var previous = e.Current;
            yield return previous;
            while (e.MoveNext())
            var item = e.Current;
            if (predicate(previous, item))
            yield return item;
            previous = item;


            yield break;




            Which actually performed not that much better than the previous suggestion using Zip when run against the same test above.







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Feb 9 at 11:44


























            answered Feb 9 at 3:47









            Nkosi

            1,870619




            1,870619











            • I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
              – t3chb0t
              Feb 9 at 5:18











            • @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
              – Nkosi
              Feb 9 at 5:25











            • I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
              – t3chb0t
              Feb 9 at 5:30







            • 1




              @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
              – Nkosi
              Feb 9 at 5:42






            • 1




              For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
              – LadislavBohm
              Feb 9 at 11:43

















            • I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
              – t3chb0t
              Feb 9 at 5:18











            • @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
              – Nkosi
              Feb 9 at 5:25











            • I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
              – t3chb0t
              Feb 9 at 5:30







            • 1




              @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
              – Nkosi
              Feb 9 at 5:42






            • 1




              For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
              – LadislavBohm
              Feb 9 at 11:43
















            I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
            – t3chb0t
            Feb 9 at 5:18





            I like the Zip version better and I think you should put the predicate where it belongs, this is, inside Where instead of using a blank marker.
            – t3chb0t
            Feb 9 at 5:18













            @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
            – Nkosi
            Feb 9 at 5:25





            @t3chb0t I preferred the Zip myself, but when testing i noticed the iterator version performed 10 times better. As for the second part of your statement, how would the predicate work inside Where. It takes two arguments while Where's predicate expects only one.
            – Nkosi
            Feb 9 at 5:25













            I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
            – t3chb0t
            Feb 9 at 5:30





            I'd do it like this - let me post it as a gist because otherwise it'll look terrible here as a single long line ;-]
            – t3chb0t
            Feb 9 at 5:30





            1




            1




            @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
            – Nkosi
            Feb 9 at 5:42




            @t3chb0t thanks. The updated zip version now performs almost as well as the enumerator, which actual would make the Zip the better choice.
            – Nkosi
            Feb 9 at 5:42




            1




            1




            For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
            – LadislavBohm
            Feb 9 at 11:43





            For my scenario returning the first element is correct but I agree that my description of the problem is different and should not return the first item. However I like this solution as it doesn't rely on indexed collection types and is readable by making it using existing LINQ methods.
            – LadislavBohm
            Feb 9 at 11:43













            up vote
            2
            down vote













            First I've tried to make your extension a little clearer by avoiding the firstIteration flag:



            public static IEnumerable<T> WherePreviousReview<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            yield break;

            T previous = collection.First();

            yield return previous;

            foreach (var item in collection.Skip(1))

            if (predicate(previous, item))

            yield return item;


            previous = item;





            This is a version that uses the power of the Where extension:



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            T previous = default(T);
            return collection.Where((current, i) =>

            bool result = i == 0 );




            This is a version that uses the Aggregate extension:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new List<T>();

            return collection.Aggregate((0, default(T), new List<T>()), (acc, cur) =>

            if (acc.Item1 == 0 ).Item3;



            The latter is not meant as a better choice, just an example of another approach. The caveat is the inner list object.




            Update



            Aggregate version with named tuples:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new T[0];

            (int index, T prev, List<T> list) seed = (0, default(T), new List<T>());

            return collection.Aggregate(seed, ((int index, T prev, List<T> list) acc, T cur) =>
            ).list;






            share|improve this answer























            • You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
              – t3chb0t
              Feb 9 at 8:09







            • 1




              @t3chb0t: I totally agree - and updated my answer.
              – Henrik Hansen
              Feb 9 at 8:35










            • The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
              – LadislavBohm
              Feb 9 at 11:47







            • 1




              @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
              – Henrik Hansen
              Feb 9 at 14:24














            up vote
            2
            down vote













            First I've tried to make your extension a little clearer by avoiding the firstIteration flag:



            public static IEnumerable<T> WherePreviousReview<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            yield break;

            T previous = collection.First();

            yield return previous;

            foreach (var item in collection.Skip(1))

            if (predicate(previous, item))

            yield return item;


            previous = item;





            This is a version that uses the power of the Where extension:



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            T previous = default(T);
            return collection.Where((current, i) =>

            bool result = i == 0 );




            This is a version that uses the Aggregate extension:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new List<T>();

            return collection.Aggregate((0, default(T), new List<T>()), (acc, cur) =>

            if (acc.Item1 == 0 ).Item3;



            The latter is not meant as a better choice, just an example of another approach. The caveat is the inner list object.




            Update



            Aggregate version with named tuples:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new T[0];

            (int index, T prev, List<T> list) seed = (0, default(T), new List<T>());

            return collection.Aggregate(seed, ((int index, T prev, List<T> list) acc, T cur) =>
            ).list;






            share|improve this answer























            • You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
              – t3chb0t
              Feb 9 at 8:09







            • 1




              @t3chb0t: I totally agree - and updated my answer.
              – Henrik Hansen
              Feb 9 at 8:35










            • The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
              – LadislavBohm
              Feb 9 at 11:47







            • 1




              @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
              – Henrik Hansen
              Feb 9 at 14:24












            up vote
            2
            down vote










            up vote
            2
            down vote









            First I've tried to make your extension a little clearer by avoiding the firstIteration flag:



            public static IEnumerable<T> WherePreviousReview<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            yield break;

            T previous = collection.First();

            yield return previous;

            foreach (var item in collection.Skip(1))

            if (predicate(previous, item))

            yield return item;


            previous = item;





            This is a version that uses the power of the Where extension:



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            T previous = default(T);
            return collection.Where((current, i) =>

            bool result = i == 0 );




            This is a version that uses the Aggregate extension:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new List<T>();

            return collection.Aggregate((0, default(T), new List<T>()), (acc, cur) =>

            if (acc.Item1 == 0 ).Item3;



            The latter is not meant as a better choice, just an example of another approach. The caveat is the inner list object.




            Update



            Aggregate version with named tuples:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new T[0];

            (int index, T prev, List<T> list) seed = (0, default(T), new List<T>());

            return collection.Aggregate(seed, ((int index, T prev, List<T> list) acc, T cur) =>
            ).list;






            share|improve this answer















            First I've tried to make your extension a little clearer by avoiding the firstIteration flag:



            public static IEnumerable<T> WherePreviousReview<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            yield break;

            T previous = collection.First();

            yield return previous;

            foreach (var item in collection.Skip(1))

            if (predicate(previous, item))

            yield return item;


            previous = item;





            This is a version that uses the power of the Where extension:



            public static IEnumerable<T> WherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            T previous = default(T);
            return collection.Where((current, i) =>

            bool result = i == 0 );




            This is a version that uses the Aggregate extension:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new List<T>();

            return collection.Aggregate((0, default(T), new List<T>()), (acc, cur) =>

            if (acc.Item1 == 0 ).Item3;



            The latter is not meant as a better choice, just an example of another approach. The caveat is the inner list object.




            Update



            Aggregate version with named tuples:



            public static IEnumerable<T> NewWherePrevious<T>(this IEnumerable<T> collection, Func<T, T, bool> predicate)

            if (collection == null) throw new ArgumentNullException(nameof(collection));
            if (predicate == null) throw new ArgumentNullException(nameof(predicate));
            if (!collection.Any())
            return new T[0];

            (int index, T prev, List<T> list) seed = (0, default(T), new List<T>());

            return collection.Aggregate(seed, ((int index, T prev, List<T> list) acc, T cur) =>
            ).list;







            share|improve this answer















            share|improve this answer



            share|improve this answer








            edited Feb 13 at 9:48


























            answered Feb 9 at 7:04









            Henrik Hansen

            3,8931417




            3,8931417











            • You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
              – t3chb0t
              Feb 9 at 8:09







            • 1




              @t3chb0t: I totally agree - and updated my answer.
              – Henrik Hansen
              Feb 9 at 8:35










            • The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
              – LadislavBohm
              Feb 9 at 11:47







            • 1




              @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
              – Henrik Hansen
              Feb 9 at 14:24
















            • You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
              – t3chb0t
              Feb 9 at 8:09







            • 1




              @t3chb0t: I totally agree - and updated my answer.
              – Henrik Hansen
              Feb 9 at 8:35










            • The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
              – LadislavBohm
              Feb 9 at 11:47







            • 1




              @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
              – Henrik Hansen
              Feb 9 at 14:24















            You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
            – t3chb0t
            Feb 9 at 8:09





            You should name the tuple properties because the aggregate approach is not necessarily a good advice with all these ItemXs; in fact it's the least readable version of all :-]
            – t3chb0t
            Feb 9 at 8:09





            1




            1




            @t3chb0t: I totally agree - and updated my answer.
            – Henrik Hansen
            Feb 9 at 8:35




            @t3chb0t: I totally agree - and updated my answer.
            – Henrik Hansen
            Feb 9 at 8:35












            The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
            – LadislavBohm
            Feb 9 at 11:47





            The first approach that omits firstIteration flag is very nice. The second approach has one flaw, thats actually a problem for my case and that is that you are iterating the whole collection to filter it. Other solutions rely on yield keyword that should only enumerate it when its actually needed. For large collections it might be a problem.
            – LadislavBohm
            Feb 9 at 11:47





            1




            1




            @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
            – Henrik Hansen
            Feb 9 at 14:24




            @LadislavBohm: I agree. It is not the best or even a good implementation. As written above - just another way.
            – Henrik Hansen
            Feb 9 at 14:24










            up vote
            1
            down vote














            For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing




            Actually there is a LINQ extension which allows you to do that. the Where extension has an overload that uses the item value and the item's index:



            List<int> test1 = new List<int>()

            23,
            45,
            16,
            8,
            90,
            25
            ;
            var test2 = test1.Where((x, i) => (i > 0)?x > test1[i - 1]:false);


            One caveat to this is, it will only work for indexed collections of types that support comparison operators.






            share|improve this answer



























              up vote
              1
              down vote














              For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing




              Actually there is a LINQ extension which allows you to do that. the Where extension has an overload that uses the item value and the item's index:



              List<int> test1 = new List<int>()

              23,
              45,
              16,
              8,
              90,
              25
              ;
              var test2 = test1.Where((x, i) => (i > 0)?x > test1[i - 1]:false);


              One caveat to this is, it will only work for indexed collections of types that support comparison operators.






              share|improve this answer

























                up vote
                1
                down vote










                up vote
                1
                down vote










                For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing




                Actually there is a LINQ extension which allows you to do that. the Where extension has an overload that uses the item value and the item's index:



                List<int> test1 = new List<int>()

                23,
                45,
                16,
                8,
                90,
                25
                ;
                var test2 = test1.Where((x, i) => (i > 0)?x > test1[i - 1]:false);


                One caveat to this is, it will only work for indexed collections of types that support comparison operators.






                share|improve this answer
















                For example to only select a number that is larger than the previous one. I don't currently know of any LINQ method that would allow me to do such a thing




                Actually there is a LINQ extension which allows you to do that. the Where extension has an overload that uses the item value and the item's index:



                List<int> test1 = new List<int>()

                23,
                45,
                16,
                8,
                90,
                25
                ;
                var test2 = test1.Where((x, i) => (i > 0)?x > test1[i - 1]:false);


                One caveat to this is, it will only work for indexed collections of types that support comparison operators.







                share|improve this answer















                share|improve this answer



                share|improve this answer








                edited Feb 9 at 6:54


























                answered Feb 9 at 6:23









                tinstaafl

                5,502625




                5,502625






















                     

                    draft saved


                    draft discarded


























                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f187135%2flinq-method-that-selects-an-item-based-on-previous-one%23new-answer', 'question_page');

                    );

                    Post as a guest













































































                    Popular posts from this blog

                    Greedy Best First Search implementation in Rust

                    Function to Return a JSON Like Objects Using VBA Collections and Arrays

                    C++11 CLH Lock Implementation