简体   繁体   English

Lazy.js:压缩范围

[英]Lazy.js: zipping with a range

I'd like to zip together a count with an array. 我想将一个计数与一个数组压缩在一起。 It seems something like this would be the most elegant way of doing it Lazy([...]).zip(Lazy.range(1, Infinity)) . 看起来像这样的事情可能是最优雅的方式Lazy([...]).zip(Lazy.range(1, Infinity)) But it doesn't produce the result I expect. 但这不会产生我期望的结果。 Here is a unit test: 这是一个单元测试:

var assert = require('assert');
var Lazy = require('lazy.js');

describe('Lazy', () => {
  describe('#zip()', () => {
    it('can zip with infinite range', () => {
      assert.deepEqual([['a', 1], ['b', 2]],
        Lazy(['a', 'b']).zip(Lazy.range(1, Infinity)).toArray());
    });
  });
});

It fails with the following error: 失败并显示以下错误:

AssertionError: [ [ 'a', 'b' ], [ 1, 2 ] ] deepEqual [ [ 'a', undefined ], [ 'b', undefined ] ]

It seems that Lazy.zip only accepts Array parameters [0] [1], and Lazy.range() produces a GeneratedSequence . 看来Lazy.zip仅接受Array参数[0] [1],而Lazy.range()GeneratedSequence

This does seem a little unintuitive in a library designed for, well, laziness. 在为懒惰而设计的库中,这似乎有点不直观。

I suppose you could do something like 我想你可以做些类似的事情

Lazy(arr).zip(Lazy.range(1, arr.length + 1).toArray()).toArray()

Incidentally, zip should produce [['a', 1], ['b', 2]] , as opposed to [['a', 'b'], [1, 2]] , which unless I'm mistaken is what you're currently testing for. 顺便说一下, zip应该产生[['a', 1], ['b', 2]] ,而不是[['a', 'b'], [1, 2]] ,除非我弄错了是您目前正在测试的内容。

Edit: Since you're converting to an array in the end anyway, have you considered just using the native Array.map ? 编辑:由于无论如何最终都将转换为数组,是否考虑过仅使用本机Array.map

arr.map((x, i) => [x, i + 1]);

-- -

[0] http://danieltao.com/lazy.js/docs/#Sequence-zip [0] http://danieltao.com/lazy.js/docs/#Sequence-zip

[1] https://github.com/dtao/lazy.js/issues/87 [1] https://github.com/dtao/lazy.js/issues/87

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM