public static void ForEach <T>(this FuncSet <IContainer <T> > f, Action <T> a) { foreach (var t in f.set) { a(t.Value); } }
public static void LinqFuncSetTest() { var init = 5; var computation = from x in MaybeT.Lift(FuncSet.Make("a", "b", "c")) from y in MaybeT.Lift(FuncSet.Make("c", "d", "e")) from _ in MaybeT.HoistFuncSet(Maybe.JustIf(init > 0, () => new Unit())) from z in MaybeT.Lift(FuncSet.Make("f")) select x + y + z; var value = computation.Run; var expected = new[] { "acf", "adf", "aef", "bcf", "bdf", "bef", "ccf", "cdf", "cef" }.Select(x => Maybe.Just(x)).MakeFuncSet(); Assert.Equal(expected, value); init = 0; computation = from x in MaybeT.Lift(FuncSet.Make("a", "b", "c")) from y in MaybeT.Lift(FuncSet.Make("c", "d", "e")) from _ in MaybeT.HoistFuncSet(Maybe.JustIf(init > 0, () => new Unit())) from z in MaybeT.Lift(FuncSet.Make("f")) select x + y + z; value = computation.Run; expected = FuncSet.Make(Maybe.Nothing <string>()); Assert.Equal(expected, value); }
public static IEnumerable <object[]> Ap2Data() { yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(), FuncSet.Make <int>(), FuncSet.Make <int>(), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(), FuncSet.Make(1), FuncSet.Make <int>(), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(), FuncSet.Make(1), FuncSet.Make(1), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(x => y => x + y), FuncSet.Make <int>(), FuncSet.Make <int>(), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(x => y => x + y), FuncSet.Make(4), FuncSet.Make(6), FuncSet.Make(10), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(x => y => x + y), FuncSet.Make(1, 2, 3), FuncSet.Make(7, 8, 9), FuncSet.Make(8, 9, 10, 9, 10, 11, 10, 11, 12), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(x => y => x + y, x => y => x * y), FuncSet.Make(5), FuncSet.Make(8), FuncSet.Make(13, 40), }); yield return(new object[] { FuncSet.Make <Func <int, Func <int, int> > >(x => y => x + y, x => y => x * y), FuncSet.Make(5, 6, 7), FuncSet.Make(10, 20, 30), FuncSet.Make(15, 25, 35, 16, 26, 36, 17, 27, 37, 50, 100, 150, 60, 120, 180, 70, 140, 210), }); }
/// <summary> /// Equivalent to <see cref="IMonad{TSource}"/>, but restricted to <see cref="FuncSet{T}"/>. Offers LINQ query support with multiple <c>from</c>-clauses. /// </summary> /// <typeparam name="TSource">The type of the source's value.</typeparam> /// <typeparam name="TMiddle">The type of the selector's result.</typeparam> /// <typeparam name="TResult">The type of the result's value.</typeparam> /// <param name="source">The source.</param> /// <param name="f">The function to apply.</param> /// <param name="resultSelector">The result-selector.</param> public static FuncSet <TResult> SelectMany <TSource, TMiddle, TResult> (this FuncSet <TSource> source, Func <TSource, FuncSet <TMiddle> > f, Func <TSource, TMiddle, TResult> resultSelector) { var enumSource = ((IEnumerable <TSource>)source); return(new FuncSet <TResult>(enumSource.SelectMany(x => f(x), resultSelector))); }
//-------------------------------------------// /// <summary> /// Initialize a table entity for the specified generic type. /// </summary> public TypeMap(Type type = null) : base(type ?? typeof(TEntity), (type ?? typeof(TEntity)).Name) { // iterate through the properties of the table entity foreach (PropertyInfo info in this.NetType.GetProperties(BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.Public | BindingFlags.FlattenHierarchy)) { this.AddPropertyMapping(info, info.Name); } // create the activator for the cell type defined activator = Dynamic.Constructor <FuncSet <TEntity> >(type); }
/// <summary> /// Initializes a new threadsafe value with a function to get the initial item value. /// </summary> public ThreadItem(Func <T> getItem) { _getItem = new FuncSet <T>(getItem); _items = new Dictionary <int, T>(); Reset(); ThreadHandle.Handles.Take(); ThreadHandle.Handles.Item.OnAdd += OnAdd; ThreadHandle.Handles.Item.OnRemove += OnRemove; ThreadHandle.Handles.Release(); _items[Thread.CurrentThread.ManagedThreadId] = getItem == null ? default(T) : getItem(); }
public static IEnumerable <object[]> EqualsData() { yield return(new object[] { FuncSet.Make <int>(), FuncSet.Make <int>(), true }); yield return(new object[] { FuncSet.Make(1), FuncSet.Make(1), true }); yield return(new object[] { FuncSet.Make(1, 2, 3), FuncSet.Make(1, 2, 3), true }); yield return(new object[] { FuncSet.Make(4), FuncSet.Make <int>(), false }); yield return(new object[] { FuncSet.Make <int>(), FuncSet.Make(7), false }); yield return(new object[] { FuncSet.Make(1, 2), FuncSet.Make(5, 6), false }); yield return(new object[] { FuncSet.Make(1, 2), FuncSet.Make(1), false }); yield return(new object[] { FuncSet.Make(1, 2, 3), FuncSet.Make(1, 2, 3, 4), false }); }
public static void FuncSetAdd(int[] xs, int elem) { var fl = new FuncSet <int>(xs); var origLength = fl.Count; fl.Add(elem); var set = new HashSet <int>(xs ?? new int[0]) { elem }; Assert.Equal(set, fl); Assert.Equal(set.Count, fl.Count); }
/// <summary> /// Initializes a new threadsafe value as either default or with a function to get the item. /// </summary> public ThreadItem(FuncSet <T> getItem = null) { _getItem = getItem; _items = new Dictionary <int, T>(); Reset(); ThreadHandle.Handles.Take(); ThreadHandle.Handles.Item.OnAdd += OnAdd; ThreadHandle.Handles.Item.OnRemove += OnRemove; ThreadHandle.Handles.Release(); if (_getItem == null) { _items[Thread.CurrentThread.ManagedThreadId] = default(T); } else { _items[Thread.CurrentThread.ManagedThreadId] = _getItem.Run(); } }
public static IEnumerable <object[]> Ap1Data() { yield return(new object[] { FuncSet.Make <Func <int, int> >(), FuncSet.Make <int>(), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, int> >(), FuncSet.Make(1), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, int> >(x => x + 1), FuncSet.Make <int>(), FuncSet.Make <int>(), }); yield return(new object[] { FuncSet.Make <Func <int, int> >(x => x + 1), FuncSet.Make(4), FuncSet.Make(5), }); yield return(new object[] { FuncSet.Make <Func <int, int> >(x => x + 1), FuncSet.Make(1, 2, 3), FuncSet.Make(2, 3, 4), }); yield return(new object[] { FuncSet.Make <Func <int, int> >(x => x + 1, x => x * 2), FuncSet.Make(5), FuncSet.Make(6, 10), }); yield return(new object[] { FuncSet.Make <Func <int, int> >(x => x + 1, x => x * 2), FuncSet.Make(5, 6, 7), FuncSet.Make(6, 7, 8, 10, 12, 14), }); }
private bool validateFunction(int function) { return(FuncSet.Contains(function)); }
public static void FuncSetEquals(FuncSet <int> xs, ISet <int> ys, bool expected) { Assert.Equal(expected, xs.Equals(ys)); }
public static void FuncSetAp2(FuncSet <Func <int, Func <int, int> > > funcs, FuncSet <int> args1, FuncSet <int> args2, IEnumerable <int> expected) { var res = args2.Ap(args1.Ap(funcs)); Assert.Equal(expected, res as IEnumerable <int>); }
/// <summary> /// Equivalent to <see cref="IFunctor{TSource}.Map{TResult}(Func{TSource, TResult})"/>, but restricted to <see cref="FuncSet{T}"/>. Offers LINQ query support with one <c>from</c>-clause. /// </summary> /// <typeparam name="TSource">The type of the source's value.</typeparam> /// <typeparam name="TResult">The type of the result's value.</typeparam> /// <param name="source">The source.</param> /// <param name="f">The function to apply.</param> public static FuncSet <TResult> Select <TSource, TResult>(this FuncSet <TSource> source, Func <TSource, TResult> f) => (FuncSet <TResult>)source.Map(f);
public static void RemoveNull <T>(this FuncSet <IContainer <T> > f) { f.set.RemoveWhere(f => f.Value == null); }