Lazy.js:用范围压缩

时间:2015-12-18 09:29:30

标签: javascript lazy.js

我想用数组将计数压缩在一起。似乎这样的事情是最优雅的做法 <meta name="viewport" content="width=device-width, initial-scale=1.0"> /* Extra small devices (phones, less than 768px) */ /* No media query since this is the default in Bootstrap */ /* Small devices (tablets, 768px and up) */ @media (min-width: @screen-sm-min) { ... } /* Medium devices (desktops, 992px and up) */ @media (min-width: @screen-md-min) { ... } /* Large devices (large desktops, 1200px and up) */ @media (min-width: @screen-lg-min) { ... } @media (max-width: @screen-xs-max) { ... } @media (min-width: @screen-sm-min) and (max-width: @screen-sm-max) { ... } @media (min-width: @screen-md-min) and (max-width: @screen-md-max) { ... } @media (min-width: @screen-lg-min) { ... } 。但它并没有产生我期望的结果。这是一个单元测试:

Lazy([...]).zip(Lazy.range(1, Infinity))

失败并出现以下错误:

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());
    });
  });
});

1 个答案:

答案 0 :(得分:3)

似乎Lazy.zip只接受Array个参数[0] [1],而Lazy.range()会产生GeneratedSequence

在设计用于懒惰的图书馆中,这似乎有点不直观。

我想你可以做点像

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

顺便说一下,zip应该生成[['a', 1], ['b', 2]],而不是[['a', 'b'], [1, 2]],除非我误解了你正在测试的内容。

修改:由于您最终要转换为数组,您是否考虑过仅使用原生Array.map

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

-

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

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