我知道Scala的列表有一个带签名的map实现和带签名(f: (A) => B):List[B]的foreach实现,(f: (A) => Unit):Unit但我正在寻找接受多个iterables的东西,就像Python map接受多个iterables一样.
我正在寻找具有特征(f: (A,B) => C, Iterable[A], Iterable[B] ):Iterable[C]或同等特征的东西.是否存在存在这样的库或类似的类似方法?
编辑:
如下所示,我可以做到
val output = myList zip( otherList ) map( x => x(0) + x(1) )
Run Code Online (Sandbox Code Playgroud)
但这会在两个步骤之间创建一个临时列表.如果评论员会发帖,我可以赞成他(提示,提示),但还有另一种方式吗?
克拉!
我想"美化"我的一个Dart脚本的输出,如下所示:
-----------------------------------------
OpenPGP signing notes from key `CD42FF00`
-----------------------------------------
<Paragraph>
Run Code Online (Sandbox Code Playgroud)
我想知道在Dart中是否有一种特别简单和/或优化的方式来打印相同的角色x时间.在Python中,print "-" * x会打印-字符x时间.
从这个答案中学习,为了这个问题的目的,我编写了以下最小代码,它使用了核心Iterable类:
main() {
// Obtained with '-'.codeUnitAt(0)
const int FILLER_CHAR = 45;
String headerTxt;
Iterable headerBox;
headerTxt = 'OpenPGP signing notes from key `CD42FF00`';
headerBox = new Iterable.generate(headerTxt.length, (e) => FILLER_CHAR);
print(new String.fromCharCodes(headerBox));
print(headerTxt);
print(new String.fromCharCodes(headerBox));
// ...
}
Run Code Online (Sandbox Code Playgroud)
这给出了预期的输出,但在Dart中有更好的方法来打印字符(或字符串)x次数吗?在我的例子中,我想打印- …
我不能使用itertools
所以编码看起来很简单,但是我在思考算法时遇到了麻烦,以便在所有迭代都被完全处理之前保持生成器运行.
该函数的想法是将2个迭代作为这样的参数......
(['a', 'b', 'c', 'd', 'e'], [1,2,5])
它的作用是产生这些价值......
a, b, b, c, c, c, c, c
但是,如果第二个迭代首先耗尽元素,则函数只会迭代剩余值一次......
所以剩下的值会像这样迭代:
d, e
def iteration(letters, numbers):
times = 0
for x,y in zip(letters, numbers):
try:
for z in range(y):
yield x
except:
continue
[print(x) for x in iteration(['a', 'b', 'c', 'd'], [1,2,3])]
Run Code Online (Sandbox Code Playgroud)
我无法忽略第一个StopIteration并继续完成.
因此,有Iterable和Iterator和List.如果您尝试提供其他Java代码的接口,以便封装由"页面"返回结果的远程服务提供的功能,您会使用什么?
例如,考虑数据库或网页(例如flickr API).在第一次检索结果后,您知道结果的总数和前N个结果,但在检索其余结果之前,您不知道剩余的结果.
随着C#我逐渐喜欢这个IEnumerable<T>界面.在很多情况下,您只需要提供和接受.此外,它在.Net库中很有用.例如,你有一个List<T>类的构造函数IEnumerable<T>.
我现在必须使用Java,我自然希望使用等效的Iterable<T>接口.但是,它似乎并不像我可以在任何地方使用它.一切似乎都在使用扩展Collection<T>接口.为什么是这样?
作为一个例子,你有一个ArrayList<T>构造函数,它采用Collection<T>:
按照集合的迭代器返回的顺序构造一个包含指定集合元素的列表.
为什么不采取Iterable<T>反而?
目的是在给定整数列表的情况下找到增加/单调数字的组.结果组中的每个项目必须是前一项目的+1增量
给出一个输入:
x = [7, 8, 9, 10, 6, 0, 1, 2, 3, 4, 5]
Run Code Online (Sandbox Code Playgroud)
我需要找到数量不断增加的群体并实现:
increasing_numbers = [(7,8,9,10), (0,1,2,3,4,5)]
Run Code Online (Sandbox Code Playgroud)
最终还有越来越多的数字:
len(list(chain(*increasing_numbers)))
Run Code Online (Sandbox Code Playgroud)
还有小组的len:
increasing_num_groups_length = [len(i) for i in increasing_numbers]
Run Code Online (Sandbox Code Playgroud)
我尝试了以下来获得增加的数字:
>>> from itertools import tee, chain
>>> def pairwise(iterable):
... a, b = tee(iterable)
... next(b, None)
... return zip(a, b)
...
>>> x = [8, 9, 10, 11, 7, 1, 2, 3, 4, 5, 6]
>>> set(list(chain(*[(i,j) for i,j in pairwise(x) if j-1==i])))
set([1, 2, 3, 4, 5, …Run Code Online (Sandbox Code Playgroud) 我有一个项目,包括许多理想情况下将实现Iterable<T>和/或Iterator<T>接口的类.但是我似乎无法找到这些接口的标准TypeScript定义(例如在typescript-collections或类似的包中).
我理解这些在ECMAScript 6中通过该Symbol.iterator机制有些标准化,但我的目标是ECMAScript 5,并且在可预见的未来将保持这样.
我可以以某种方式获取这些接口而不自己定义它们(例如,为了将来与其他模块的兼容性)?
我刚刚从 PHP 7.1 文档中听说了 Iterables。但是没有得到它的实际用例,而且这个概念对我来说也不是很清楚。那么任何人都可以用一些简单的例子来解释它以更快地抓住它吗?
我想知道为什么以及我们在哪里使用它?可迭代的好处是什么?
我以前从未在 PHP 中使用过生成器,文档中也没有显示返回类型声明的示例。
在 PhpStorm 中,执行此操作时 IDE 中出现错误:
public function getDataIncrementally(): void {
yield from [/* some large set of numbers*/];
}
Run Code Online (Sandbox Code Playgroud)
错误是:
生成器只能声明返回类型为 Generator、Iterator 或 Traversable,或可迭代,不允许使用 void。
我可以看到继承树是Traversable-> Iterator-> Generator。同时,iterable是 PHP 7.1 中引入的一种新的伪类型。
iterable如果我只需要支持 PHP >= 7.1 ,是否适合用于返回类型声明?
我发现一些迭代可以重复迭代:
const iterable = {
[Symbol.iterator]: function* () {
yield 1;
yield 3;
yield 5;
}
}
console.log([...iterable]);
console.log([...iterable]);
console.log([...iterable]);Run Code Online (Sandbox Code Playgroud)
虽然有些不能:
function* generatorFn() {
yield 1;
yield 3;
yield 5;
}
const iterable = generatorFn();
console.log([...iterable]);
console.log([...iterable]);
console.log([...iterable]);Run Code Online (Sandbox Code Playgroud)
是否有一个可迭代对象是否应该重复迭代的规则?
我理解为什么它们的行为不同(这是因为第二种情况,当iterable[Symbol.iterator]调用函数时,返回相同的迭代器(它iterable本身。可以尝试iterable[Symbol.iterator]() === iterable,它也会返回true。iterable.next也是一个函数。所以在这种情况下,iterable是一个生成器object、iterable 和 iterator,所有三个)。但我想知道 iterable 作为对象类型,是否有明确定义的行为是否应该或不应该重复迭代。)
iterable ×10
python ×3
generator ×2
iterator ×2
java ×2
javascript ×2
php ×2
applicative ×1
dart ×1
ecmascript-6 ×1
interface ×1
list ×1
php-7.1 ×1
pretty-print ×1
scala ×1
tuples ×1
typescript ×1