Ken'ichi Matsuyama
Ken'ichi Matsuyama

Reputation: 369

LINQ/Dictionary Connecting two methods into a single one

I've tried like almost everything, I've asked similar question earlier and got some guidlines to do so, but it doesn't work, I mean it works when there are two methods but it hurts to look at all those lines of code that are duplicated. So I need help how to connect those two into a single method.

private Dictionary<string, int> SplitterMP(string[] file, bool distinct, bool pairs)
{
    var query = file
        .SelectMany(i => File.ReadLines(i)
        .SelectMany(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
        .AsParallel()
        .Select(word => word.ToLower())
        .Where(word => !word.All(char.IsDigit)));
    if (pairs)
    {
        var pairWise = query.Pairwise((first, second) => string.Format("{0} {1}", first, second));

        return query
                .Concat(pairWise)
                .GroupBy(word => word)
                .ToDictionary(g => g.Key, g => g.Count());
    }
    return query
        .GroupBy(word => word)
        .ToDictionary(g => g.Key, g => g.Count());
}

private Dictionary<string, int> SplitterS(string[] file, bool distinct, bool pairs)
{
    List<string> allFilesWords = new List<string>();
    foreach (var filename in file)
    {
        var query = File.ReadLines(filename)
            .SelectMany(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
            .AsParallel()
            .Select(word => word.ToLower())
            .Where(word => !word.All(char.IsDigit));
        if (distinct)
        {
            allFilesWords.AddRange(query.Distinct());
        }
    }
    return allFilesWords
        .GroupBy(word => word)
        .ToDictionary(g => g.Key, g => g.Count());
}

So the first function works for pairs = true and distinct = false and the second one works for pairs = false and distinct = true. I want it to be in one single Splitter method to be even able to call both true and not doing some shenanigans like now I do.

Upvotes: 0

Views: 75

Answers (1)

Dave Bish
Dave Bish

Reputation: 19646

I'm not 100% sure what you mean, but can you do this?

private Dictionary<string, int> Splitter(string[] file, bool distinct, bool pairs)
{
    var query = file
        .SelectMany(i => File.ReadLines(i)
        .SelectMany(line => line.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries))
        .AsParallel()
        .Select(word => word.ToLower())
        .Where(word => !word.All(char.IsDigit)));

    if (pairs)
        query = query.Concat(query.Pairwise((first, second) => string.Format("{0} {1}", first, second)));

    if(distinct)
        query = query.Distinct();

    return query
        .GroupBy(word => word)
        .ToDictionary(g => g.Key, g => g.Count());
}

Upvotes: 4

Related Questions