Skip to content

Instantly share code, notes, and snippets.

@smudge202
Created September 25, 2015 07:34
Show Gist options
  • Save smudge202/502914250d01f77eea3e to your computer and use it in GitHub Desktop.
Save smudge202/502914250d01f77eea3e to your computer and use it in GitHub Desktop.
How to do fluent enumerable method mentioned in todo (@bathroomcommits)
public class Demand
{
public static DemandResult That(Func<bool> predicate) => new DemandResult(predicate);
public static void That(bool predicate, string reason)
{
if (!predicate) throw new InvalidOperationException(reason);
}
public static DemandSetup<IEnumerable<T>> That<T>(IEnumerable<T> source) => new EnumerableDemand<T>(source);
}
public interface DemandSetup<T>
{
T Source { get; }
}
internal sealed class EnumerableDemand<T> : DemandSetup<IEnumerable<T>>
{
public IEnumerable<T> Source { get; }
public EnumerableDemand(IEnumerable<T> source) { Source = source; }
}
public static class EnumerableDemands
{
public static void NonEmpty<T>(this DemandSetup<IEnumerable<T>> setup, string reason)
{
if (!setup.Source.Any()) throw new InvalidOperationException(reason);
}
}
@MattJOlson
Copy link

Nice! It's a lot closer to what I had in mind; thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment