Functionally Recursive Application State Manager


Keywords
functional, recursive, state-management
License
Unlicense
Install
npm install recfun@0.0.11

Documentation

recfun: Recursive(ly?) Functional Application State Manager

Recfun can be used to make composable processes.

API

  • close: function(fun)
    • The close function takes a function as it's parameter. It is appended to the history array. history: array Array containing all functions that have been supplied with close()
  • run: function([to])
    • Executes the first function in history. It's output is then passed on as the parameter of the following function. If to argument is supplied, the return value will be the function at position to in the history.
  • states: [ ]
    • array When ever checkout is executed, this array will be keep the results of each execution. This way you can see how the application is modifying the initial data. The difference between states and history are 2: History keeps additonal elements that compress previous results. History keeps functions, while states keeps the results of those functions.
  • checkout: function([from])
    • Similar to as run, but will execute starting from the last known executed state.
  • collapse: TODO
  • chain: TODO

Example

// assuming Node (console) context
var recfun = require('recfun');
var model = new recfun.recfun();
model.close(function(){
    // random binary data used as our model
    return "00110";
});
model.close(function(o){
    // turn string into array of characters 
    return o.split("");
});
model.close(function(o){
    // turn array values into int
    return o.map(function(value,index,array){
        return parseInt(value);
    });
});
model.close(function(o){
    // check if values > 0
    return o.map(function(value,index,array){
        return value>0;
    })
});
model.close(function(o){
    // print results
    console.log(o);
    return o;
});
// toString() logs the recfun object. checkout() hasn't been called yet
// so function return values have not been chached in recfun.states
model.toString();   // => { history:[ [Function], [Function], [Function], 
//                    [Function], [Function] ],
//              states: [],
//              debug: false,
//              checkpoint: 0 }

// run([from]) executes the function chain from beggining to end
// without caching the results. It takes an optional 
// parameter *from* that tells it to start running from a certain 
// point in the function chain this may be useful if you have cached
// some results using checkout().
// the return value is the return value in the last function executed 
// which means that if the last function does NOT return anything, the 
// return value is undefined
model.run();    // => [ false, false, true, false, true ]

// checkout([to]) executes the function chain from beggining to end 
// while caching the results in recfun.states. It takes an optional 
// parameter *to* that tells it to stop executing at a certain point 
// in the function chain. like run() the return value is the return 
// value in the last function executed which means that if the last 
// function does NOT return anything, the return value is undefined
model.checkout();// => [ false, false, true, false, true ]

// if we use toString() again, we see all the changes to the data on 
// the recfun.states array
model.toString(); // => {   history:[ 
//                    [Function],[Function],[Function],
//                    [Function],[Function],[Function]
//                   ],
//              states:[ 
//                  '00110',
//                  [ '0', '0', '1', '1', '0' ], 
//                  [ 0, 0, 1, 1, 0 ],
//                  [ false, false, true, true, false ],
//                  [ false, false, true, true, false ]
//                  ],
//              debug: false,
//              checkpoint: 5 }