<commit_before>var Monad = require('./monad');

function run(tasks, value) {
	while (tasks.length) setTimeout(tasks.shift(), 0, value);
}

function pendingValue(producer) {
	var isPending = true, value, pending = [];

	producer(function(result) {
		isPending = false;
		value = result;

		run(pending, value);
	}, function(error) {
		// TODO: implement left part
	});

	return function(next) {
		if (isPending) {
			pending.push(next);
		} else {
			run([next], value);
		}
	};
}

function flatten(value, right, left) {
	if (Monad.isMonad(value)) {
		return value.bind(right, left);
	} else {
		return right(value);
	}
}

function Future(producer) {
	var wrappedValue = pendingValue(producer);

	return {
		isMonad: true,
		bind: function(right) {
			return Future(function(resolve, reject) {
				wrappedValue(function(value) {
					flatten(right(value), resolve, reject);
				});
			});
		}
	};
}

function Resolve(value) {
	return Future(function(resolve) {
		resolve(value);
	});
}

function Reject(error) {
	return Future(function(resolve, reject) {
		reject(error);
	});
}

exports.Resolve = Resolve;
exports.Reject = Reject;
exports.Future = Future;<commit_msg>separation<commit_after>var Monad = require('./monad');

function run(tasks, value) {
	while (tasks.length) {
		setTimeout(tasks.shift(), 0, value);
	}
}

function pendingValue(producer) {
	var isPending = true, value, pending = [];

	producer(function(result) {
		isPending = false;
		value = result;

		run(pending, value);
	}, function(error) {
		// TODO: implement left part
	});

	return function(next) {
		if (isPending) {
			pending.push(next);
		} else {
			run([next], value);
		}
	};
}

function flatten(value, right, left) {
	if (Monad.isMonad(value)) {
		return value.bind(right, left);
	} else {
		return right(value);
	}
}

function bindFuture(next, right, left) {
	return Future(function(resolve, reject) {
		next(function(value) {
			flatten(right(value), resolve);
		}, reject);
	});
}

function Future(producer) {
	var value = pendingValue(producer);

	return {
		isMonad: true,
		bind: function(right, left) {
			return bindFuture(value, right, left);
		}
	};
}

function Resolve(value) {
	return Future(function(resolve) {
		resolve(value);
	});
}

function Reject(error) {
	return Future(function(resolve, reject) {
		reject(error);
	});
}

exports.Resolve = Resolve;
exports.Reject = Reject;
exports.Future = Future;
{'fraction_non_alphanumeric': 0.158004158004158, 'fraction_numerical': 0.0008316008316008316, 'mean_word_length': 4.219088937093275, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2292497', 'n_tokens_mistral': 900, 'n_tokens_neox': 840, 'n_words': 176}