0

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)). 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 ] ]
Jon Tirsen
  • 4,750
  • 4
  • 29
  • 27

1 Answers1

3

It seems that Lazy.zip only accepts Array parameters [0] [1], and Lazy.range() produces a 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.

Edit: Since you're converting to an array in the end anyway, have you considered just using the native 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

BenC
  • 451
  • 3
  • 7