斯卡拉是否有一个相当于C#收益呢? [英] Does Scala have an equivalent to C# yield?

查看:183
本文介绍了斯卡拉是否有一个相当于C#收益呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新来斯卡拉,并从我的理解产生在Scala中并不像在C#中的产量,它更像是选择。

I'm new to Scala, and from what I understand yield in Scala is not like yield in C#, it is more like select.

斯卡拉是否有类似C#的产量东西吗? C#的产量是伟大的,因为它使得编写迭代器很容易。

Does Scala have something similar to C#'s yield? C#'s yield is great because it makes writing iterators very easy.

更新:这里是从C#伪code的例子,我希望能在斯卡拉实现:

Update: here's a pseudo code example from C# I'd like to be able to implement in Scala:

public class Graph<T> {
   public IEnumerable<T> BreadthFirstIterator() {
      List<T> currentLevel = new List<T>();
      currentLevel.add(_root);

      while ( currentLevel.count > 0 ) {
         List<T> nextLevel = new List<T>();
         foreach( var node in currentLevel ) {
            yield return node;
            nextLevel.addRange( node.Children );
         }
         currentLevel = nextLevel;
      }
   }
}

这code实现了图的迭代广度优先遍历,用产量,它返回一个迭代器,使呼叫者可以遍历使用普通的for循环图,例如:

This code implements an iterative breadth first traversal of a graph, using yield, it returns an iterator, so that callers can traverse the graph using a regular for loop, e.g.:

graph.BreadthFirstIterator().foreach( n => Console.WriteLine( n ) );

在C#中,产量只是语法糖可以很容易地编写一个迭代器(的IEnumerable&LT; T&GT; 在.NET中,类似可迭代在Java中)。作为一个迭代器,其懒洋洋地评估。

In C#, yield is just syntactic sugar to make it easy to write an iterator (IEnumerable<T> in .Net, similar to Iterable in Java). As an iterator, its evaluated lazily.

更新二:我可能是错在这里,但我认为在C#产量的整点是为了让你不必写一个高阶函数。例如。你可以写一个正循环,或者使用类似的方法选择 / 地图 / 过滤 / ,其中,而不是传递一个函数,那么它会遍历序列。

Update II: I could be wrong here, but I think the whole point of yield in C# is so that you don't have to write a higher order function. E.g. you can write a regular for loop or use a method like select/map/filter/where instead of passing in a function which will then traverse the sequence.

例如。 graph.iterator()的foreach(N =&GT;的println(N))。而不是 graph.iterator(N =&GT;的println(N) )

这样你就可以轻松地把它们连,例如 graph.iterator()图(X =&GT; x.foo).filter(Y =&GT; y.bar&GT; = 2)。的foreach(Z =&GT;的println(Z))。

This way you can chain them easily, e.g graph.iterator().map(x => x.foo).filter(y => y.bar >= 2).foreach(z => println(z)).

推荐答案

我想答案(2.8禁止改变)是,答案是否定的,Scala没有类似C#的产量语法糖写的迭代器(实现IEumerable或可迭代)。

I think the answer (barring changes in 2.8) is that the answer is no, Scala does not have syntactic sugar similar to C#'s yield to write iterators (implementations of IEumerable or Iterable).

然而,在Scala中可以代替实现通过在函数传递给它会调用在遍历每个项目遍历类似的结果。这种方法也可以在C#中的相同的方式实现的。

However, in Scala you could instead achieve a similar result by passing in a function to the traversal which it would invoke on each item in the traversal. This approach could also be implemented in the same fashion in C#.

下面是我在C#编写导线无需使用收益率:

Here is how I'd write Traverse in C# without the use of yield:

public class Graph<T> {
   public void BreadthFirstTraversal( Action<T> f) {
      List<T> currentLevel = new List<T>();
      currentLevel.add(_root);

      while ( currentLevel.count > 0 ) {
         List<T> nextLevel = new List<T>();
         foreach( var node in currentLevel ) {
            f(node);
            nextLevel.addRange( node.Children );
         }
         currentLevel = nextLevel;
      }
   }
}

然后,您可以使用它是这样的:

You could then use it like this:

graph.BreadthFirstTraversal( n => Console.WriteLine( n ) );

或者是这样的:

graph.BreadthFirstTraversal( n =>
{
   Console.WriteLine(n);
   DoSomeOtherStuff(n);
});

这篇关于斯卡拉是否有一个相当于C#收益呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆