<commit_before>//
//  Producer.swift
//  ReactiveCocoa
//
//  Created by <NAME> on 2014-06-25.
//  Copyright (c) 2014 GitHub. All rights reserved.
//

import LlamaKit

/// A stream that will begin generating Events when a Consumer is attached,
/// possibly performing some side effects in the process. Events are pushed to
/// the consumer as they are generated.
///
/// A corollary to this is that different Consumers may see a different timing
/// of Events, or even a different version of events altogether.
public struct Producer<T> {
	private let producerClosure: Consumer<T> -> ()

	/// Initializes a Producer that will run the given action whenever an
	/// Consumer is attached.
	public init(produce: Consumer<T> -> ()) {
		producerClosure = produce
	}

	/// Creates a Producer that will immediately complete.
	public static func empty() -> Producer<T> {
		return Producer { consumer in
			consumer.put(.Completed)
		}
	}

	/// Creates a Producer that will immediately yield a single value then
	/// complete.
	public static func single(value: T) -> Producer<T> {
		return Producer { consumer in
			consumer.put(.Next(Box(value)))
			consumer.put(.Completed)
		}
	}

	/// Creates a Producer that will immediately generate an error.
	public static func error(error: NSError) -> Producer<T> {
		return Producer { consumer in
			consumer.put(.Error(error))
		}
	}

	/// Creates a Producer that will iterate over the given sequence whenever a
	/// Consumer is attached.
	///
	/// If the Producer will be consumed multiple times, the given sequence must
	/// be multi-pass (i.e., support obtaining and using multiple generators).
	public static func fromSequence<S: SequenceType where S.Generator.Element == T>(sequence: S) -> Producer<T> {
		return Producer { consumer in
			for value in sequence {
				consumer.put(.Next(Box(value)))
			}

			consumer.put(.Completed)
		}
	}

	/// Creates a Producer that will never send any events.
	public static func never() -> Producer<T> {
		return Producer { _ in () }
	}

	/// Starts producing events for the given consumer, performing any side
	/// effects embedded within the Producer.
	///
	/// Optionally returns a Disposable which will cancel the work associated
	/// with event production, and prevent any further events from being sent.
	public func produce(consumer: Consumer<T>) -> Disposable {
		producerClosure(consumer)
		return consumer.disposable<commit_msg>Write Producer.fromSequence the annoying way<commit_after>//
//  Producer.swift
//  ReactiveCocoa
//
//  Created by <NAME> on 2014-06-25.
//  Copyright (c) 2014 GitHub. All rights reserved.
//

import LlamaKit

/// A stream that will begin generating Events when a Consumer is attached,
/// possibly performing some side effects in the process. Events are pushed to
/// the consumer as they are generated.
///
/// A corollary to this is that different Consumers may see a different timing
/// of Events, or even a different version of events altogether.
public struct Producer<T> {
	private let producerClosure: Consumer<T> -> ()

	/// Initializes a Producer that will run the given action whenever an
	/// Consumer is attached.
	public init(produce: Consumer<T> -> ()) {
		producerClosure = produce
	}

	/// Creates a Producer that will immediately complete.
	public static func empty() -> Producer<T> {
		return Producer { consumer in
			consumer.put(.Completed)
		}
	}

	/// Creates a Producer that will immediately yield a single value then
	/// complete.
	public static func single(value: T) -> Producer<T> {
		return Producer { consumer in
			consumer.put(.Next(Box(value)))
			consumer.put(.Completed)
		}
	}

	/// Creates a Producer that will immediately generate an error.
	public static func error(error: NSError) -> Producer<T> {
		return Producer { consumer in
			consumer.put(.Error(error))
		}
	}

	/// Creates a Producer that will iterate over the given sequence whenever a
	/// Consumer is attached.
	///
	/// If the Producer will be consumed multiple times, the given sequence must
	/// be multi-pass (i.e., support obtaining and using multiple generators).
	public static func fromSequence<S: SequenceType where S.Generator.Element == T>(sequence: S) -> Producer<T> {
		return Producer { consumer in
			var generator = sequence.generate()

			while let value: T = generator.next() {
				consumer.put(.Next(Box(value)))
			}

			consumer.put(.Completed)
		}
	}

	/// Creates a Producer that will never send any events.
	public static func never() -> Producer<T> {
		return Producer { _ in () }
	}

	/// Starts producing events for the given consumer, performing any side
	/// effects embedded within the Producer.
	///
	/// Optionally returns a Disposable which will cancel the work associated
	/// with event production, and prevent any further events from being sent.
	public func produce(consumer: Consumer<T>) -> Disposable {
		producerClosure(consumer)
		return consumer.disposable
{'fraction_non_alphanumeric': 0.10367960967676357, 'fraction_numerical': 0.00487904045537711, 'mean_word_length': 3.300699300699301, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 41, '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': '897004', 'n_tokens_mistral': 1503, 'n_tokens_neox': 1335, 'n_words': 555}