/// <summary> /// Maps all of the examples in an example space by the given selector function. /// </summary> /// <typeparam name="T">The type of the target example space's values.</typeparam> /// <typeparam name="TResult">The new type of an example's value</typeparam> /// <param name="exampleSpace">The example space to operate on.</param> /// <param name="selector">A function to apply to each example in the example space.</param> /// <returns>A new example space with the mapping function applied.</returns> public static IExampleSpace <TResult> MapExamples <T, TResult>( this IExampleSpace <T> exampleSpace, Func <IExample <T>, IExample <TResult> > f) { return(ExampleSpaceFactory.Create( f(exampleSpace.Current), exampleSpace.Subspace.Select(es => MapExamples(es, f)))); }
/// <summary> /// Filters the examples in an example space by the given predicate. /// </summary> /// <typeparam name="T">The type of the target example space's values.</typeparam> /// <param name="exampleSpace">The example space to operate on.</param> /// <param name="pred">The predicate used to test each value in the example space.</param> /// <returns>A new example space, containing only the examples whose values passed the predicate.</returns> public static IExampleSpace <T>?Filter <T>( this IExampleSpace <T> exampleSpace, Func <T, bool> pred) { if (pred(exampleSpace.Current.Value) == false) { return(null); } return(ExampleSpaceFactory.Create( exampleSpace.Current, exampleSpace.Subspace .Select(es => es.Filter(pred)) .Where(es => es != null) .Cast <IExampleSpace <T> >())); }
public static IExampleSpace <T> Cast <T>(this IExampleSpace exampleSpace) { T newValue; try { newValue = (T)exampleSpace.Current.Value; } catch { newValue = (T)Convert.ChangeType(exampleSpace.Current.Value, typeof(T)); } var example = new Example <T>(exampleSpace.Current.Id, newValue, exampleSpace.Current.Distance); var subspace = exampleSpace.Subspace.Select(child => Cast <T>(child)); return(ExampleSpaceFactory.Create(example, subspace)); }