/*global session*/  //JSLinted, http://jslint.com
/*
   return array of intermediate values for
   http://en.wikipedia.org/wiki/Collatz_conjecture

   $ coproc2 collatz_conjecture.js <(echo '{"value":50}') http://dp3-l3:2227; echo
   [50,25,76,38,19,58,29,88,44,22,11,34,17,52,26,13,40,20,10,5,16,8,4,2,1]
   $
*/
session.input.readAsJSON(function (error, json) {
    'use strict';
    if (error) {
        session.reject("Unexpected error in readAsJSON(): " + error);
        return;
    }

    function f(n) {
        return [n].concat((n === 1)            
            ? [] 
            : f((n % 2) 
                ? 3 * n + 1 
                : n / 2));
    }

    session.output.write(f(json.value));
});