Created
April 5, 2015 11:51
-
-
Save LukaHorvat/c63af608a28fac017977 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static Gen<ConsList<T>> Sequence<T>(this ConsList<Gen<T>> list) | |
{ | |
if (list.IsNil) return Return(ConsList.Nil<T>()); | |
return list.Head.Bind(t => | |
list.Tail.Sequence().Bind(ts => | |
Return(ts.Prepend(t)))); | |
} | |
public static Gen<T> Return<T>(T value) => FromGenerator(rnd => value); | |
public static Gen<TB> Ap<T, TB>(this Gen<Func<T, TB>> gf, Gen<T> gen) => | |
gf.Bind(f => gen.Bind(a => Return(f(a)))); | |
public static Gen<int> To(int max) => FromGenerator(rand => rand.GetInt % (max + 1)); | |
public static Gen<T> OneOf<T>(ConsList<Gen<T>> list) => | |
To(list.Count() - 1).Bind(n => list[n]); | |
public static Gen<T> ElementOf<T>(ConsList<T> list) => | |
OneOf(list.Select(t => Return(t)).ToConsList()); | |
public static Gen<ConsList<T>> ListOf<T>(this Gen<T> gen, int n) => | |
Enumerable.Repeat(gen, n).ToConsList().Sequence(); | |
public static Gen<T> FromGenerator<T>(Func<ImmutableRandom, T> g) => new Gen<T>(g); | |
public static T RunGen<T>(this Gen<T> gen, ImmutableRandom rand) => gen.generator(rand).Item1; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment