about summary refs log blame commit diff
path: root/lib/Extensions.cs
blob: 3c81f17e367539d428361b03fbf4241dc8e90739 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13

                                 










                                                                                                        










                                                                                                        






                                                                                               

     
using System;
using System.Collections.Generic;
using System.Linq;

namespace aoc2019.lib
{
    public static class Extensions
    {
        public static IEnumerable<IEnumerable<T>> Permute<T>(this IEnumerable<T> list)
        {
            if (list.Count() == 1) return new[] { list };
            return list.SelectMany(t => Permute(list.Where(x => !x.Equals(t))), (v, p) => p.Prepend(v));
        }

        public static IEnumerable<string> Chunk(this string str, int chunkSize)
        {
            for (int i = 0; i < str.Length; i += chunkSize)
                yield return str.Substring(i, chunkSize);
        }

        public static string ToDelimitedString<T>(this IEnumerable<T> enumerable, string delimiter = "")
        {
            return string.Join(delimiter, enumerable);
        }

        public static IEnumerable<T> Repeat<T>(this IEnumerable<T> sequence, int? count = null)
        {
            while (count == null || count-- > 0)
                foreach (var item in sequence)
                    yield return item;
        }
    }
}