Avatar of artemkorsakov

artemkorsakov's solution

to Strain in the C# Track

Published at Feb 25 2019 · 0 comments
Instructions
Test suite
Solution

Implement the keep and discard operation on collections. Given a collection and a predicate on the collection's elements, keep returns a new collection containing those elements where the predicate is true, while discard returns a new collection containing those elements where the predicate is false.

For example, given the collection of numbers:

  • 1, 2, 3, 4, 5

And the predicate:

  • is the number even?

Then your keep operation should produce:

  • 2, 4

While your discard operation should produce:

  • 1, 3, 5

Note that the union of keep and discard is all the elements.

The functions may be called keep and discard, or they may need different names in order to not clash with existing functions or concepts in your language.

Restrictions

Keep your hands off that filter/reject/whatchamacallit functionality provided by your standard library! Solve this one yourself using other basic tools instead.

Running the tests

To run the tests, run the command dotnet test from within the exercise directory.

Initially, only the first test will be enabled. This is to encourage you to solve the exercise one step at a time. Once you get the first test passing, remove the Skip property from the next test and work on getting that test passing. Once none of the tests are skipped and they are all passing, you can submit your solution using exercism submit Strain.cs

Further information

For more detailed information about the C# track, including how to get help if you're having trouble, please visit the exercism.io C# language page.

Source

Conversation with James Edward Gray II https://twitter.com/jeg2

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

StrainTest.cs

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

public class StrainTest
{
    [Fact]
    public void Empty_keep()
    {
        Assert.Equal(new LinkedList<int>(), new LinkedList<int>().Keep(x => x < 10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Keep_everything()
    {
        Assert.Equal(new HashSet<int> { 1, 2, 3 }, new HashSet<int> { 1, 2, 3 }.Keep(x => x < 10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Keep_first_and_last()
    {
        Assert.Equal(new[] { 1, 3 }, new[] { 1, 2, 3 }.Keep(x => x % 2 != 0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Keep_neither_first_nor_last()
    {
        Assert.Equal(new List<int> { 2, 4 }, new List<int> { 1, 2, 3, 4, 5 }.Keep(x => x % 2 == 0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Keep_strings()
    {
        var words = "apple zebra banana zombies cherimoya zelot".Split(' ');
        Assert.Equal("zebra zombies zelot".Split(' '), words.Keep(x => x.StartsWith("z")));
    }

    [Fact(Skip = "Remove to run test")]
    public void Keep_arrays()
    {
        var actual = new[]
            {
                new[] { 1, 2, 3 },
                new[] { 5, 5, 5 },
                new[] { 5, 1, 2 },
                new[] { 2, 1, 2 },
                new[] { 1, 5, 2 },
                new[] { 2, 2, 1 },
                new[] { 1, 2, 5 }
            };
        var expected = new[] { new[] { 5, 5, 5 }, new[] { 5, 1, 2 }, new[] { 1, 5, 2 }, new[] { 1, 2, 5 } };
        Assert.Equal(expected, actual.Keep(x => x.Contains(5)));
    }

    [Fact(Skip = "Remove to run test")]
    public void Empty_discard()
    {
        Assert.Equal(new LinkedList<int>(), new LinkedList<int>().Discard(x => x < 10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Discard_nothing()
    {
        Assert.Equal(new HashSet<int> { 1, 2, 3 }, new HashSet<int> { 1, 2, 3 }.Discard(x => x > 10));
    }

    [Fact(Skip = "Remove to run test")]
    public void Discard_first_and_last()
    {
        Assert.Equal(new[] { 2 }, new[] { 1, 2, 3 }.Discard(x => x % 2 != 0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Discard_neither_first_nor_last()
    {
        Assert.Equal(new List<int> { 1, 3, 5 }, new List<int> { 1, 2, 3, 4, 5 }.Discard(x => x % 2 == 0));
    }

    [Fact(Skip = "Remove to run test")]
    public void Discard_strings()
    {
        var words = "apple zebra banana zombies cherimoya zelot".Split(' ');
        Assert.Equal("apple banana cherimoya".Split(' '), words.Discard(x => x.StartsWith("z")));
    }

    [Fact(Skip = "Remove to run test")]
    public void Discard_arrays()
    {
        var actual = new[]
            {
                new[] { 1, 2, 3 },
                new[] { 5, 5, 5 },
                new[] { 5, 1, 2 },
                new[] { 2, 1, 2 },
                new[] { 1, 5, 2 },
                new[] { 2, 2, 1 },
                new[] { 1, 2, 5 }
            };
        var expected = new[] { new[] { 1, 2, 3 }, new[] { 2, 1, 2 }, new[] { 2, 2, 1 } };
        Assert.Equal(expected, actual.Discard(x => x.Contains(5)));
    }
}
using System;
using System.Collections.Generic;
using System.Linq;

public static class Strain
{
    public static IEnumerable<T> Keep<T>(this IEnumerable<T> collection, Func<T, bool> predicate)
    {
        var result = new List<T>();
        foreach (var item in collection)
        {
            if (predicate(item))
            {
                result.Add(item);
            }
        }

        return result;
    }

    public static IEnumerable<T> Discard<T>(this IEnumerable<T> collection, Func<T, bool> predicate)
    {
        var result = new List<T>();
        foreach (var item in collection)
        {
            if (!predicate(item))
            {
                result.Add(item);
            }
        }

        return result;
    }
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?