https://www.codewars.com/kata/zip-it/train/csharp
using System; using System.Collections.Generic; using System.Linq; /// <summary> /// https://www.codewars.com/kata/zip-it/train/csharp /// </summary> public static class Kata { public static object[] ZipIt(this object[] a, object[] b, Func<object, object, object> fn) { if (a.Length > b.Length) { a = a.Take(b.Length).ToArray(); } else if (a.Length < b.Length) { b = b.Take(a.Length).ToArray(); } var list = new List<object>(); for (int i = 0; i < a.Length; i++) { var temp = fn(a[i], b[i]); list.Add(temp); } return list.ToArray(); } }
优化:
return a.Select((t, i) => fn(t, b[i])).ToArray();
public static class Kata { public static object[] ZipIt(this object[] a, object[] b, Func<object, object, object> fn) { return a.Zip(b, fn).ToArray(); } }
public static class Kata { public static object[] ZipIt(this object[] a, object[] b, Func<object,object,object> fn) { return Enumerable.Zip(a, b, fn).ToArray(); } }