我在数据库中有一组行,我想提供一个接口来像这样旋转它们:
def findAll: Iterable[MyObject]
Run Code Online (Sandbox Code Playgroud)
我们不需要同时在内存中包含所有实例.在C#中,您可以使用yield轻松创建这样的生成器,编译器负责将循环通过记录集的代码转换为迭代器(将其反转).
我当前的代码如下所示:
def findAll: List[MyObject] = {
val rs = getRs
val values = new ListBuffer[MyObject]
while ( rs.next() )
values += new valueFromResultSet(rs)
values.toList
}
Run Code Online (Sandbox Code Playgroud)
有没有办法可以将其转换为不将整个集存储在内存中?也许我可以使用一个理解?
更新:
2006年在python.org上提出了使内置字符串不可迭代的想法.我的问题不同之处在于我试图偶尔禁止这个功能; 仍然这整个线程非常相关.
以下是Guido的批评意见,他们str
在试验基础上实施了不可迭代的评论:
[...]我实现了这个(这很简单)然后发现我必须修复大量遍布字符串的地方.例如:
sre解析器和编译器使用set("0123456789")之类的东西,并迭代输入regexp的字符来解析它.
difflib有两个字符串列表定义的API(文件的典型逐行差异),或两个字符串(典型的行内差异),甚至两个任何列表(对于广义序列差异) .
optparse.py,textwrap.py,string.py中的小变化.
而且我甚至还没有在regrtest.py框架工作的地方(由于difflib问题).
我放弃了这个项目; 补丁是SF补丁1471291.我不再赞成这个想法; 它只是不实用,而且我在sre和difflib中找到的用例都反驳了迭代字符串的好理由的前提.
原始问题:
虽然字符串的一个简洁功能是字符串是可迭代的,但当与鸭子打字相结合时,它可能会导致灾难:
# record has to support [] operation to set/retrieve values
# fields has to be an iterable that contains the fields to be set
def set_fields(record, fields, value):
for f in fields:
record[f] = value
set_fields(weapon1, ('Name', 'ShortName'), 'Dagger')
set_fields(weapon2, ('Name',), 'Katana')
set_fields(weapon3, 'Name', 'Wand') # I was tired and forgot to put parentheses
Run Code Online (Sandbox Code Playgroud)
除了isinstance(fields, …
我有一个文件"test.txt":
this is 1st line
this is 2nd line
this is 3rd line
Run Code Online (Sandbox Code Playgroud)
以下代码
lines = open("test.txt", 'r')
for line in lines:
print "loop 1:"+line
for line in lines:
print "loop 2:"+line
Run Code Online (Sandbox Code Playgroud)
只打印:
loop 1:this is 1st line
loop 1:this is 2nd line
loop 1:this is 3rd line
Run Code Online (Sandbox Code Playgroud)
它根本不打印loop2.
两个问题:
open()返回的文件对象是可迭代的吗?这就是为什么它可以用于for循环?
为什么loop2根本没有印刷?
假设您想要流式迭代器的元素; 让我们使用一个具体的例子Scanner
来实现Iterator<String>
.
给出一个Iterator
,说:
// Scanner implements Iterator<String>
Iterator<String> scanner = new Scanner(System.in);
Run Code Online (Sandbox Code Playgroud)
从中创建的选项Stream<String>
很笨重:
StreamSupport.stream(
Spliterators.spliteratorUnknownSize(scanner, Spliterator.ORDERED), false);
Run Code Online (Sandbox Code Playgroud)
或稍微简洁,但迟钝:
StreamSupport.stream(
((Iterable<String>) () -> new Scanner(System.in)).spliterator(), false);
Run Code Online (Sandbox Code Playgroud)
在JDK中的某个地方是否有一个返回Stream<T>
给定的工厂方法Iterator<T>
?
在ES6中,我试图arguments
在传递给Set
构造函数时将对象用作iterable .它在IE11和Chrome 47中运行良好.它在Firefox 43中不起作用(抛出a TypeError: arguments is not iterable
).我查看了ES6规范,无法真正找到arguments
对象是否应该是可迭代的定义.
这是我尝试做的一个例子:
function destroyer(arr) {
var removes = new Set(arguments);
return arr.filter(function(item) {
return !removes.has(item);
});
}
// remove items 2, 3, 5 from the passed in array
var result = destroyer([3, 5, 1, 2, 2], 2, 3, 5);
log(result);
Run Code Online (Sandbox Code Playgroud)
仅供参考,我知道此代码有各种解决方法,例如将arguments对象复制到实数数组或使用rest参数.这个问题是关于ES6中是否arguments
应该使用该对象iterable
,可以在任何可预期的迭代中使用.
我正在使用Typescript 2.4.1并在我的项目中升级了许多包.其中我将Angular从2升级到4.3.1.在@types包中进行了许多更正之后,我留下的错误是:
\node_modules\@types\jquery\index.d.ts(2955,63): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(767,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(771,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(775,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(779,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(783,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(787,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(791,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(795,24): error TS2304: Build:Cannot find name 'Iterable'.
\node_modules\@types\three\three-core.d.ts(799,24): error TS2304: Build:Cannot find name 'Iterable'.
Run Code Online (Sandbox Code Playgroud)
我找到了许多类似的问题和答案,目前的解决方案是针对"es2015"和/或添加lib:["dom","es2015","es2015.iterable"].我已经尝试了所有这些以及更多,但仍然留下相同的'Iterable'错误.
我更新的tsconfig.json是这样的:
{
"compileOnSave": true,
"compilerOptions": {
"declaration": …
Run Code Online (Sandbox Code Playgroud) 迭代器与迭代器相同还是不同?
从规范来看,iterable似乎是一个对象,obj
例如,obj[Symbol.iterator]
引用一个函数,以便在调用时返回一个具有next
可以返回{value: ___, done: ___}
对象的方法的对象:
function foo() {
let i = 0;
const wah = {
next: function() {
if (i <= 2) return { value: (1 + 2 * i++), done: false }
else return { value: undefined, done: true }
}
};
return wah; // wah is iterator
}
let bar = {} // bar is iterable
bar[Symbol.iterator] = foo;
console.log([...bar]); // [1, 3, 5]
for (a of bar) …
Run Code Online (Sandbox Code Playgroud) 我在python中有一个类,它有一个可迭代的实例变量.我想通过迭代嵌入式迭代来迭代类的实例.
我实现如下:
def __iter__(self):
return self._iterable.__iter__()
Run Code Online (Sandbox Code Playgroud)
我觉得__iter__()
在iterable上调用方法并不是很舒服,因为它是一种特殊的方法.这是你如何在python中解决这个问题,还是有更优雅的解决方案?
鉴于:
x = ['w', 'e', 's', 's', 's', 'z','z', 's']
Run Code Online (Sandbox Code Playgroud)
每次出现都s
出现在以下指数:
1st:2
2nd:3
3rd:4
4th:7
如果我这样做,x.index('s')
我将得到第一个索引.
我如何得到第四个指数s
?
并且它们都获得Consumer作为参数.因此,如果Java 8是为了避免混淆,就像它在Time API中所做的那样,为什么它会增加一个新的混乱?还是我错过了一些观点?
iterable ×10
python ×4
iterator ×3
ecmascript-6 ×2
java ×2
javascript ×2
angular ×1
foreach ×1
indexing ×1
java-8 ×1
java-stream ×1
jdbc ×1
python-3.x ×1
scala ×1
string ×1
typescript ×1