我正在查看https://github.com/playframework/Play20/tree/master/samples/scala/websocket-chat上的示例
要制作一个websocket控制器,你可以这样写:
def chat(username: String) = WebSocket.async[JsValue] { request =>
ChatRoom.join(username)
}
Run Code Online (Sandbox Code Playgroud)
Chatroom.join返回一个scala.concurrent.Future [(Iteratee [JsValue,_],Enumerator [JsValue])].但是在Play中使用的iteratee和枚举器在哪里!框架?该网页套接字类(WebSocket.scala)似乎忽视了投入:
case class WebSocket[A](f: RequestHeader => (Enumerator[A], Iteratee[A, Unit]) => Unit) (implicit val frameFormatter: WebSocket.FrameFormatter[A]) extends Handler {
type FRAMES_TYPE = A
/**
* Returns itself, for better support in the routes file.
*
* @return itself
*/
def apply() = this
}
Run Code Online (Sandbox Code Playgroud)
怎么玩!在消耗输入时管理iteratee的变化状态?
在集合的框架类中,我经常看到IEnumerator<T>单独实现为内部类,并在GetEnumerator方法中返回它的实例.
现在假设我正在编写自己的集合类,它们将内置一个内置集合List<T>或T[]作为持有者,如下所示:
public class SpecialCollection<T> : IEnumerable<T>
{
List<T> list;
public SpecialCollection<T>()
{
}
public IEnumerator<T> GetEnumerator()
{
return list.GetEnumerator();
//or
return list.Where(x => some logic).GetEnumerator();
//or directly rely on yield keyword
yield return x; //etc
}
}
Run Code Online (Sandbox Code Playgroud)
我应该编写自己的枚举器类,还是可以返回List<T>类的枚举器?我是否应该编写自己的枚举类?
我也有一个相关的问题.如果它不是那么重要或没有太大的区别,为什么BCL中的每个集合类都写自己的IEnumerator?
例如,List<T>班级有类似的东西
T[] items;
public IEnumerator<T> GetEnumerator()
{
return new List<T>.Enumerator(items);
}
Run Code Online (Sandbox Code Playgroud) 我有一个枚举,其中包含逐渐传入的服务调用的响应。
ToList对可枚举进行操作,因为这会阻塞,直到收到所有响应,而不是在收到响应时列出它们。 如何获取可枚举的第一个元素并返回可枚举的延续?我无法使用迭代器方法,因为出现编译错误:
迭代器不能有 ref、in 或 out 参数。
我试过这段代码:
public IEnumerable<object> GetFirstAndRemainder(IEnumerable<object> enumerable, out object first)
{
first = enumerable.Take(1).FirstOrDefault();
return enumerable.Skip(1); // Second interation - unexceptable
}
// This one has a compilation error: Iterators cannot have ref, in or out parameters
public IEnumerable<object> GetFirstAndRemainder2(IEnumerable<object> enumerable, out object first)
{
var enumerator = enumerable.GetEnumerator();
enumerator.MoveNext();
first = enumerator.Current;
while (enumerator.MoveNext())
{
yield return enumerator.Current;
}
}
Run Code Online (Sandbox Code Playgroud) 我想通过nextis- cloneable为外部迭代创建一个枚举器,以便克隆保留当前的枚举状态。
举个例子,假设我有一个方法,它返回一个枚举数,它产生平方数:
def square_numbers
return enum_for(__method__) unless block_given?
n = d = 1
loop do
yield n
d += 2
n += d
end
end
square_numbers.take(10)
#=> [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
Run Code Online (Sandbox Code Playgroud)
我想枚举前 5 个平方数,并为每个值打印随后的 3 个平方数。一些无关紧要的事情each_cons:
square_numbers.take(8).each_cons(4) do |a, *rest|
printf("%2d: %2d %2d %2d\n", a, *rest)
end
Run Code Online (Sandbox Code Playgroud)
输出:
1: 4 9 16
4: 9 16 25
9: 16 25 36
16: 25 36 49
25: 36 …Run Code Online (Sandbox Code Playgroud) 我有一个第三方api,它有一个类,它返回类中不同项的枚举器.
我需要删除该枚举器中的项目,因此我不能使用"for each".我能想到的唯一选择是通过遍历枚举来获取计数,然后运行正常的for循环来删除项目.
有人知道避免这两个循环的方法吗?
谢谢
[更新]抱歉混淆,但安德烈在评论中是正确的.
这是我脑子里的一些伪代码无法正常工作,我正在寻找一个不会涉及两个循环的解决方案,但我想这是不可能的:
for each (myProperty in MyProperty)
{
if (checking some criteria here)
MyProperty.Remove(myProperty)
}
Run Code Online (Sandbox Code Playgroud)
MyProperty是实现枚举器和remove方法的第三方类.
可能重复:
修改foreach中列表的最佳方法是什么?
假设我有一个ObservableCollection mycollection,我想通过枚举器做一些事情:
foreach(var x in mycollection)
{
// This will do something based on data of x and remove x from mycollection
x.Close();
}
Run Code Online (Sandbox Code Playgroud)
该方法Close()有一行代码 - mycollection.Remove(x);.当我运行此代码时,得到以下错误:
收集被修改; 枚举操作可能无法执行.
我无法更改方法,Close()因为它在应用程序的许多其他地方被调用.我该如何解决这个问题?
有没有办法在Ruby中循环枚举器?鉴于这段代码:
a=[1,2,3]
a.to_enum
a.next => 1
a.next => 2
a.next => 3
a.next => 1
Run Code Online (Sandbox Code Playgroud)
next当枚举数到达最后一个元素时,如何让方法返回第一个元素?
我已经GetEnumerator为一个简单的类实现了这个方法,并且很惊讶我无法使用linq命令枚举器(调用this.OrderBy(x => x)无效).有人可以解释一下这里发生了什么吗?我做错了什么或只是想要迭代的调查员?
class Test
{
private Dictionary<int, string> dict
= new Dictionary<int, string>();
public IEnumerator<int> GetEnumerator()
{
return dict.Keys.GetEnumerator();
}
public Test()
{
dict[1] = "test";
dict[2] = "nothing";
}
public IEnumerable<int> SortedKeys
{
get { return this.OrderBy(x => x); } // illegal!
}
public void Print()
{
foreach(var key in this)
Console.WriteLine(dict[key]);
}
}
Run Code Online (Sandbox Code Playgroud) 我正在与几个迭代器一起工作,在这些迭代器中,我必须按照以下方式做点事情(enum是一个枚举器)
enums_with_zero << enum.rewind if enum.peek == 0
Run Code Online (Sandbox Code Playgroud)
通常可以正常工作,但这是在#next枚举已经被调用几次之后。问题在于,enum可能在末尾传递了一些值enum,我遇到enum.peek了StopIteration因为enum完成而引发问题的问题。有没有一种方法我可以把在保护检查enum.peek或enum.next会导致StopIteration之前,我把它。例如,某些东西会具有这种行为?
class Enumerator
def has_next?
begin
peek && true
rescue StopIteration
false
end
end
end
Run Code Online (Sandbox Code Playgroud) enumerator ×10
c# ×6
ruby ×3
.net ×2
ienumerable ×2
ienumerator ×2
clone ×1
enumerate ×1
foreach ×1
iterate ×1
linq ×1
loops ×1
scala ×1
websocket ×1