zoukankan      html  css  js  c++  java
  • Customer IEnuramble Extension

    public static class IEnurambleExtension
    {
        public static IEnumerable<TSource> DistinctBy<TSource, TKey>
            (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        { 
            HashSet<TKey> keys = new HashSet<TKey>();
    
            foreach (TSource element in source)
                if (keys.Add(keySelector(element))) 
                    yield return element;
        }
    
        public static IEnumerable<int> DistinctByReturnIndexes<TSource, TKey>
            (this IEnumerable<TSource> source, Func<TSource, TKey> keySelector)
        {
            HashSet<TKey> keys = new HashSet<TKey>();
            int i = 0;
            foreach (TSource element in source)
            {
                if (!keys.Add(keySelector(element)))
                    yield return i;
                i++;
            }
        }
    
        public static int FirstContainsWithIndex<TSource>
            (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
        {
            int i = 0;
            foreach (TSource element in source)
            {
                if (predicate(element))
                    return i;
                i++;
            }
    
            return -1;
        }
    
        public static IEnumerable<int> ContainsReturnIndexes<TSource>
            (this IEnumerable<TSource> source, Func<TSource, bool> predicate)
        {
            int i = 0;
            foreach (TSource element in source)
            {
                if (predicate(element))
                   yield return i;
                i++;
            }
        }
    
    
        public static void Print<TSource, TKey>
             (this IEnumerable<TSource> source,  Func<TSource, TKey> keySelector)
            {
                foreach (TSource element in source)
                    Console.WriteLine(keySelector(element));
            }
    }
  • 相关阅读:
    初识Mybatis
    Servlet基础
    JSP数据交互(二)
    JSP九大内置对象
    JSP数据交互(一)
    动态网页开发基础
    使用jquery操作dom
    jQuery中的事件与动画
    jQuery选择器
    [leetcode]198. House Robber小偷抢钱
  • 原文地址:https://www.cnblogs.com/yang_sy/p/3586764.html
Copyright © 2011-2022 走看看