<commit_before>//
//  merge-select.swift
//  concurrency
//
//  Created by <NAME> on 2015-01-26.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

/**
  Merge an array of Receivers into one Receiver.
  Every item from the input channels will be able to be received via the returned channel.

  This function uses the select() function to merge channels; it will only block
  if all channels block. The output channel will close when all inputs are closed.

  :param: channels an array of Receivers to merge.

  :return: a single Receiver provide access to get every message received by the input Receivers.
*/

public func merge<R: ReceiverType where R: Selectable>(channels: [R]) -> Receiver<R.ReceivedElement>
{
  if channels.count == 0
  { // Not likely to happen, but return a closed channel.
    return Receiver(Chan<R.ReceivedElement>())
  }

  let (tx, rx) = Channel.Make(R.ReceivedElement.self, channels.count*2)

  async {
    let selectables = channels.map { $0 as Selectable }
    while let (s, selection) = select(selectables)
    {
      if let element: R.ReceivedElement = selection.getData()
      {
        tx <- element
      }
    }

    tx.close()<commit_msg>The select()-based version of merge() renamed to mergeSelect()<commit_after>//
//  merge-select.swift
//  concurrency
//
//  Created by <NAME> on 2015-01-26.
//  Copyright (c) 2014 <NAME>. All rights reserved.
//

/**
  Merge an array of Receivers into one Receiver.
  Every item from the input channels will be able to be received via the returned channel.

  This function uses the select() function to merge channels; it will only block
  if all channels block. The output channel will close when all inputs are closed.

  :param: channels an array of Receivers to merge.

  :return: a single Receiver provide access to get every message received by the input Receivers.
*/

public func mergeSelect<R: ReceiverType where R: Selectable>(channels: [R]) -> Receiver<R.ReceivedElement>
{
  if channels.count == 0
  { // Not likely to happen, but return a closed channel.
    return Receiver(Chan<R.ReceivedElement>())
  }

  let (tx, rx) = Channel.Make(R.ReceivedElement.self, channels.count*2)

  async {
    let selectables = channels.map { $0 as Selectable }
    while let (s, selection) = select(selectables)
    {
      if let element: R.ReceivedElement = selection.getData()
      {
        tx <- element
      }
    }

    tx.close()
{'fraction_non_alphanumeric': 0.10028889806025588, 'fraction_numerical': 0.012381345439537762, 'mean_word_length': 3.1015228426395938, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '3852847', 'n_tokens_mistral': 735, 'n_tokens_neox': 694, 'n_words': 280}