2015年4月17日金曜日

worldcomponent The tiny FRP

http://libraries.io/npm/worldcomponent

worldcomponent 1.0.1

world component tiny FRP module

Homepage: https://github.com/sakurafunctional/worldcomponent

Platform: npm

Language: JavaScript

License: MIT

View on registry: https://www.npmjs.com/package/worldcomponent/

npm test

var ___ = require('./worldcomponent');

var ___a = ___(0);
var ___b = ___(0);

___.world = ___a.compute(___.log('a:'));
___.world = ___b.compute(___.log('b:'));

___.world = ___a.compute(function(x)
{
  ___.world = ___b.appear(x * 5);
});

var f = function()
{
  ___.world = ___a.appear(___a.now() + 1);
};
var timer = setInterval(f, 1000);

a: 0
b: 0
a: 0
b: 0
a: 1
b: 5
a: 2
b: 10
a: 3
b: 15
a: 4
b: 20
a: 5
b: 25
a: 6
b: 30
a: 7
b: 35

worldcomponent.js

var worldcomponent = function(initialVal)
{
  var computingF = [];
  var value = {};
  var state;
  Object.defineProperties(value,
  {
    val: //value.val
    {
      get: function()
      {
        return state;
      },
      set: function(x)
      {
        state = x;
        computingF.map(
          function(f)
          {
            f(x);
          });
        return;
      }
    }
  });
  var o = {
    compute: function(f)
    {
      var f1 = function()
      {
        computingF[computingF.length] = f; //push  f
        value.val = initialVal;
      };
      return f1;
    },
    appear: function(a)
    {
      var f1 = function(){value.val = a;};
      return f1;
    },
    now: function()
    {
      return value.val;
    }
  };

  return o;
};

Object.defineProperties(worldcomponent,
{
  world: //our physical world
  {
    set: function(f)
    {
      return f();
    }
  }
});

worldcomponent.log = function(a)
{
  var f = console.log.bind(console, a);
  return f;
};

module.exports = worldcomponent;

1 件のコメント:

  1. `value.val`のSetter内の処理で、配列computingFをループ処理するのにArray#mapを利用しているようですが、Array#forEachではダメなのでしょうか?
    Array#mapは配列の各要素に処理を行い、その要素を含んだ新しい配列を返す関数です。しかし、コード内ではArray#mapの返り値を利用していないようです。
    単なるループ処理であるなら、Array#forEachが適切であると考えます。

    返信削除