Is it possible to return a list from an iterator block?

John Smith

I have this function:

class Path : List<LineSegment> { }

private IEnumerable<LineSegment> GetLineSegments(CollisionType collisionType, Path path)
{
    if (collisionType == CollisionType.End)
    {
        yield return path.First();
        yield return path.Last();
    }
    else if (collisionType == CollisionType.Segment)
    {
        foreach (LineSegment lineSegment in path)
        {
            yield return lineSegment;
        }
    }
}

Basically, I am checking for collision (between ball and line) in two ways. In one case I only want to check the end points of the drawn path, but in the other case I want to return each line segment in the entire path.

It seems to be a bit weird to loop through the given list just to return the entire list. Can I just return the list? I'd like to be able to do something like this but I'm getting errors:

private IEnumerable<LineSegment> GetLineSegments(CollisionType collisionType, Path path)
{
    if (collisionType == CollisionType.End)
    {
        yield return path.First();
        yield return path.Last();
    }
    else if (collisionType == CollisionType.Segment)
    {
        return path.AsEnumerable<LineSegment>();
    }
}
Servy

You have no choice but to use a foreach loop. It's a somewhat frequently requested feature, but one that is not in the language.

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

How to return item index from list SML?

来自分类Dev

return value from possible type in std::variant through std::visit

来自分类Dev

如何实现 list<T>::iterator?

来自分类Dev

get all possible combinations of k elements from a list

来自分类Dev

我如何定义map :: iterator和list :: iterator的列表

来自分类Dev

我如何定义map :: iterator和list :: iterator的列表

来自分类Dev

Scala:Iterator与List上的筛选器行为

来自分类Dev

list_iterator类在哪里定义?

来自分类Dev

list :: iterator对于移至列表无效?

来自分类Dev

ListIterator 或 Iterator 不会停在 List 的末尾

来自分类Dev

possible outcomes for a List

来自分类Dev

Why does not Iterator have add method whereas List Iterator has one in Java?

来自分类Dev

Is it possible to add a list to an irregular column?

来自分类Dev

std :: list iterator.erase()导致无效的指针

来自分类Dev

从std :: list <Shape *> :: iterator调用指针上的函数

来自分类Dev

在Kotlin中删除Iterator或List之前的任意内容

来自分类Dev

std :: list iterator.erase()导致无效的指针

来自分类Dev

Scala:List.iterator产生空的迭代器

来自分类Dev

类型错误:“list_iterator”对象不是异步可迭代的

来自分类Dev

Return value from BroadcastReceiver

来自分类Dev

为什么Iterator在Java中没有add方法而List Iterator在Java中只有add方法?

来自分类Dev

打字稿array.from(iterator)类型错误,无法转换?

来自分类Dev

Possible to modify a List while iterating through it?

来自分类Dev

错误:'std::__cxx11::list<User>::iterator' {aka 'struct std::_List_iterator<User>'} 没有名为 XXX 的成员

来自分类Dev

postgres return json from a function

来自分类Dev

Is it possible in typescript to have a function return the type of a class that extends it

来自分类Dev

R: sum the elements of each list on a list and return the result in a data frame

来自分类Dev

如何将std :: <list> :: reverse_iterator与运算符+一起使用?

来自分类Dev

错误:从'int'分配给'_List_iterator <unsigned int,unsigned int&,unsigned int *>'

Related 相关文章

  1. 1

    How to return item index from list SML?

  2. 2

    return value from possible type in std::variant through std::visit

  3. 3

    如何实现 list<T>::iterator?

  4. 4

    get all possible combinations of k elements from a list

  5. 5

    我如何定义map :: iterator和list :: iterator的列表

  6. 6

    我如何定义map :: iterator和list :: iterator的列表

  7. 7

    Scala:Iterator与List上的筛选器行为

  8. 8

    list_iterator类在哪里定义?

  9. 9

    list :: iterator对于移至列表无效?

  10. 10

    ListIterator 或 Iterator 不会停在 List 的末尾

  11. 11

    possible outcomes for a List

  12. 12

    Why does not Iterator have add method whereas List Iterator has one in Java?

  13. 13

    Is it possible to add a list to an irregular column?

  14. 14

    std :: list iterator.erase()导致无效的指针

  15. 15

    从std :: list <Shape *> :: iterator调用指针上的函数

  16. 16

    在Kotlin中删除Iterator或List之前的任意内容

  17. 17

    std :: list iterator.erase()导致无效的指针

  18. 18

    Scala:List.iterator产生空的迭代器

  19. 19

    类型错误:“list_iterator”对象不是异步可迭代的

  20. 20

    Return value from BroadcastReceiver

  21. 21

    为什么Iterator在Java中没有add方法而List Iterator在Java中只有add方法?

  22. 22

    打字稿array.from(iterator)类型错误,无法转换?

  23. 23

    Possible to modify a List while iterating through it?

  24. 24

    错误:'std::__cxx11::list<User>::iterator' {aka 'struct std::_List_iterator<User>'} 没有名为 XXX 的成员

  25. 25

    postgres return json from a function

  26. 26

    Is it possible in typescript to have a function return the type of a class that extends it

  27. 27

    R: sum the elements of each list on a list and return the result in a data frame

  28. 28

    如何将std :: <list> :: reverse_iterator与运算符+一起使用?

  29. 29

    错误:从'int'分配给'_List_iterator <unsigned int,unsigned int&,unsigned int *>'

热门标签

归档