Avatar of JigsawCode

JigsawCode's solution

to Accumulate in the C# Track

Published at Sep 30 2019 · 0 comments
Instructions
Test suite
Solution

Implement the accumulate operation, which, given a collection and an operation to perform on each element of the collection, returns a new collection containing the result of applying that operation to each element of the input collection.

Given the collection of numbers:

  • 1, 2, 3, 4, 5

And the operation:

  • square a number (x => x * x)

Your code should be able to produce the collection of squares:

  • 1, 4, 9, 16, 25

Check out the test suite to see the expected function signature.

Restrictions

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

To be more specific, you are not allowed to use any of the built-in LINQ methods.

Laziness test

Since accumulate returns an IEnumerable, it's execution is deferred until ToList() it is called on it, which is tested with the Accumulate_is_lazy method

Hints

This exercise requires you to write an extension method. For more information, see this page.

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 Accumulate.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

AccumulateTest.cs

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

public class AccumulateTest
{
    [Fact]
    public void Empty_accumulation_produces_empty_accumulation()
    {
        Assert.Equal(new int[0], new int[0].Accumulate(x => x * x));
    }

    [Fact(Skip = "Remove to run test")]
    public void Accumulate_squares()
    {
        Assert.Equal(new[] { 1, 4, 9 }, new[] { 1, 2, 3 }.Accumulate(x => x * x));
    }

    [Fact(Skip = "Remove to run test")]
    public void Accumulate_upcases()
    {
        Assert.Equal(new List<string> { "HELLO", "WORLD" }, new List<string> { "hello", "world" }.Accumulate(x => x.ToUpper()));
    }

    [Fact(Skip = "Remove to run test")]
    public void Accumulate_reversed_strings()
    {
        Assert.Equal("eht kciuq nworb xof cte".Split(' '), "the quick brown fox etc".Split(' ').Accumulate(Reverse));
    }

    private static string Reverse(string value)
    {
        var array = value.ToCharArray();
        Array.Reverse(array);
        return new string(array);
    }

    [Fact(Skip = "Remove to run test")]
    public void Accumulate_within_accumulate()
    {
        var actual = new[] { "a", "b", "c" }.Accumulate(c =>
            string.Join(" ", new[] { "1", "2", "3" }.Accumulate(d => c + d)));
        Assert.Equal(new[] { "a1 a2 a3", "b1 b2 b3", "c1 c2 c3" }, actual);
    }

    [Fact(Skip = "Remove to run test")]
    public void Accumulate_is_lazy()
    {
        var counter = 0;
        var accumulation = new[] { 1, 2, 3 }.Accumulate(x => x * counter++);

        Assert.Equal(0, counter);
        accumulation.ToList();
        Assert.Equal(3, counter);
    }

    [Fact(Skip = "Remove to run test")]
    public void Accumulate_allows_different_return_type()
    {
        Assert.Equal(new[] { "1", "2", "3" }, new[] { 1, 2, 3 }.Accumulate(x => x.ToString()));
    }
}
using System;
using System.Collections.Generic;

public static class AccumulateExtensions
{
    public static IEnumerable<U> Accumulate<T, U>(this IEnumerable<T> collection, Func<T, U> func)
    {
        foreach (var item in collection) yield return func(item);
    }
}

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?