<commit_before>//  Copyright (c) 2015 <NAME>. All rights reserved.

/// Parses `parser` 0 or one time.
public postfix func |? <C: CollectionType, T> (parser: Parser<C, T>.Function) -> Parser<C, T?>.Function {
	return (parser --> unit) | { .right(nil, $1) }
}


/// Parses either `left` or `right`.
public func | <C: CollectionType, T, U> (left: Parser<C, T>.Function, right: Parser<C, U>.Function) -> Parser<C, Either<T, U>>.Function {
	return alternate(left, right)
}

/// Parses either `left` or `right` and coalesces their trees.
public func | <C: CollectionType, T> (left: Parser<C, T>.Function, right: Parser<C, T>.Function) -> Parser<C, T>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: id, ifRight: id) }
}

/// Parses either `left` or `right`, dropping `right`’s parse tree.
public func | <C: CollectionType, T> (left: Parser<C, T>.Function, right: Parser<C, Ignore>.Function) -> Parser<C, T?>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: unit, ifRight: const(nil)) }
}

/// Parses either `left` or `right`, dropping `left`’s parse tree.
public func | <C: CollectionType, T> (left: Parser<C, Ignore>.Function, right: Parser<C, T>.Function) -> Parser<C, T?>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: const(nil), ifRight: unit) }
}

/// Parses either `left` or `right`, dropping both parse trees.
public func | <C: CollectionType> (left: Parser<C, Ignore>.Function, right: Parser<C, Ignore>.Function) -> Parser<C, Ignore>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: id, ifRight: id) }
}


// MARK: - Private

/// Defines alternation for use in the `|` operator definitions above.
private func alternate<C: CollectionType, T, U>(left: Parser<C, T>.Function, right: Parser<C, U>.Function)(input: C, index: C.Index) -> Parser<C, Either<T, U>>.Result {
	return left(input, index).map { (.left($0), $1) } ?? right(input, index).map { (.right($0), $1) }
}


// MARK: - Operators

/// Optional alternation operator.
postfix operator |? {}


// MARK: - Imports

import Either
import Prelude
<commit_msg>Either disjunction.<commit_after>//  Copyright (c) 2015 <NAME>. All rights reserved.

/// Parses `parser` 0 or one time.
public postfix func |? <C: CollectionType, T> (parser: Parser<C, T>.Function) -> Parser<C, T?>.Function {
	return (parser --> unit) | { .right(nil, $1) }
}


/// Parses either `left` or `right`.
public func | <C: CollectionType, T, U> (left: Parser<C, T>.Function, right: Parser<C, U>.Function) -> Parser<C, Either<T, U>>.Function {
	return alternate(left, right)
}

/// Parses either `left` or `right` and coalesces their trees.
public func | <C: CollectionType, T> (left: Parser<C, T>.Function, right: Parser<C, T>.Function) -> Parser<C, T>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: id, ifRight: id) }
}

/// Parses either `left` or `right`, dropping `right`’s parse tree.
public func | <C: CollectionType, T> (left: Parser<C, T>.Function, right: Parser<C, Ignore>.Function) -> Parser<C, T?>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: unit, ifRight: const(nil)) }
}

/// Parses either `left` or `right`, dropping `left`’s parse tree.
public func | <C: CollectionType, T> (left: Parser<C, Ignore>.Function, right: Parser<C, T>.Function) -> Parser<C, T?>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: const(nil), ifRight: unit) }
}

/// Parses either `left` or `right`, dropping both parse trees.
public func | <C: CollectionType> (left: Parser<C, Ignore>.Function, right: Parser<C, Ignore>.Function) -> Parser<C, Ignore>.Function {
	return alternate(left, right) --> { $0.either(ifLeft: id, ifRight: id) }
}


// MARK: - Private

/// Defines alternation for use in the `|` operator definitions above.
private func alternate<C: CollectionType, T, U>(left: Parser<C, T>.Function, right: Parser<C, U>.Function)(input: C, index: C.Index) -> Parser<C, Either<T, U>>.Result {
	return left(input, index).map { (.left($0), $1) } ?? right(input, index).map { (.right($0), $1) }
}

/// Disjunction of two `Either`s.
private func ||| <A, B> (a: Either<A, B>, b: Either<A, B>) -> Either<(A, A), B> {
	return (a.right ?? b.right).map(Either<(A, A), B>.right) ?? (a.left &&& b.left).map(Either<(A, A), B>.left)!
}


// MARK: - Operators

/// Optional alternation operator.
postfix operator |? {}


// MARK: - Imports

import Either
import Prelude

{'fraction_non_alphanumeric': 0.19505556815604447, 'fraction_numerical': 0.0063506464050805175, 'mean_word_length': 4.188235294117647, 'pattern_counts': {'":': 0, '<': 69, '<?xml version=': 0, '>': 94, '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': '7626542', 'n_tokens_mistral': 1595, 'n_tokens_neox': 1549, 'n_words': 463}