This code works but it can be improved.
Created
January 30, 2024 00:17
-
-
Save karenpayneoregon/e823f866a47162c34523683c1131c0e8 to your computer and use it in GitHub Desktop.
Provides code to show start and end index for each element in a list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class Container<T> | |
{ | |
public T? Value { get; set; } | |
public Index StartIndex { get; set; } | |
public Index EndIndex { get; set; } | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var Months = DateTimeFormatInfo.CurrentInfo.MonthNames[..^1].ToList(); | |
var containers = Months.Get(); | |
foreach (var container in containers) | |
{ | |
Debug.WriteLine($"{container.StartIndex,-4}{container.EndIndex,-4}{container.Value}"); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static class RangeHelpers | |
{ | |
public static List<Container<T>> Get<T>(this List<T> list) => | |
list.Select((element, index) => new Container<T> | |
{ | |
Value = element, | |
StartIndex = new Index(index), | |
EndIndex = new Index(Enumerable.Range(0, list.Count).Reverse().ToList()[index], true) | |
}).ToList(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment