0

I have a code to generate fib sequences with lazy.js

var fibF = function()
{
  var seq = []; //build sequence array in this closure
  var f = function(n)
  {
    var val;
    if (n <= 1)
    {
      val = 1; // as the Fib definition in Math
    }
    else
    {
      val = seq[n - 2] + seq[n - 1]; // as the Fib definition in Math
    }
    seq[n] = val;
    return val;
  };
  return f;
}();

var fibSequence = _.generate(fibF);  


/*  just for test
var fib_1000 =
  fibSequence
  .take(1000)  
  .toArray();  

console.log(fib_1000); 
//[ 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,...........,4.346655768693743e+208 ]

*/

At the same time, I have a code of timer with Bacon.js

var B = require('baconjs');

var timeSequence = B
      .interval(1000); //every second
     
timeSequence 
    .onValue(function() 
      {
        console.log(require('moment')().format('MMMM Do YYYY, h:mm:ss')); 
        // print timestamps every second
      }); 

Then,

I want to map the the fibSequence onto timeSequence such as

var mySequence = fibSequence.map(timeSequence);

or

var mySequence = timeSequence.map(fibSequence);

Is it possible?

If so, please show me the way.

Any workaround solution is welcome.

Thanks.

EDIT working code:

//to simplify use Natrual, instead of Fib

var _ = require('lazy.js');
var __ = require('baconjs');

var natural = function(n)
{
  return n;
};

var _natural = _.generate(natural); //natural numbers
var __timer = __.interval(1000); //every second

var map_to__ = function(_seq, __seq)
{
  var it = _seq.getIterator();

  var sequence =
    __seq
    .map(function()
    {
      it.moveNext();
      return it.current();
    });

  return sequence;
};

var __mappedTimer = map_to__(_natural, __timer);

__mappedTimer
  .onValue(function(x)
  {
    console.log(x); // print every second
  });
Community
  • 1
  • 1

1 Answers1

0

I'm not sure whether this is the intended use of iterators, but it should work:

var it = fibSequence.getIterator()
var mySequence = timeSequence.map(function() {
    return it.moveNext() && it.current();
});
Bergi
  • 513,640
  • 108
  • 821
  • 1,164
  • Thanks I sort of get the idea, but still not working , please see my Q edit. –  Jun 25 '14 at 12:30
  • Hm, I've tested it online now and it works. What exactly is the error you get? – Bergi Jun 25 '14 at 13:47
  • .onValue(function(x) ^ TypeError: Object object has no method 'onValue' –  Jun 25 '14 at 18:51
  • So, seems `mySequence` is not Bacon object, I thought. –  Jun 25 '14 at 18:52
  • As I said, [I cannot reproduce](http://jsfiddle.net/V9h83/) (with your code). There must be something else you're doing wrong, yes. – Bergi Jun 25 '14 at 19:01
  • You said, you've tested in online now and it works, but the jsFiddle http://jsfiddle.net/V9h83/ does not work since it uses hotlink of github. –  Jun 25 '14 at 19:48
  • So, I upload lazy, bacon lib files to my own github.io page, and linked them. Now it works. http://jsfiddle.net/Uq2Jt/ Thanks. –  Jun 25 '14 at 19:49
  • However, the strange thing is once I made the same code to node.js, the error occurred. I do not know why. –  Jun 25 '14 at 19:55
  • Sorry, I deifined Obj.map in another place, and which conflicts in the code. works. I update my Q. Appreciated. –  Jun 25 '14 at 21:46