static void Main(string[] args) { // Listing 3.2 Constructing BCL immutable collections var list = ImmutableList.Create <int>(); //#A list = list.Add(1); //#B list = list.Add(2); list = list.Add(3); var builder = ImmutableList.CreateBuilder <int>(); //#C builder.Add(1); //#D builder.Add(2); builder.Add(3); list = builder.ToImmutable(); //#E // Listing 3.14 A functional list in C# FList <int> list1 = FList <int> .Empty; FList <int> list2 = list1.Cons(1).Cons(2).Cons(3); FList <int> list3 = FList <int> .Cons(1, FList <int> .Empty); FList <int> list4 = list2.Cons(2).Cons(3); Demo.PrintSeparator(); Console.WriteLine("Listing 3.15 Lazy list implementation"); var lazyList1 = new LazyList <int>(42, new Lazy <LazyList <int> >(() => new LazyList <int>(21, LazyList <int> .Empty))); var lazyList = lazyList1.Append( new LazyList <int>(3, LazyList <int> .Empty)); lazyList.Iterate(Console.WriteLine); Demo.PrintSeparator(); Console.WriteLine("Listing 3.16 Immutable B-tree representation"); var tree = new BinaryTree <int>(20, new BinaryTree <int>(9, new BinaryTree <int>(4, new BinaryTree <int>(2, null, null), null), new BinaryTree <int>(10, null, null)), null); var exist9 = tree.Contains(9); Console.WriteLine($"Tree contains 9 : {exist9}"); var tree21 = tree.Insert(21); var exist21 = tree21.Contains(21); Console.WriteLine($"Tree21 contains 21 : {exist21}\n"); tree.InOrder(Console.WriteLine); Console.ReadLine(); }
public static FList <T> Where <T> (this FList <T> list, Func <T, bool> predicate) { if (list.IsEmpty) { return(list); } var newTail = list.Tail.Where(predicate); return(predicate(list.Head) ? newTail.Cons(list.Head) : newTail); }
public FList <T> Cons(T element) //#G { return(FList <T> .Cons(element, this)); }
} //#E public static FList <T> Cons(T head, FList <T> tail) //#F { return(tail.IsEmpty ? new FList <T>(head, Empty) : new FList <T>(head, tail)); }