public void TestEmptySet() { var expected = 1.LiftList(); var zipper = ConsListZipper <int> .ToZipper(ConsList.Nil <int>()); var result = zipper.Set(1).ToList(); Assert.AreEqual(expected, result); }
public static StateEither <TState, TLeft, IEnumerable <TRight> > Sequence <TState, TLeft, TRight>(this IEnumerable <StateEither <TState, TLeft, TRight> > stateTs) { return(new StateEither <TState, TLeft, IEnumerable <TRight> >(new State <TState, IEither <TLeft, IEnumerable <TRight> > >(s => { var retval = ConsList.Nil <TRight>().AsRight <TLeft, IConsList <TRight> >(); foreach (var state in stateTs) { var res = state.Out.Run(s); s = res.Item1; retval = from xs in retval from x in res.Item2 select x.Cons(xs); } return Tuple.Create(s, retval.Select(xs => xs.AsEnumerable().Reverse())); }))); }
public void TestApplicative(string firstName, string lastName, int age, int[] expectedErrors) { var m = EnumerableMonoid <Failure> .Only; Func <string, string, int, Person> makePerson = Person.MakePerson; var personOrError = ValidateStringNotNull(firstName).SelectMany(ValidateNameLength) .Select(makePerson.Curry()) .Apply(ValidateStringNotNull(lastName).SelectMany(ValidateNameLength), m) .Apply(ValidateAgeInRange(age), m); var errorCodes = personOrError.Match( success: p => ConsList.Nil <int>(), failure: errors => errors.Select(error => error.ErrorCode).ToConsList()); var expectedErrorList = expectedErrors.ToConsList(); Assert.IsTrue(expectedErrors.ToConsList().Equals(errorCodes)); }
public void TestAsEnumerable(IEnumerable <int> expected, int fake) { var actual = 1.Cons(2.Cons(3.Cons(ConsList.Nil <int>()))).AsEnumerable(); Assert.AreEqual(expected, actual); }
private static IConsList <T> MkList <T>(IEnumerable <T> ts) { return(ts.Reverse().Aggregate(ConsList.Nil <T>(), (lst, t) => t.Cons(lst))); }
/// <summary> /// Alters the zipper to point at the last element in the source list /// </summary> /// <returns>A new zipper with the pointer set to the first element of the source list</returns> public ConsListZipper <T> Last() { return(new ConsListZipper <T>(_after.FoldL(_before, (befores, a) => a.Cons(befores)), ConsList.Nil <T>()).Prev()); }
/// <summary> /// Alters the zipper to point at the first element in the source list /// </summary> /// <returns>A new zipper with the pointer set to the first element of the source list</returns> public ConsListZipper <T> First() { return(new ConsListZipper <T>(ConsList.Nil <T>(), _before.FoldL(_after, (afters, b) => b.Cons(afters)))); }
/// <summary> /// Helper that constructs a zipper from a cons list /// </summary> /// <param name="xs">The list you wish to create a zipper for</param> /// <returns>A new zipper</returns> public static ConsListZipper <T> ToZipper(IConsList <T> xs) { return(new ConsListZipper <T>(ConsList.Nil <T>(), xs)); }
public static Task <IEnumerable <T> > Sequence <T>(this IEnumerable <Task <T> > taskTs) { var initial = Task.FromResult(ConsList.Nil <T>()); return(taskTs.Reverse().Aggregate(initial, (current, task) => current.SelectMany(ts => task.Select(t => t.Cons(ts)))).Select(tasks => tasks.AsEnumerable())); }
/// <summary> /// Sequence takes a list of computations and builds from them a computation which will /// run each in turn and produce a list of the results. /// /// Note that due to C#s lack of higher kinded types, this must be specified for every type of computation /// This is the sequence for IMaybe computations /// </summary> /// <typeparam name="T">Type of value yielded by each computation</typeparam> /// <param name="maybeTs">The list of computations</param> /// <returns>A computation thqt yields a sequence of values of type 'T</returns> public static IMaybe <IEnumerable <T> > Sequence <T>(this IEnumerable <IMaybe <T> > maybeTs) { var initial = ConsList.Nil <T>().ToMaybe(); return(maybeTs.Reverse().Aggregate(initial, (current, maybe) => current.SelectMany(ts => maybe.Select(t => t.Cons(ts)))).Select(xs => xs.AsEnumerable())); }
public static StateIo <TState, IEnumerable <TValue> > Sequence <TState, TValue>(this IEnumerable <StateIo <TState, TValue> > stateTs) { var initial = ConsList.Nil <TValue>().ToStateIo <TState, IConsList <TValue> >(); return(stateTs.Aggregate(initial, (current, anIoState) => current.SelectMany(ts => anIoState.Select(t => t.Cons(ts)))).Select(ioStates => ioStates.AsEnumerable().Reverse())); }
public static IEither <TLeft, IEnumerable <TRight> > Sequence <TLeft, TRight>(this IEnumerable <IEither <TLeft, TRight> > xs) { var initial = ConsList.Nil <TRight>().AsRight <TLeft, IConsList <TRight> >(); return(xs.Aggregate(initial, (current, anEither) => current.SelectMany(ts => anEither.Select(t => t.Cons(ts)))).Select(eithers => eithers.AsEnumerable().Reverse())); }
/// <summary> /// Sequence takes a list of computations and builds from them a computation which will /// run each in turn and produce a list of the results. /// /// Note that due to C#s lack of higher kinded types, this must be specified for every type of computation /// This is the sequence for State computations /// </summary> /// <typeparam name="TState">Type of associated state</typeparam> /// <typeparam name="T">Type of value yielded by each computation</typeparam> /// <param name="states">The list of computations</param> /// <returns>A computation thqt yields a sequence of values of type 'T</returns> public static State <TState, IEnumerable <T> > Sequence <TState, T>(this IEnumerable <State <TState, T> > states) { var initial = ConsList.Nil <T>().Insert <TState, IConsList <T> >(); return(states.Aggregate(initial, (current, s) => current.SelectMany(ts => s.Select(t => t.Cons(ts)))).Select(x => x.AsEnumerable().Reverse())); }