gfoidl / Stochastics

Stochastic tools, distrubution, analysis
MIT License
3 stars 0 forks source link

Enable Sample for fluent-interface scenarios #24

Closed gfoidl closed 6 years ago

gfoidl commented 6 years ago

Instead of Ctor consider static factory methods.

public static Sample Create(IEnumerable<double> values);

Cf. https://github.com/gfoidl/Stochastics/pull/22#issuecomment-359005151

gfoidl commented 6 years ago

Although #22 got closed, a fluent scenario would be great.

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    static class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<double> rawValues = Enumerable.Range(0, 5).Select(i => (double)i);

            var rawSample        = new Sample();
            var compressedSample = new Sample();

            var compressedValues = rawValues
                .AddWithIteration(rawSample)
                .Compress()
                .AddWithIteration(compressedSample)
                .ToList();
        }

        private static IEnumerable<double> Compress(this IEnumerable<double> values)
        {
            foreach (double item in values)
                yield return item;
        }
    }

    public class Sample
    {
        public void Add(double item)
        { }
    }

    public static class SampleExtensions
    {
        public static IEnumerable<double> AddWithIteration(this IEnumerable<double> values, Sample sample)
        {
            foreach (double item in values)
            {
                sample.Add(item);
                yield return item;
            }
        }
    }
}
gfoidl commented 6 years ago

It is better to have a SampleBuilder instead of extension methods for Sample

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApp1
{
    static class Program
    {
        static void Main(string[] args)
        {
            IEnumerable<double> rawValues = Enumerable.Range(0, 5).Select(i => (double)i);

            var rawSampleBuilder        = new SampleBuilder();
            var compressedSampleBuilder = new SampleBuilder();

            var compressedValues = rawValues
                .AddToSampleBuilder(rawSampleBuilder)
                .Compress()
                .AddToSampleBuilder(compressedSampleBuilder)
                .ToList();

            Sample rawSample        = rawSampleBuilder.GetSample();
            Sample compressedSample = compressedSampleBuilder.GetSample();
        }

        private static IEnumerable<double> Compress(this IEnumerable<double> values)
        {
            foreach (double item in values)
                yield return item;
        }
    }

    public class Sample
    {
        private readonly double[] _values;

        public Sample(IEnumerable<double> values) => _values = values.ToArray();
    }

    public class SampleBuilder
    {
        private readonly List<double> _values = new List<double>();

        public IEnumerable<double> Add(IEnumerable<double> values)
        {
            foreach (double item in values)
            {
                _values.Add(item);
                yield return item;
            }
        }

        public Sample GetSample() => new Sample(_values);
    }

    public static class SampleBuilderExtensions
    {
        public static IEnumerable<double> AddToSampleBuilder(this IEnumerable<double> values, SampleBuilder sampleBuilder)
        {
            return sampleBuilder.Add(values);
        }
    }
}

This solution is the "better" #22