static ZipList <ZipList <T> > Transpose2 <T>(this ZipList <ZipList <T> > matrix) { if (matrix.IsEmpty) { return(ZipList.Pure(ZipList.Empty <T>())); } else { return (from m in matrix.Value join r in Transpose(matrix.Tail.Value) on 1 equals 1 select ZipList.Cons <T>(m, new Lazy <ZipList <T> >(() => r))); } }
// The transpose function implemented using the // lazy functional list declared above #region Alternative implementation of Transpose static ZipList <ZipList <T> > Transpose <T>(this ZipList <ZipList <T> > matrix) { if (matrix.IsEmpty) { return(ZipList.Pure(ZipList.Empty <T>())); } else { return (ZipList. PureFunc((T v, ZipList <T> l) => ZipList.Cons(v, new Lazy <ZipList <T> >(() => l))). Apply(matrix.Value). Apply(Transpose(matrix.Tail.Value))); } }
// Similar demos as those from blog post, but based // on the functional lazy list implementation #region Alternative demos private static void ZipListDemos() { var nums1 = ZipList.Range(0, 10); var nums2 = ZipList.Range(0, 10); var q = from n in nums1 join m in nums2 on 1 equals 1 select n + m; foreach (var z in q.AsEnumerable()) { Console.Write("{0}, ", z); } Console.WriteLine(); Console.WriteLine(); var matrix = new[] { new[] { 1, 2, 3 }, new[] { 10, 20, 30 }, new[] { 100, 200, 300 } }; var zipMatrix = matrix.Select(ZipList.ToZipList).ToZipList(); var transposed = zipMatrix.Transpose2(); foreach (var a in transposed.AsEnumerable()) { foreach (var b in a.AsEnumerable()) { Console.Write("{0}, ", b); } Console.WriteLine(); } }