Run Code
|
API
|
Code Wall
|
Users
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
Enumerable expansion
//Rextester.Program.Main is the entry point for your code. Don't change it. //Compiler version 4.0.30319.17929 for Microsoft (R) .NET Framework 4.5 using System; using System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; namespace Rextester { public class Program { public static void Main(string[] args) { int[] data = new int[5]; Random rand = new Random(); data = data.Fill(() => rand.Next()).ToArray(); Console.WriteLine(data.ArrString()); Console.WriteLine(data .Map(i => BitConverter.GetBytes(i)) .ArrString() ); Console.WriteLine(data .Map(i => BitConverter.GetBytes(i)) .Expand() .Map(b => b.ToString("x2")) .ArrString() ); } } static class Extn { public static IEnumerable<Out> Map<In,Out>(this IEnumerable<In> a, Func<In,Out> f) { foreach(In i in a) yield return f(i); } public static IEnumerable<T> Expand<T>(this IEnumerable<IEnumerable<T>> a) { foreach(IEnumerable<T> e in a) foreach(T t in e) yield return t; } public static Out Fold<In,Out>(this IEnumerable<In> a, Out initial, Func<In,Out,Out> f) { Out tor = initial; foreach(In i in a) tor = f(i, tor); return tor; } public static string ArrString<T>(this IEnumerable<T> a) { string res = a.Fold("[", (i, o) => o + i.ToString() + ", "); return res.Remove(res.Length - 2) + "]"; } public static IEnumerable<T> Fill<T>(this IEnumerable<T> a, Func<T> f) { int L = a.Count(); for(int x = 0; x < L; x++) yield return f(); } } }
run
|
edit
|
history
|
help
0
Please
log in
to post a comment.
Doubt-" 1 parameter Gen _Derive_Class inherit the 2 parameter Gen_Base_Class"
IEquatable_resolved
my first
Riemann Sums
Symantic check for object type variables
Fórum Multiple Tests Returning Error Message
eg
InvokeProgram
Classes, properties, methods, ling begining
Enumerable
Please log in to post a comment.