// // SequenceExtensions.cs // // Authors: // Alexander Chebaturkin (chebaturkin@gmail.com) // // Copyright (C) 2011 Alexander Chebaturkin // // Permission is hereby granted, free of charge, to any person obtaining // a copy of this software and associated documentation files (the // "Software"), to deal in the Software without restriction, including // without limitation the rights to use, copy, modify, merge, publish, // distribute, sublicense, and/or sell copies of the Software, and to // permit persons to whom the Software is furnished to do so, subject to // the following conditions: // // The above copyright notice and this permission notice shall be // included in all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. // using System; using System.Collections.Generic; namespace Mono.CodeContracts.Static.DataStructures { public static class SequenceExtensions { public static Sequence Cons (this Sequence rest, T elem) { return Sequence.Cons (elem, rest); } public static Sequence Append (this Sequence list, Sequence append) { if (list == null) return append; if (append == null) return list; return Cons (list.Tail.Append (append), list.Head); } public static Sequence Where (this Sequence list, Predicate keep) { if (list == null) return null; Sequence rest = list.Tail.Where (keep); if (!keep (list.Head)) return rest; if (rest == list.Tail) return list; return Cons (rest, list.Head); } public static void Apply (this Sequence list, Action action) { Sequence.Apply (list, action); } public static IEnumerable AsEnumerable (this Sequence list) { return Sequence.PrivateGetEnumerable (list); } public static bool Any (this Sequence list, Predicate predicate) { if (list == null) return false; if (predicate (list.Head)) return true; return list.Tail.Any (predicate); } public static bool All (this Sequence list, Predicate predicate) { if (list == null) return true; if (!predicate (list.Head)) return false; return list.Tail.All (predicate); } public static int Length (this Sequence list) { return Sequence.LengthOf (list); } public static bool IsEmpty (this Sequence list) { return list == null; } public static Sequence Select (this Sequence list, Func selector) { return Sequence.Select (list, selector); } public static T Last (this Sequence list) { if (list == null) return default(T); while (Sequence.LengthOf (list) > 1) list = list.Tail; return list.Head; } public static Sequence Coerce (this Sequence list) where S : T { return list.Select (l => (T) l); } public static Sequence Reverse (this Sequence list) { Sequence rest = null; for (Sequence cur = list; cur != null; cur = cur.Tail) rest = rest.Cons (cur.Head); return rest; } public static void ForEach (this Sequence seq, Action action) { if (seq == null) return; action (seq.Head); seq.Tail.ForEach (action); } } }