Skip to content

Latest commit

 

History

History
55 lines (39 loc) · 1.39 KB

File metadata and controls

55 lines (39 loc) · 1.39 KB

Mentoring

Reasonable solutions

Using yield

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 element in collection)
        {
            yield return func(element);
        }
    }
}

Storing the results in an intermediate list

Solutions that don't use the yield keyword such as the following one which stores the results in an intermediate list don't pass the laziness test. The accumulate execution must be deferred until ToList() is called on it, which is tested with the Accumulate_is_lazy method.

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)
    {
        var accumulated = new List<U>();

        foreach (var element in collection)
        {
            accumulated.Add(func(element));
        }

        return accumulated;
    }
}

Common suggestions

  • If the student doesn't use the yield keyword, suggest them to look into it.

Talking points

  • Per the restrictions in the README, the student is not allowed to use LINQ's Select() method.