using System; using System.Collections.Generic; namespace Chernobyl { /// /// Utility and extension methods for . /// public static class LazyExt { /// /// Returns a function that returns the value of the when invoked. /// public static Func AsFunc(this Lazy lazy) => () => lazy.Value; /// /// Wraps the specified object in a . /// public static Lazy AsLazy(this T instance) => new Lazy(() => instance); /// /// Returns the function in the form of a . /// public static Lazy AsLazy(this Func func) => new Lazy(func); /// /// Returns a that returns a projection of /// using as the projector. /// public static Lazy Select(this Lazy source, Func selector) => source.AsFunc().Select(selector).AsLazy(); /// /// Performs an upcast from to . /// public static Lazy Upcast(this Lazy func) where TSource : TReturn => func.Select(v => v); /// /// Returns the within the without /// creating the value within the . /// public static IEnumerable Switch(this Lazy> lazy) => lazy.AsFunc().Switch(); /// /// Causes the to create its value. /// public static void Create(this Lazy lazy) => lazy.AsFunc().Invoke(); /// /// Returns a that invokes with the value /// returned by . /// /// The instance to take the value from. /// The method to invoke after a value is created from . public static Lazy AfterCreate(this Lazy lazy, Action action) => new Lazy(() => { var result = lazy.Value; action(result); return result; }); /// /// Returns a that, when accessed, returns the result of a call to /// . /// public static Lazy Invoke(this Lazy> func) => func.Select(f => f()); /// /// Returns a that, when accessed, returns the result of a call to /// . /// public static Lazy Invoke(this Lazy> func, T1 arg1) => func.Select(f => f(arg1)); } }