<commit_before>/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author <NAME> */
/** @author <NAME> */
/** @module */

var Stream = require('../Stream');
var promise = require('../promises');

var when = promise.when;
var resolve = promise.Promise.resolve;
var reject = promise.Promise.reject;

exports.flatMapError = flatMapError;
exports.throwError = throwError;

/**
 * Create a stream containing only an error
 * @param {*} e error value, preferably an Error or Error subtype
 * @returns {Stream} new stream containing only an error
 */
function throwError(e) {
	return new Stream(reject, e);
}

/**
 * If stream encounters an error, recover and continue with items from stream
 * returned by f.
 * stream:                  -a-b-c-X-
 * f(X):                           d-e-f-g-
 * flatMapError(f, stream): -a-b-c-d-e-f-g-
 * @param {function(error:*):Stream} f function which returns a new stream
 * @param {Stream} stream
 * @returns {Stream} new stream which will recover from an error by calling f
 */
function flatMapError(f, stream) {
	return stream.beget(stepFlatMapError, { stream: stream, recover: f }, dispose);
}

function stepFlatMapError (s) {
	return resolve(s.stream.state).then(function (state) {
		return stepWithRecovery(s.recover, s.stream.beget(s.stream.step, state));
	}).catch(function (e) {
		// TODO: Should we call s.stream.dispose here?
		return stepWithRecovery(thrower, s.recover(e));
	});
}

function stepWithRecovery(f, stream) {
	return when(function (i) {
		return i.done ? i
			: i.withState({ stream: stream.beget(stream.step, i.state), recover: f });
	}, when(stream.step, stream.state));
}

function dispose(t, x, s) {
	return s.stream.dispose(t, x, s.stream.state);
}

function thrower(e) {
	throw e;
}<commit_msg>Avoid creating extra wrapper streams in flatMapError<commit_after>/** @license MIT License (c) copyright 2010-2014 original author or authors */
/** @author <NAME> */
/** @author <NAME> */
/** @module */

var Stream = require('../Stream');
var promise = require('../promises');

var when = promise.when;
var resolve = promise.Promise.resolve;
var reject = promise.Promise.reject;

exports.flatMapError = flatMapError;
exports.throwError = throwError;

/**
 * Create a stream containing only an error
 * @param {*} e error value, preferably an Error or Error subtype
 * @returns {Stream} new stream containing only an error
 */
function throwError(e) {
	return new Stream(reject, e);
}

/**
 * If stream encounters an error, recover and continue with items from stream
 * returned by f.
 * stream:                  -a-b-c-X-
 * f(X):                           d-e-f-g-
 * flatMapError(f, stream): -a-b-c-d-e-f-g-
 * @param {function(error:*):Stream} f function which returns a new stream
 * @param {Stream} stream
 * @returns {Stream} new stream which will recover from an error by calling f
 */
function flatMapError(f, stream) {
	return stream.beget(stepFlatMapError, { stream: stream, state: stream.state, recover: f }, dispose);
}

function stepFlatMapError (s) {
	return resolve(s.state).then(function (state) {
		return stepWithRecovery(s.recover, s.stream, state);
	}).catch(function (e) {
		// TODO: Should we call s.stream.dispose here?
		var stream = s.recover(e);
		return stepWithRecovery(thrower, stream, stream.state);
	});
}

function stepWithRecovery(f, stream, state) {
	return when(function (i) {
		return i.done ? i
			: i.withState({ stream: stream, state: i.state, recover: f });
	}, when(stream.step, state));
}

function dispose(t, x, s) {
	return s.stream.dispose(t, x, s.state);
}

function thrower(e) {
	throw e;
}
{'fraction_non_alphanumeric': 0.14636163175303196, 'fraction_numerical': 0.004410143329658214, 'mean_word_length': 3.7007772020725387, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, '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': '1040086', 'n_tokens_mistral': 1194, 'n_tokens_neox': 1153, 'n_words': 360}