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

/// An iterable stream.
public enum Stream<T> {
	case Cons(Box<T>, Memo<Stream<T>>)
	case Nil


	// MARK: Lifecycle

	/// Initializes with a ReducibleType.
	public init<R: ReducibleType where R.Element == T>(_ reducible: R) {
		let reduce: Reducible<Stream, T>.Enumerator = (reducible.reducer()) { initial, _ in initial }
		self = reduce(Nil, fix { combine in
			{ into, each in .right(.cons(each, Memo(reduce(into, combine)))) }
		})
	}


	/// Initializes with a generating function.
	public init(_ f: () -> T?) {
		self = Stream.construct(f)()
	}

	/// Initializes with a `SequenceType`.
	public init<S: SequenceType where S.Generator.Element == T>(_ sequence: S) {
		var generator = sequence.generate()
		self.init({ generator.next() })
	}


	/// Maps a generator of `T?` into a generator of `Stream`.
	public static func construct(generate: () -> T?) -> () -> Stream<T> {
		return fix { recur in
			{ generate().map { self.cons($0, Memo(recur())) } ?? Nil }
		}
	}

	/// Constructs a `Stream` from `first` and its `@autoclosure`’d continuation.
	public static func cons(first: T, _ rest: @autoclosure () -> Stream) -> Stream {
		return Cons(Box(first), Memo(unevaluated: rest))
	}

	/// Constructs a `Stream` from `first` and its `Memo`ized continuation.
	public static func cons(first: T, _ rest: Memo<Stream>) -> Stream {
		return Cons(Box(first), rest)
	}


	// MARK: Properties

	public var first: T? {
		switch self {
		case let Cons(x, _):
			return x.value
		case Nil:
			return nil
		}
	}

	public var rest: Stream {
		switch self {
		case let Cons(_, rest):
			return rest.value
		case Nil:
			return Nil
		}
	}


	// MARK: Combinators

	/// Returns a `Stream` of the first `n` elements of the receiver.
	///
	/// If `n` <= 0, returns the empty `Stream`.
	public func take(n: Int) -> Stream {
		if n <= 0 { return Nil }

		let rest = self.rest
		return first.map { .cons($0, rest.take(n - 1)) } ?? Nil
	}

	/// Returns a `Stream` without the first `n` elements of `stream`.
	///
	/// If `n` <= 0, returns the receiver.
	///
	/// If `n` <= the length of the receiver, returns the empty `Stream`.
	public func drop(n: Int) -> Stream {
		if n <= 0 { return self }
		return rest.drop(n - 1)
	}


	/// Returns a `Stream` produced by mapping the elements of the receiver with `f`.
	public func map<U>(f: T -> U) -> Stream<U> {
		let rest = self.rest
		return first.map { .cons(f($0), rest.map(f)) } ?? Stream<U>.Nil
	}
}


// MARK: API

/// Returns the first element of `stream`, or `nil` if `stream` is `Nil`.
public func first<T>(stream: Stream<T>) -> T? {
	return stream.first
}

/// Drops the first element of `stream`.
public func dropFirst<T>(stream: Stream<T>) -> Stream<T> {
	return stream.rest
}


// MARK: SequenceType conformance.

extension Stream: SequenceType {
	public func generate() -> GeneratorOf<T> {
		var stream = self<commit_msg>Implement Stream destructuring to (T, Memo<Stream>)?.<commit_after>//  Copyright (c) 2014 <NAME>. All rights reserved.

/// An iterable stream.
public enum Stream<T> {
	case Cons(Box<T>, Memo<Stream<T>>)
	case Nil


	// MARK: Lifecycle

	/// Initializes with a ReducibleType.
	public init<R: ReducibleType where R.Element == T>(_ reducible: R) {
		let reduce: Reducible<Stream, T>.Enumerator = (reducible.reducer()) { initial, _ in initial }
		self = reduce(Nil, fix { combine in
			{ into, each in .right(.cons(each, Memo(reduce(into, combine)))) }
		})
	}


	/// Initializes with a generating function.
	public init(_ f: () -> T?) {
		self = Stream.construct(f)()
	}

	/// Initializes with a `SequenceType`.
	public init<S: SequenceType where S.Generator.Element == T>(_ sequence: S) {
		var generator = sequence.generate()
		self.init({ generator.next() })
	}


	/// Maps a generator of `T?` into a generator of `Stream`.
	public static func construct(generate: () -> T?) -> () -> Stream<T> {
		return fix { recur in
			{ generate().map { self.cons($0, Memo(recur())) } ?? Nil }
		}
	}

	/// Constructs a `Stream` from `first` and its `@autoclosure`’d continuation.
	public static func cons(first: T, _ rest: @autoclosure () -> Stream) -> Stream {
		return Cons(Box(first), Memo(unevaluated: rest))
	}

	/// Constructs a `Stream` from `first` and its `Memo`ized continuation.
	public static func cons(first: T, _ rest: Memo<Stream>) -> Stream {
		return Cons(Box(first), rest)
	}


	// MARK: Properties

	public var first: T? {
		return destructure()?.0
	}

	public var rest: Stream {
		return destructure()?.1.value ?? Nil
	}


	public func destructure() -> (T, Memo<Stream>)? {
		switch self {
		case let Cons(x, rest):
			return (x.value, rest)
		case Nil:
			return nil
		}
	}


	// MARK: Combinators

	/// Returns a `Stream` of the first `n` elements of the receiver.
	///
	/// If `n` <= 0, returns the empty `Stream`.
	public func take(n: Int) -> Stream {
		if n <= 0 { return Nil }

		return destructure().map { .cons($0, $1.value.take(n - 1)) } ?? Nil
	}

	/// Returns a `Stream` without the first `n` elements of `stream`.
	///
	/// If `n` <= 0, returns the receiver.
	///
	/// If `n` <= the length of the receiver, returns the empty `Stream`.
	public func drop(n: Int) -> Stream {
		if n <= 0 { return self }

		return rest.drop(n - 1)
	}


	/// Returns a `Stream` produced by mapping the elements of the receiver with `f`.
	public func map<U>(f: T -> U) -> Stream<U> {
		return destructure().map { .cons(f($0), $1.value.map(f)) } ?? .Nil
	}
}


// MARK: API

/// Returns the first element of `stream`, or `nil` if `stream` is `Nil`.
public func first<T>(stream: Stream<T>) -> T? {
	return stream.first
}

/// Drops the first element of `stream`.
public func dropFirst<T>(stream: Stream<T>) -> Stream<T> {
	return stream.rest
}


// MARK: SequenceType conformance.

extension Stream: SequenceType {
	public func generate() -> GeneratorOf<T> {
		var stream = self
{'fraction_non_alphanumeric': 0.16420945602440265, 'fraction_numerical': 0.005083884087442806, 'mean_word_length': 3.1043115438108484, 'pattern_counts': {'":': 0, '<': 52, '<?xml version=': 0, '>': 71, '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': '732242', 'n_tokens_mistral': 2158, 'n_tokens_neox': 1972, 'n_words': 662}