// Generated by CoffeeScript 1.8.0
(function() {
  var Promise, delayP, delay_, waitForP, whileP;

  Promise = require("promise");

  exports.ifP = function(i, t, e) {
    return Promise.from(i).then(function(truth) {
      if (truth && typeof t === "function") {
        return t(truth);
      } else if (typeof e === "function") {
        return e(truth);
      }
    });
  };

  exports.mapP = function(arrP, fP) {
    return Promise.all(arrP, fP).then(function(_arg) {
      var arr, f;
      arr = _arg[0], f = _arg[1];
      return Promise.all(arr).then(function(elms) {
        return Promise.all(elms.map(f));
      });
    });
  };

  exports.composeP = function(funs) {
    var cmp;
    cmp = function(f, g) {
      return function(x) {
        return Promise.from(x).then(g).then(f);
      };
    };
    return funs.reduceRight(function(prev, cur) {
      return cmp(cur, prev);
    });
  };

  exports.pipeP = function(val, funs) {
    var cmp;
    cmp = function(prev, cur) {
      return Promise.from(prev).then(cur);
    };
    return funs.reduce(cmp, val);
  };

  exports.eachP = function(arr0, fun) {
    return Promise.from(arr0).then(function(arr) {
      return arr.reduce(function(prev0, cur0) {
        return Promise.from(prev0).then(function(prev) {
          return Promise.from(cur0).then(function(cur) {
            return Promise.from(fun(cur)).then(function(fcur) {
              var nextPrev;
              nextPrev = [].concat(prev, fcur);
              return nextPrev;
            });
          });
        });
      }, []);
    });
  };

  exports.reduceP = function(arr0, fun0, start0) {
    var argc;
    argc = arguments.length;
    return Promise.all([arr0, fun0, start0]).then(function(_arg) {
      var arr, fun, funfun, start;
      arr = _arg[0], fun = _arg[1], start = _arg[2];
      funfun = function(prev0, cur0) {
        return Promise.all([prev0, cur0]).then(function(_arg1) {
          var cur, prev;
          prev = _arg1[0], cur = _arg1[1];
          return fun(prev, cur);
        });
      };
      if (argc > 2) {
        return arr.reduce(funfun, start);
      } else {
        return arr.reduce(funfun);
      }
    });
  };

  exports.whileP = whileP = function(guard, statement, value0) {
    return Promise.from(value0).then(function(current) {
      return Promise.from(guard(current)).then(function(g) {
        if (g) {
          return Promise.from(statement(current)).then(function(next) {
            return whileP(guard, statement, next);
          });
        } else {
          return current;
        }
      });
    });
  };

  exports.delayP = delayP = function(millis, value) {
    return new Promise(function(resolve) {
      return setTimeout((function() {
        return resolve(value);
      }), millis);
    });
  };

  delay_ = function(millis) {
    return function(value) {
      return delayP(millis, value);
    };
  };

  exports.waitForP = waitForP = function(condition, timeout, interval, value) {
    var guard, start;
    start = Date.now();
    guard = function(v) {
      var msg;
      if (Date.now() - start > timeout) {
        msg = "waitForP: timeout (" + timeout + "ms) exceeded";
        throw new Error(msg);
      } else {
        return Promise.from(condition(v)).then(function(c) {
          return !c;
        });
      }
    };
    return whileP(guard, delay_(interval || 20), value);
  };

}).call(this);
