using System; namespace dk.nsi.seal.FunctionalExt { // More can be done to make this more pleasant to work with - feel free to change/modify this public abstract class Option { public Option Map(Func a) => FlatMap(v => new Some(a(v))); public abstract Option FlatMap(Func> a); public T IfNone(T v) => IfNone(() => v); public T IfNone(Func f) => Match(id => id, f); public abstract R Match(Func some, Func none); public static implicit operator Option(T v) => new Some(v); public static Option None() => new NoneValue(); private class Some : Option { private readonly T value; public Some(T value) { if(value == null) throw new ArgumentException("Option cannot contain null. " + "Use None to represent the absence of data."); this.value = value; } public override Option FlatMap(Func> a) => a(value); public override R Match(Func some, Func none) => some(value); } private class NoneValue : Option { public override Option FlatMap(Func> a) => new NoneValue(); public override R Match(Func some, Func none) => none(); } } }