实现不带yield 关键字的Linqs Select。无法遵循控制流程

发布于 2024-12-17 12:37:43 字数 2267 浏览 3 评论 0原文

我知道有很多关于 Linq 及其内部工作原理的文章。受到 Jon Skeets EduLinq 的启发,我想揭开 Linq Operators 背后发生的事情的神秘面纱。所以我尝试做的是实现 Linqs Select() 方法,乍一看听起来很无聊。但我实际上想做的是在不使用yield 关键字的情况下实现它。

到目前为止,这就是我所得到的:

class Program
{
    static void Main(string[] args)
    {
        var list = new int[] {1, 2, 3};

        var otherList = list.MySelect(x => x.ToString()).MySelect(x => x + "test");

        foreach (var item in otherList)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();
    }
}

public static class EnumerableEx
{
    public static IEnumerable<R> MySelect<T, R>(this IEnumerable<T> sequence, Func<T, R> apply)
    {
        return new EnumerableWrapper<R, T>(sequence, apply);
    }
}

public class EnumerableWrapper<T, O> : IEnumerable<T>
{
    private readonly IEnumerable<O> _sequence;
    private readonly Func<O, T> _apply;

    public EnumerableWrapper(IEnumerable<O> sequence, Func<O, T> apply)
    {
        _sequence = sequence;
        _apply = apply;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return new EnumeratorWrapper<T, O>(_sequence, _apply);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class EnumeratorWrapper<T, O> : IEnumerator<T>
{
    private readonly IEnumerator<O> _enumerator;
    private readonly Func<O, T> _apply;

    public EnumeratorWrapper(IEnumerable<O> sequence, Func<O, T> apply)
    {
        _enumerator = sequence.GetEnumerator();
        _apply = apply;
    }

    public void Dispose()
    {
    }

    public bool MoveNext()
    {
        var hasItems = _enumerator.MoveNext();
        if (hasItems)
            Current = _apply(_enumerator.Current);
        return hasItems;
    }

    public void Reset()
    {
        _enumerator.Reset();
    }

    public T Current { get; private set; }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}

它似乎有效。但是,我很难遵循它的控制流程。正如你所看到的,我依赖于预测。这会导致 MoveNext() 方法中发生奇怪的事情(对我来说很奇怪!)。如果在 MoveNext() 方法的每一行中设置断点,您将看到控制流实际上在不同实例之间跳转,并且永远不会在一批中执行该方法。它的跳跃就好像它使用了不同的线程或者我们使用了yield。但归根结底,这只是一个正常的方法,所以我想知道这是怎么回事?

I know a lot has been written about Linq and it's inner workings. Inspired by Jon Skeets EduLinq, I wanted to to demystify what's going on behind the Linq Operators. So what I tried to do is to implement Linqs Select() method which sounds pretty boring at first sight. But what I'm actually trying to do is to implement it without the use of the yield keyword.

So here is what I got so far:

class Program
{
    static void Main(string[] args)
    {
        var list = new int[] {1, 2, 3};

        var otherList = list.MySelect(x => x.ToString()).MySelect(x => x + "test");

        foreach (var item in otherList)
        {
            Console.WriteLine(item);
        }

        Console.ReadLine();
    }
}

public static class EnumerableEx
{
    public static IEnumerable<R> MySelect<T, R>(this IEnumerable<T> sequence, Func<T, R> apply)
    {
        return new EnumerableWrapper<R, T>(sequence, apply);
    }
}

public class EnumerableWrapper<T, O> : IEnumerable<T>
{
    private readonly IEnumerable<O> _sequence;
    private readonly Func<O, T> _apply;

    public EnumerableWrapper(IEnumerable<O> sequence, Func<O, T> apply)
    {
        _sequence = sequence;
        _apply = apply;
    }

    public IEnumerator<T> GetEnumerator()
    {
        return new EnumeratorWrapper<T, O>(_sequence, _apply);
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

public class EnumeratorWrapper<T, O> : IEnumerator<T>
{
    private readonly IEnumerator<O> _enumerator;
    private readonly Func<O, T> _apply;

    public EnumeratorWrapper(IEnumerable<O> sequence, Func<O, T> apply)
    {
        _enumerator = sequence.GetEnumerator();
        _apply = apply;
    }

    public void Dispose()
    {
    }

    public bool MoveNext()
    {
        var hasItems = _enumerator.MoveNext();
        if (hasItems)
            Current = _apply(_enumerator.Current);
        return hasItems;
    }

    public void Reset()
    {
        _enumerator.Reset();
    }

    public T Current { get; private set; }

    object IEnumerator.Current
    {
        get { return Current; }
    }
}

It seems to work. However, I have trouble to follow it's control flow. As you can see, I chain to projections. This causes to let strange things (strange for me!) happen in the MoveNext() method. If you set breakpoints in every line of the MoveNext() method you will see that the control flow actually jumps between the different instances and never works through the method in one batch. It's jumping as if it were using different threads or if we were using yield. But in the end, it's just a normal method so I wonder what's going on there?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浪菊怪哟 2024-12-24 12:37:43

每次您对结果调用 MoveNext() 时,它都会:

  • 对最终迭代器(我将其称为 Y)调用 MoveNext(),这将
  • ...在前一个迭代器(我将其称为 X)上调用 MoveNext(),这将...
  • 原始迭代器上调用 MoveNext() (数组的),它将返回一个数字。
  • X 中的 MoveNext() 将调用投影 x =>; x.ToString() 以便它
  • 在 Y 中具有适当的 Current 成员 MoveNext() 然后将调用投影 x =>; x + "test"X.Current 的结果进行测试,并将结果存储在 Y.Current

所以这里没有什么特别神奇的事情 - 你'我们刚刚接到了一堆电话,就像平常一样。调试体验将向您展示从一个 EnumeratorWrapper.MoveNext 到另一个的调用;唯一奇怪的跳跃是当您单步执行在 Main 方法中声明的投影时。

如果这不能解释是什么让您感到困惑,请详细说明您不理解流程的具体情况,我会尽力提供帮助。 (很高兴看到你想更多地了解所有这些事情是如何运作的。很高兴看到志同道合的人!)

Each time you call MoveNext() on the result it will:

  • Call MoveNext() on the final iterator (which I'll call Y), which will...
  • Call MoveNext() on the previous iterator (which I'll call X), which will...
  • Call MoveNext() on the original iterator (of the array), which will return a number.
  • MoveNext() in X will then call the projection x => x.ToString() so that it has an appropriate Current member
  • MoveNext() in Y will then call the projection x => x + "test" on the result of X.Current, and store the result in Y.Current

So there's nothing particularly magical going on here - you've just got stacked calls, just as normal. The debugging experience will show you that call from one EnumeratorWrapper.MoveNext to another; the only odd jumping around will be when you step through the projections, which are declared in the Main method.

If that doesn't explain what was confusing you, please give more details about exactly where you don't understand the flow, and I'll see how I can help. (It's great to see you wanting to learn more about how all these things work though. Good to see a kindred spirit!)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文