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

public struct FlatMapSequenceView<Base: SequenceType, Each: SequenceType>: SequenceType {
	private init(_ base: Base, _ transform: Base.Generator.Element -> Each) {
		self.base = base
		self.transform = transform
	}


	// MARK: SequenceType

	public func generate() -> GeneratorOf<Each.Generator.Element> {
		var outer = base.generate()
		var inner: Each.Generator?
		return GeneratorOf {
			next(
				{ () -> Bool in
					inner = outer.next().map(self.transform)?.generate()
					return inner != nil
				},
				{ inner?.next() }
			)
		}
	}


	// MARK: Private

	private let base: Base
	private let transform: Base.Generator.Element -> Each
}

private func next<T>(outer: () -> Bool, inner: () -> T?) -> T? {
	return inner() ?? (outer() ? next(outer, inner) : nil)
}


extension LazySequence {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazySequence<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}

extension LazyForwardCollection {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazyForwardCollection<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}

extension LazyBidirectionalCollection {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazyBidirectionalCollection<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}<commit_msg>Extend lazy flatMap to random-access collections.<commit_after>//  Copyright (c) 2015 <NAME>. All rights reserved.

public struct FlatMapSequenceView<Base: SequenceType, Each: SequenceType>: SequenceType {
	private init(_ base: Base, _ transform: Base.Generator.Element -> Each) {
		self.base = base
		self.transform = transform
	}


	// MARK: SequenceType

	public func generate() -> GeneratorOf<Each.Generator.Element> {
		var outer = base.generate()
		var inner: Each.Generator?
		return GeneratorOf {
			next(
				{ () -> Bool in
					inner = outer.next().map(self.transform)?.generate()
					return inner != nil
				},
				{ inner?.next() }
			)
		}
	}


	// MARK: Private

	private let base: Base
	private let transform: Base.Generator.Element -> Each
}

private func next<T>(outer: () -> Bool, inner: () -> T?) -> T? {
	return inner() ?? (outer() ? next(outer, inner) : nil)
}


extension LazySequence {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazySequence<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}

extension LazyForwardCollection {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazyForwardCollection<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}

extension LazyBidirectionalCollection {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazyBidirectionalCollection<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}

extension LazyRandomAccessCollection {
	func flatMap<Each: SequenceType>(transform: S.Generator.Element -> Each) -> LazySequence<FlatMapSequenceView<LazyRandomAccessCollection<S>, Each>> {
		return lazy(FlatMapSequenceView(self, transform))
	}
}
{'fraction_non_alphanumeric': 0.13396282089111833, 'fraction_numerical': 0.002360578341693715, 'mean_word_length': 4.7360406091370555, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 67, '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': '917248', 'n_tokens_mistral': 1136, 'n_tokens_neox': 1022, 'n_words': 235}