Yan Cui
I help clients go faster for less using serverless technologies.
Another gem I found on StackOverflow today (I’ve been spending a lot of time there these last couple of days..), this time in the form of a question on how to append or prepend a single value to an IEnumerable<T>.
Greg provided an elegant solution to this particular problem, and here’s his answer:
public static IEnumerable<T> Append<T>(this IEnumerable<T> source, T item) { foreach (T i in source) yield return i; yield return item; } public static IEnumerable<T> Prepend<T>(this IEnumerable<T> source, T item) { yield return item; foreach (T i in source) yield return i; }
Which you can use like this:
var all = GetHeaders().Append(GetData());
As others have stated, what’s so nice about this solution is that it does not mutate the original collection, instead it generates a new IEnumerbale<T>.
Whenever you’re ready, here are 4 ways I can help you:
- If you want a one-stop shop to help you quickly level up your serverless skills, you should check out my Production-Ready Serverless workshop. Over 20 AWS Heroes & Community Builders have passed through this workshop, plus 1000+ students from the likes of AWS, LEGO, Booking, HBO and Siemens.
- If you want to learn how to test serverless applications without all the pain and hassle, you should check out my latest course, Testing Serverless Architectures.
- If you’re a manager or founder and want to help your team move faster and build better software, then check out my consulting services.
- If you just want to hang out, talk serverless, or ask for help, then you should join my FREE Community.