static (IExampleSpace <T>?exampleSpace, int countTaken) TakeRec(IExampleSpace <T> exampleSpace, int count) { if (count <= 0) { return(null, 0); } var takenSubspace = new List <IExampleSpace <T> >(); var countTaken = 1; foreach (var subExampleSpace in exampleSpace.Subspace) { if (countTaken >= count) { break; } var(takenSubExampleSpace, subCountTaken) = TakeRec(subExampleSpace, count - countTaken); if (takenSubExampleSpace != null) { takenSubspace.Add(takenSubExampleSpace); } countTaken += subCountTaken; } var takenExampleSpace = ExampleSpaceFactory.Create <T>(exampleSpace.Current, takenSubspace); return(takenExampleSpace, countTaken); }
static IExampleSpace <TResult> JoinExampleSpaces( IExampleSpace <T> leftExampleSpace, IExampleSpace <TResult> rightExampleSpace, Func <T, IGen <TResult> > selector, GenParameters parameters) { var jointExampleSpace = rightExampleSpace.MapExamples(example => new Example <TResult>( ExampleId.Combine(leftExampleSpace.Current.Id, example.Id), example.Value, leftExampleSpace.Current.Distance + example.Distance)); var boundLeftSubspace = leftExampleSpace.Subspace.SelectMany(leftExampleSubspace => BindSubspace(leftExampleSubspace, selector, parameters)); return(ExampleSpaceFactory.Create( jointExampleSpace.Current, Enumerable.Concat(boundLeftSubspace, rightExampleSpace.Subspace))); }