text:
/**
 * @class CM_FormField_Text
 * @extends CM_FormField_Abstract
 */
var CM_FormField_Text = CM_FormField_Abstract.extend({
  _class: 'CM_FormField_Text',

  /** @type Boolean */
  _skipTriggerChange: false,

  /** @type String */
  _valueLast: null,

  events: {
    'blur input, textarea': function() {
      this.trigger('blur');
    },
    'focus input, textarea': function() {
      this.trigger('focus');
    },
    'change input, textarea': function() {
      this.triggerChange();
    }
  },

  ready: function() {
    this._valueLast = this.getInput().val();
  },

  /**
   * @param {String} value
   */
  setValue: function(value) {
    this._skipTriggerChange = true;
    this.$('input, textarea').val(value);
    this._skipTriggerChange = false;
  },

  /**
   * @return {Boolean}
   */
  hasFocus: function() {
    return this.getInput().is(':focus');
  },

  triggerChange: function() {
    var valueCurrent = this.getInput().val();
    if (this._valueLast !== valueCurrent) {
      this._valueLast = valueCurrent;
      if (!this._skipTriggerChange) {
        this.trigger('change');
      }
    }
  },

  enableTriggerChangeOnInput: function() {
    // `propertychange` and `keyup` needed for IE9
    this.getInput().on('input propertychange keyup', _.bind(this.triggerChange, this));
  }
});


text:
package io.redlink.smarti.processor.token;

import org.springframework.stereotype.Component;

import io.redlink.smarti.model.Conversation;
import io.redlink.smarti.model.Message;
import io.redlink.smarti.model.Token;
import io.redlink.smarti.processor.token.impl.TokenFilterProcessor;

/**
 * {@link Component}s implementing this interface will be used
 * by the {@link TokenFilterProcessor} to filter {@link Token}s
 * extracted from {@link Message}s of a {@link Conversation}
 * an conversation
 * @author Rupert Westenthaler
 *
 */
public interface TokenFilter {

    /**
     * Checks if the parsed token should be filtered given the
     * parsed {@link Message} and {@link Conversation} as context
     * @param token the token
     * @param lang the language of the conversation
     * @param con the conversation
     * @return true if the token should be removed.
     * Otherwise false
     */
    boolean filter(Token token, String lang, Conversation con);

}

redpajama_set_name: RedPajamaGithub


text:
import { routerReducer } from 'react-router-redux';
import { combineReducers } from 'redux';
import clients from './clientReducer'

  const rootReducer = combineReducers({
    routing: routerReducer,
    clients
  })

export default rootReducer;


text:
package workflow

import (
	"context"

	zanzibar "github.com/uber/zanzibar/runtime"
)

// ServiceCFrontHelloWorkflow defines the interface for ServiceCFrontHello workflow
type ServiceCFrontHelloWorkflow interface {
	Handle(
		ctx context.Context,
		reqHeaders zanzibar.Header,
	) (context.Context, string, zanzibar.Header, error)
}


text:
// Follow up for problem "Populating Next Right Pointers in Each Node".

// What if the given tree could be any binary tree? Would your previous solution still work?

// Note:

// You may only use constant extra space.
// For example,
// Given the following binary tree,
//          1
//        /  \
//       2    3
//      / \    \
//     4   5    7
// After calling your function, the tree should look like:
//          1 -> NULL
//        /  \
//       2 -> 3 -> NULL
//      / \    \
//     4-> 5 -> 7 -> NULL

// 看都看了好久。。。 这道题朝着上次开始的方向想就对了,一会再写一个queue based level order traverse.

// Method 1 两层while分别在进行:外层:走root层有没有走到头
// 内层走child一直要走到链接玩。
// tricky part is: currentChild开始是tempChild的地址,进入内层loop第一个if满足的情况下会修改tempChild
// next的元素置为List的首元素,而后边的操作会让currentChild离开tempChild的绑定 从而在内层while走完之后赋值给
// root 滚动进入下一个level

/**
 * Definition for binary tree with next pointer.
 * public class TreeLinkNode {
 *     int val;
 *     TreeLinkNode left, right, next;
 *     TreeLinkNode(int x) { val = x; }
 * }
 */

public class Solution {
    public void connect(TreeLinkNode root) {
        while(root != null){
            TreeLinkNode tempChild = new TreeLinkNode(0);
            TreeLinkNode currentChild = tempChild;
            while(root != null){
                if(root.left != null) {
                    currentChild.next = root.left;
                    currentChild = currentChild.next;
                }
                if(root.right != null) {
                    currentChild.next = root.right;
                    currentChild = currentChild.next;
                }
                root = root.next;
            }
            root = tempChild.next;
        }
    }

    // Method 2 if(i == n - 1) continue; 比较通俗易懂
    public void connect(TreeLinkNode root) {
        Queue<TreeLinkNode> q = new LinkedList<>();
        if (root == null) return;
        q.offer(root);
        while(!q.isEmpty()){
            int n = q.size();
            TreeLinkNode node = q.poll();
            for (int i = 0; i < n; i++){
                if(node.left != null) q.add(node.left);
                if(node.right != null) q.add(node.right);
                if(i == n - 1) continue;
                node.next = q.poll();
                node = node.next;
            }
        }
    }
}


text:
module CMSScanner
  module Finders
    module InterestingFindings
      # SearchReplaceDB2 finder
      class SearchReplaceDB2 < Finder
        # @return [ InterestingFinding ]
        def aggressive(_opts = {})
          path = 'searchreplacedb2.php'

          return unless /by interconnect/i.match?(target.head_and_get(path).body)

          NS::Model::SearchReplaceDB2.new(target.url(path), confidence: 100, found_by: found_by)
        end
      end
    end
  end
end


text:
"""Stub version of the Conversion API."""

import os

from google.appengine.api import apiproxy_stub
from google.appengine.api import conversion
from google.appengine.api.conversion import conversion_service_pb
from google.appengine.runtime import apiproxy_errors

__all__ = ["ConversionServiceStub"]

_STATIC_FILES_DIR = os.path.join(
    os.path.dirname(os.path.abspath(__file__)), "static")

_CONVERTED_FILES_STUB = {
    "application/pdf": "test.pdf",
    "image/png": "test.png",
    "text/html": "test.html",
    "text/plain": "test.txt",
    }

_SUPPORTED_PATHS = {
    "application/pdf": ("image/png", "text/html", "text/plain"),
    "image/bmp": ("application/pdf", "text/html", "text/plain"),
    "image/gif": ("application/pdf", "text/html", "text/plain"),
    "image/jpeg": ("application/pdf", "text/html", "text/plain"),
    "image/png": ("application/pdf", "text/html", "text/plain"),
    "text/html": ("application/pdf", "image/png", "text/plain"),
    "text/plain": ("application/pdf", "image/png", "text/html"),
    }

def _validate_conversion_request(request):
  """Validates ConversionRequest and throws ConversionServiceError if invalid.

  Args:
    request: A conversion request.

  Raises:
    Application error if the conversion request is invalid.
  """
  if not request.IsInitialized():
    raise apiproxy_errors.ApplicationError(
        conversion_service_pb.ConversionServiceError.INVALID_REQUEST,
        "The conversion request is not initialized correctly")

  if not request.conversion_list():
    raise apiproxy_errors.ApplicationError(
        conversion_service_pb.ConversionServiceError.INVALID_REQUEST,
        "At least one conversion is required in the request")

  if request.conversion_size() > conversion.CONVERSION_MAX_NUM_PER_REQUEST:
    raise apiproxy_errors.ApplicationError(
        conversion_service_pb.ConversionServiceError.TOO_MANY_CONVERSIONS,
        "At most ten conversions are allowed in the request")

  for x in range(0, request.conversion_size()):
    if (request.conversion(x).ByteSize() >
        conversion.CONVERSION_MAX_SIZE_BYTES):
      raise apiproxy_errors.ApplicationError(
          conversion_service_pb.ConversionServiceError.CONVERSION_TOO_LARGE,
          "Each conversion should not be over %d bytes" %
          (conversion.CONVERSION_MAX_SIZE_BYTES))

    if not request.conversion(x).input().asset_list():
      raise apiproxy_errors.ApplicationError(
          conversion_service_pb.ConversionServiceError.INVALID_REQUEST,
          "At least one asset is required in input document")

    for y in range(0, request.conversion(x).input().asset_size()):
      input_asset = request.conversion(x).input().asset(y)
      if not input_asset.has_data():
        raise apiproxy_errors.ApplicationError(
            conversion_service_pb.ConversionServiceError.INVALID_REQUEST,
            "Asset data field must be set in input document")
      if not input_asset.has_mime_type():
        raise apiproxy_errors.ApplicationError(
            conversion_service_pb.ConversionServiceError.INVALID_REQUEST,
            "Asset mime type field must be set in input document")

    input_mime_type = request.conversion(x).input().asset(0).mime_type()
    output_mime_type = request.conversion(x).output_mime_type()
    if not _is_supported(input_mime_type, output_mime_type):
      raise apiproxy_errors.ApplicationError(
          conversion_service_pb.ConversionServiceError.UNSUPPORTED_CONVERSION,
          "Conversion from %s to %s is not supported" %
          (input_mime_type, output_mime_type))

def _is_supported(input_mime_type, output_mime_type):
  """Whether the conversion path is supported."""
  return (input_mime_type in _SUPPORTED_PATHS and
          output_mime_type in _SUPPORTED_PATHS[input_mime_type])

class ConversionServiceStub(apiproxy_stub.APIProxyStub):
  """A stub for the ConversionService API for offline development.

  Provides stub functions which allow a developer to test integration before
  deployment.

  Since there's no obvious way to simulate the conversion service, we bundle
  "canonical" files under the static subdirectory for each target
  conversion type and return them all the time regardless of the input.
  For each conversion, we only return one asset here.
  """

  def __init__(self, service_name="conversion"):
    """Constructor."""
    apiproxy_stub.APIProxyStub.__init__(
        self, service_name,
        conversion.CONVERSION_MAX_SIZE_BYTES *
        conversion.CONVERSION_MAX_NUM_PER_REQUEST)

  def _Dynamic_Convert(self, request, response):
    _validate_conversion_request(request)

    for x in range(0, request.conversion_size()):
      result = response.add_result()
      result.set_error_code(conversion_service_pb.ConversionServiceError.OK)
      output_mime_type = request.conversion(x).output_mime_type()
      output_asset = result.mutable_output().add_asset()
      output_asset.set_mime_type(output_mime_type)
      output_asset.set_data(open(os.path.join(
          _STATIC_FILES_DIR,
          _CONVERTED_FILES_STUB[output_mime_type]), "r").read())
      output_asset.set_name(_CONVERTED_FILES_STUB[output_mime_type])


text:
<?php

namespace Symbiose\Component\Searching;

use Zend\Search\Lucene\Lucene,
	Symbiose\Component\Searching\Exception\SearchException as Exception
;

class Search
{
	protected $file;
	protected $index;

	public function __construct($file)
	{
		$this->file = $file;
		if(!file_exists($file)) {
			$this->index = Lucene::create($file);
		}
		else {
			$this->index = Lucene::open($file);
		}
	}

	public function __call($name, $arguments) {
        if($arguments) {
			$argNumber = count($arguments);
			switch($argNumber) {
				case 0:
					return $this->index->$name();
				break;
				case 1:
					return $this->index->$name($arguments[0]);
				break;
				case 2:
					return $this->index->$name($arguments[0], $arguments[1]);
				break;
			}
        }
        else {
        	return $this->index->$name();
        }
    }
}


text:
using System;
using System.Collections.Generic;
using Framework.Domain;

namespace Framework
{
    public interface IForeignKeyMappingManager
    {
        void RegisterForeignKeyMapping<TParentEntity, TChildEntity>(
            IForeignKeyMapping<TParentEntity, TChildEntity> mapping);

        IList<TChildEntity> GetForeignKeyValues<TParentEntity, TChildEntity>(TParentEntity entity);
    }

    public class ForeignKeyMappingManager : IForeignKeyMappingManager
    {
        static IForeignKeyMappingManager s_instance = new ForeignKeyMappingManager();

        IDictionary<string, IDictionary<string, object>> _foreignKeMapping = new Dictionary<string, IDictionary<string, object>>();

        private ForeignKeyMappingManager()
        {
        }

        public static IForeignKeyMappingManager GetInstance()
        {
            return s_instance;
        }

        public void RegisterForeignKeyMapping<TParentEntity, TChildEntity>(
            IForeignKeyMapping<TParentEntity, TChildEntity> mapping)
        {
            string parentName = typeof(TParentEntity).FullName;

            if (!_foreignKeMapping.ContainsKey(parentName))
                _foreignKeMapping.Add(parentName, new Dictionary<string, object>());

            IDictionary<string, object> maps = _foreignKeMapping[parentName];
            string childName = typeof(TChildEntity).FullName;

            if (!maps.ContainsKey(childName))
                maps.Add(childName, mapping);
        }

        public IList<TChildEntity> GetForeignKeyValues<TParentEntity, TChildEntity>(TParentEntity entity)
        {
            string parentName = typeof(TParentEntity).FullName;

            if (!_foreignKeMapping.ContainsKey(parentName))
                throw new ArgumentException(string.Format("Foreign key mapping associated to parent type '{0}' does not exists", parentName));

            IDictionary<string, object> maps = _foreignKeMapping[parentName];
            string childName = typeof(TChildEntity).FullName;

            if (!maps.ContainsKey(childName))
                throw new ArgumentException(string.Format("Foreign key mapping associated to child type '{0}' does not exists", childName));

            IForeignKeyMapping<TParentEntity, TChildEntity> fkMapping = (IForeignKeyMapping<TParentEntity, TChildEntity>)maps[childName];

            return fkMapping.GetChildEntities(entity);
        }
    }
}


text:
// Generated from com/linkedin/restli/client/config/RequestConfigKey.g4 by ANTLR 4.5
package com.linkedin.restli.client.config;

import org.antlr.v4.runtime.ParserRuleContext;
import org.antlr.v4.runtime.misc.NotNull;
import org.antlr.v4.runtime.tree.ErrorNode;
import org.antlr.v4.runtime.tree.TerminalNode;

/**
 * This class provides an empty implementation of {@link RequestConfigKeyListener},
 * which can be extended to create a listener which only needs to handle a subset
 * of the available methods.
 */
public class RequestConfigKeyBaseListener implements RequestConfigKeyListener {
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterKey(RequestConfigKeyParser.KeyContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitKey(RequestConfigKeyParser.KeyContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterInbound(RequestConfigKeyParser.InboundContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitInbound(RequestConfigKeyParser.InboundContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterOutbound(RequestConfigKeyParser.OutboundContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitOutbound(RequestConfigKeyParser.OutboundContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterRestResource(RequestConfigKeyParser.RestResourceContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitRestResource(RequestConfigKeyParser.RestResourceContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterOperationIn(RequestConfigKeyParser.OperationInContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitOperationIn(RequestConfigKeyParser.OperationInContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterOperationOut(RequestConfigKeyParser.OperationOutContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitOperationOut(RequestConfigKeyParser.OperationOutContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterSimpleOp(RequestConfigKeyParser.SimpleOpContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitSimpleOp(RequestConfigKeyParser.SimpleOpContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterHttpExtraOp(RequestConfigKeyParser.HttpExtraOpContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitHttpExtraOp(RequestConfigKeyParser.HttpExtraOpContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterComplex(RequestConfigKeyParser.ComplexContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitComplex(RequestConfigKeyParser.ComplexContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterComplexOp(RequestConfigKeyParser.ComplexOpContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitComplexOp(RequestConfigKeyParser.ComplexOpContext ctx) { }

	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void enterEveryRule(ParserRuleContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void exitEveryRule(ParserRuleContext ctx) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void visitTerminal(TerminalNode node) { }
	/**
	 * {@inheritDoc}
	 *
	 *
The default implementation does nothing.

	 */
	@Override public void visitErrorNode(ErrorNode node) { }
}


text:
from __future__ import print_function, division
import sys
import math
import pickle
from random import shuffle
from collections import defaultdict

import glog
from iterator import MultiInstanceIterator, MultiInstanceFoldIterator
from evaluator import multi_instance_predict
from learner import LogisticRegressionLearner as LogReg
from sklearn.metrics import precision_recall_curve
from sklearn.metrics import average_precision_score
from sklearn.preprocessing import label_binarize

class MultiInstanceLearner(object):
    def __init__(self, name, relation_name, iter_train, fold=4):
        assert fold > 1

        self.name = name
        # The positive relation label.
        self.relation_name = relation_name
        # A base multi-class learner for all relations and N binary
        # learner for each relation.
        self.multi_class_learner = None
        self.binary_learner = None

        # User should define the iterator for training data.
        self.multi_iter_train = iter_train
        self.fold = fold
        self.fold_iter = MultiInstanceFoldIterator('train', iter_train, fold)
        self.fold_iter.load_fold()
        self.e_step_result = None

    def create_learner(self):
        self.binary_learner = LogReg(self.name)
        self.multi_class_learner = []
        for i in range(self.fold):
            # Avoid train and test on the same data in the EM process.
            self.multi_class_learner.append(LogReg('MULTICLASS_{}'.format(i)))

    def init_multiclass_classifier(self):
        msg = 'Initializing {} multiclass classifiers...'.format(self.fold)
        glog.info(msg)

        for i in range(self.fold):
            fold_examples = []
            fold_labels = []
            for instances, labels in \
                    self.fold_iter.iter_other_fold_as_group_relation_pair(i):
                for instance in instances:
                    fold_examples.append(instance.features)
                    fold_labels.append(labels[0])

            # 20 iterations.
            self.multi_class_learner[i].learn(fold_examples, fold_labels, 50)

        msg = 'Initialized {} multiclass classifiers'.format(self.fold)
        glog.info(msg)

    # MIML-RE version
    def init_binary_classifier(self):
        msg = 'Initializing binary classifier'
        glog.info(msg)

        # We initialize the binary classifiers such that a relation
        # has a high weight for itself. Since other relation appearing
        # including the NONE relation doesn't neccessary mean the
        # entity pair has no current relation.
        examples = [[self.relation_name], ['NONE']]

        # 20 iterations.
        self.binary_learner.learn(examples, [self.relation_name, 'NONE'], 20)
        # probs = learner.predict_prob(examples)
        # glog.info('{} {}'.format(examples, probs))

    # Riedel version
    # def init_binary_classifier(self):
    #     examples, labels = [], []
    #     for instance, label in self.multi_iter_train.iter_as_instance_relation_pair():
    #         examples.append(instance.features)
    #         if label is not None:
    #             labels.append(label.name)
    #         else:
    #             labels.append('NONE')
    #
    #     for relation, learner in self.binary_learners.items():
    #         msg = 'Initializing binary classifier {}...'.format(relation.name)
    #         glog.info(msg)
    #
    #         # 20 iterations.
    #         learner.learn(examples, labels, 20)
    #         # probs = learner.predict_prob(examples)
    #         # glog.info('{} {}'.format(examples, probs))
    #
    #     msg = 'Initialized {} binary classifiers'.format(
    #         len(self.binary_learners))
    #     glog.info(msg)

    def e_step(self, fold):
        glog.info('Fold {}, running E step...'.format(fold))
        e_step_result = []
        z_changed = defaultdict(int)
        high_conf = 0

        for instances, group_relations in \
                self.fold_iter.iter_fold_as_group_relation_pair(fold):
            examples = [instance.features for instance in instances]

            # Randomize the instances before doing the E step, since the z
            # labels depend on each other. We don't want to introduce
            # some bias due to the specific iteration order of the instances.
            shuffle(examples)

            z_values = self.multi_class_learner[fold].predict(examples)
            z_probs = self.multi_class_learner[fold].predict_prob(examples)
            assert len(z_values) == len(z_probs)

            for v, p in zip(z_values, z_probs):
                for vv, pp in p:
                    if vv == v:
                        if pp > 0.9:
                            high_conf += 1
                        break

            for i, example in enumerate(examples):
                z_joint_probs = []
                z_label_probs = z_probs[i]

                for mention_label, z_prob in z_label_probs:
                    curr = z_values[:i] + [mention_label] + z_values[i + 1:]
                    z_joint_prob = math.log(z_prob)

                    # This is MIML-RE features.
                    # Get features for all binary classifiers, following
                    # the at-least-one assumption.
                    features = set(curr)
                    if len(features) > 1:
                        # At-least-one assumption. If there is a positive
                        # z-label, then remove the NONE relation prediction.
                        features.discard('NONE')
                    features = list(features)

                    # This is Riedel features.
                    # features = []
                    # for j, curr_label in enumerate(curr):
                    #     if curr_label == 'NONE':
                    #         continue
                    #     # Only use features in active instances.
                    #     features += examples[j]

                    # Adding probability for all the positive and negative
                    # relations for joint inference.

                    assert len(group_relations) == 1
                    if len(group_relations) > 0:
                        gold = group_relations[0]
                    else:
                        gold = 'NONE'

                    y_label_probs = self.binary_learner.predict_one_prob(features)
                    y_matched = False
                    for y, y_prob in y_label_probs:
                        if y == gold:
                            if y_prob > 0:
                                z_joint_prob += math.log(y_prob)
                            else:
                                z_joint_prob += float('-inf')
                            y_matched = True
                            break
                    assert y_matched

                    z_joint_probs.append((mention_label, z_joint_prob))

                # Get the best z label based on joint inference.
                best_z_prob = max(z_joint_probs, key=lambda a: a[1])

                if z_values[i] != best_z_prob[0]:
                    change = '{} -> {}'.format(z_values[i], best_z_prob[0])
                    z_changed[change] += 1

                z_values[i] = best_z_prob[0]
            # Add the inferred z labels for the M step.
            e_step_result.append((examples, z_values, group_relations))

        self.e_step_result.append(e_step_result)
        # During E step, some z labels will change due to the joint inference.
        glog.info('Mention label changes: {}'.format(z_changed))
        glog.info('>0.9 instances: {}'.format(high_conf))

    def m_step(self, fold):
        glog.info('Fold {}, running M step...'.format(fold))
        multi_class_data = []
        multi_class_label = []
        binary_class_data = []
        binary_class_label = []

        e_step_result = []
        instance_count = 0
        label_count = 0
        for i in range(self.fold):
            if i == fold:
                continue
            for eres in self.e_step_result[i]:
                e_step_result.append(eres)
                instance_count += len(eres[0])
                label_count += len(eres[1])
                assert instance_count == label_count, (
                    instance_count, label_count)

        for examples, z_values, group_relations in e_step_result:
            multi_class_data += examples
            multi_class_label += z_values

            y_features = list(set(z_values))
            binary_class_data.append(y_features)
            if len(group_relations) == 0:
                binary_class_label.append('NONE')
            else:
                assert len(group_relations) == 1
                binary_class_label.append(group_relations[0])

        # Train multi-class classifier.
        self.multi_class_learner[fold].learn(multi_class_data,
                                             multi_class_label, 50)

        # Train binary classifiers.
        self.binary_learner.learn(binary_class_data,
                                  binary_class_label, 10)

    def learn(self, epoch=10):
        for i in range(epoch):
            glog.info('Epoch {}'.format(i))
            self.e_step_result = []
            for f in range(self.fold):
                self.e_step(f)
            for f in range(self.fold):
                self.m_step(f)

if __name__ == '__main__':
    glog.setLevel(glog.DEBUG)
    train_file = sys.argv[1]
    test_file = sys.argv[2]
    eval_file = sys.argv[3]
    # Used for training the binary classifier at entity pair level.
    relation_name = sys.argv[4]

    # Iterator for training data.
    iter_train = MultiInstanceIterator('train')
    iter_train.base_iter.set_min_feature_freq(2)

    iter_train.base_iter.set_feature_set('miml')
    iter_train.base_iter.log_setting()

    iter_train.base_iter.load_instance(train_file)
    iter_train.load_as_training_data(shuffle_entity_pair=True)

    # Iterator for test set. We don't set group limit
    # since this may be some human annotated corpus, so
    # that we should evaluate all instances.
    iter_test = MultiInstanceIterator('test')
    iter_test.base_iter.set_feature_set('miml')

    # Load data.
    iter_test.base_iter.load_instance(test_file)
    iter_test.load_as_test_data()

    # Create the learner.
    milearner = MultiInstanceLearner('train', relation_name, iter_train, fold=4)

    # Init.
    milearner.create_learner()
    milearner.init_multiclass_classifier()
    milearner.init_binary_classifier()

    # Training for 5 epochs.
    milearner.learn(5)

    examples = []
    gold = []
    for instance, gold_label in iter_test.iter_as_instance_relation_pair():
        examples.append(instance.features)
        gold.append(gold_label)

    predictions, probs = multi_instance_predict(
        milearner.multi_class_learner, examples)

    new_probs = []
    for prec, prob in zip(predictions, probs):
        if prec == 'NONE':
            new_probs.append(1-prob)
        else:
            new_probs.append(prob)

    # This will be a column vector where 1=REL and 0=NONE
    # new_prob will be a column vvector where element are prob of REL.
    gold_bin = label_binarize(gold, milearner.multi_class_learner[0].model.classes_)
    precision, recall, thresholds, average_precision = {}, {}, {}, {}
    precision[0], recall[0], thresholds[0] = precision_recall_curve(gold_bin, new_probs)
    average_precision[0] = average_precision_score(gold_bin, new_probs)
    with open(eval_file, 'wb') as f:
        pickle.dump((precision, recall, average_precision, thresholds), f)


text:
// Copyright (c) Microsoft Open Technologies, Inc. All rights reserved. See License.txt in the project root for license information.

namespace System.Web.OData.Builder
{
    internal class OperationTitleAnnotation
    {
        public OperationTitleAnnotation(string title)
        {
            Title = title;
        }

        public string Title { get; private set; }
    }
}


text:
''' main.py '''
import argparse
import os
import shutil
import sys
import traceback

import heron.common.src.python.utils.log as log
import heron.tools.common.src.python.utils.config as config
import heron.tools.cli.src.python.result as result
import heron.tools.admin.src.python.standalone as standalone

Log = log.Log

HELP_EPILOG = '''Getting more help:
  heron help <command> Prints help and options for <command>

For detailed documentation, go to https://heron.incubator.apache.org'''

# pylint: disable=protected-access,superfluous-parens
class _HelpAction(argparse._HelpAction):
  def __call__(self, parser, namespace, values, option_string=None):
    parser.print_help()

    # retrieve subparsers from parser
    subparsers_actions = [
        action for action in parser._actions
        if isinstance(action, argparse._SubParsersAction)
    ]

    # there will probably only be one subparser_action,
    # but better save than sorry
    for subparsers_action in subparsers_actions:
      # get all subparsers and print help
      for choice, subparser in list(subparsers_action.choices.items()):
        print("Subparser '{}'".format(choice))
        print(subparser.format_help())
        return

################################################################################
def get_command_handlers():
  '''
  Create a map of command names and handlers
  '''
  return {
      'standalone': standalone,
  }

################################################################################
def create_parser(command_handlers):
  '''
  Main parser
  :return:
  '''
  parser = argparse.ArgumentParser(
      prog='heron',
      epilog=HELP_EPILOG,
      formatter_class=config.SubcommandHelpFormatter,
      add_help=True)

  subparsers = parser.add_subparsers(
      title="Available commands",
      metavar='<command> <options>')

  command_list = sorted(command_handlers.items())
  for command in command_list:
    command[1].create_parser(subparsers)

  return parser

################################################################################
def run(handlers, command, parser, command_args, unknown_args):
  '''
  Run the command
  :param command:
  :param parser:
  :param command_args:
  :param unknown_args:
  :return:
  '''

  if command in handlers:
    return handlers[command].run(command, parser, command_args, unknown_args)
  err_context = 'Unknown subcommand: %s' % command
  return result.SimpleResult(result.Status.InvocationError, err_context)

def cleanup(files):
  '''
  :param files:
  :return:
  '''
  for cur_file in files:
    if os.path.isdir(cur_file):
      shutil.rmtree(cur_file)
    else:
      shutil.rmtree(os.path.dirname(cur_file))

################################################################################
def check_environment():
  '''
  Check whether the environment variables are set
  :return:
  '''

  if not config.check_release_file_exists():
    sys.exit(1)

################################################################################
def execute(handlers):
  '''
  Run the command
  :return:
  '''
  # verify if the environment variables are correctly set
  check_environment()

  # create the argument parser
  parser = create_parser(handlers)

  # if no argument is provided, print help and exit
  if len(sys.argv[1:]) == 0:
    parser.print_help()
    return 0

  # insert the boolean values for some of the options
  sys.argv = config.insert_bool_values(sys.argv)

  try:
    # parse the args
    args, unknown_args = parser.parse_known_args()
  except ValueError as ex:
    Log.error("Error while parsing arguments: %s", str(ex))
    Log.debug(traceback.format_exc())
    sys.exit(1)

  command_line_args = vars(args)

  # set log level
  log.set_logging_level(command_line_args)
  Log.debug("Input Command Line Args: %s", command_line_args)

  # command to be execute
  command = command_line_args['subcommand']

  # print the input parameters, if verbose is enabled
  Log.debug("Processed Command Line Args: %s", command_line_args)

  results = run(handlers, command, parser, command_line_args, unknown_args)

  return 0 if result.is_successful(results) else 1

def main():
  # Create a map of supported commands and handlers
  command_handlers = get_command_handlers()
  # Execute
  return execute(command_handlers)

if __name__ == "__main__":
  sys.exit(main())


text:
<?php
namespace TribalScents\Sniffs\PHP;

use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Standards\Generic\Sniffs\PHP;

/**
 * TribalScents_Sniffs_PHP_DeprecatedFunctionsSniff.
 *
 * Discourages the use of deprecated WordPress functions that are kept in PHP for
 * compatibility with older versions.
 *
 * @category  PHP
 * @package   PHP_CodeSniffer
 * @author    Matthew Batchelder <borkweb@gmail.com>
 * @author    Zachary Tirrell <zbtirrell@gmail.com>
 * @copyright 2012 The Events Calendar
 * @license   https://github.com/the-events-calendar/TribalScents/blob/master/licence.txt BSD Licence
 * @version   Release: 1.4.0
 * @link      http://pear.php.net/package/PHP_CodeSniffer
 */
class DeprecatedFunctionsSniff extends PHP\ForbiddenFunctionsSniff
{

    /**
     * A list of forbidden functions with their alternatives.
     *
     * The value is NULL if no alternative exists. IE, the
     * function should just not be used.
     *
     * @var array(string => string|null)
     */
		public $forbiddenFunctions = array(
			"get_postdata"                          => "get_post()",
			"start_wp"                              => "The Loop - {@link http://codex.wordpress.org/The_Loop Use new WordPress Loop}",
			"the_category_ID"                       => "get_the_category()",
			"the_category_head"                     => "get_the_category_by_ID()",
			"previous_post"                         => "previous_post_link()",
			"next_post"                             => "next_post_link()",
			"user_can_create_post"                  => "current_user_can()",
			"user_can_create_draft"                 => "current_user_can()",
			"user_can_edit_post"                    => "current_user_can()",
			"user_can_delete_post"                  => "current_user_can()",
			"user_can_set_post_date"                => "current_user_can()",
			"user_can_edit_post_date"               => "current_user_can()",
			"user_can_edit_post_comments"           => "current_user_can()",
			"user_can_delete_post_comments"         => "current_user_can()",
			"user_can_edit_user"                    => "current_user_can()",
			"get_linksbyname"                       => "get_bookmarks()",
			"wp_get_linksbyname"                    => "wp_list_bookmarks()",
			"get_linkobjectsbyname"                 => "get_bookmarks()",
			"get_linkobjects"                       => "get_bookmarks()",
			"get_linksbyname_withrating"            => "get_bookmarks()",
			"get_links_withrating"                  => "get_bookmarks()",
			"get_autotoggle"                        => null,
			"list_cats"                             => "wp_list_categories()",
			"wp_list_cats"                          => "wp_list_categories()",
			"dropdown_cats"                         => "wp_dropdown_categories()",
			"list_authors"                          => "wp_list_authors()",
			"wp_get_post_cats"                      => "wp_get_post_categories()",
			"wp_set_post_cats"                      => "wp_set_post_categories()",
			"get_archives"                          => "wp_get_archives()",
			"get_author_link"                       => "get_author_posts_url()",
			"link_pages"                            => "wp_link_pages()",
			"get_settings"                          => "get_option()",
			"permalink_link"                        => "the_permalink()",
			"permalink_single_rss"                  => "the_permalink_rss()",
			"wp_get_links"                          => "wp_list_bookmarks()",
			"get_links"                             => "get_bookmarks()",
			"get_links_list"                        => "wp_list_bookmarks()",
			"links_popup_script"                    => null,
			"get_linkrating"                        => "sanitize_bookmark_field()",
			"get_linkcatname"                       => "get_category()",
			"comments_rss_link"                     => "post_comments_feed_link()",
			"get_category_rss_link"                 => "get_category_feed_link()",
			"get_author_rss_link"                   => "get_author_feed_link()",
			"comments_rss"                          => "get_post_comments_feed_link()",
			"create_user"                           => "wp_create_user()",
			"gzip_compression"                      => null,
			"get_commentdata"                       => "get_comment()",
			"get_catname"                           => "get_cat_name()",
			"get_category_children"                 => "get_term_children()",
			"get_the_author_description"            => "get_the_author_meta('description')",
			"the_author_description"                => "the_author_meta('description')",
			"get_the_author_login"                  => "get_the_author_meta('login')",
			"the_author_login"                      => "the_author_meta('login')",
			"get_the_author_firstname"              => "get_the_author_meta('first_name')",
			"the_author_firstname"                  => "the_author_meta('first_name')",
			"get_the_author_lastname"               => "get_the_author_meta('last_name')",
			"the_author_lastname"                   => "the_author_meta('last_name')",
			"get_the_author_nickname"               => "get_the_author_meta('nickname')",
			"the_author_nickname"                   => "the_author_meta('nickname')",
			"get_the_author_email"                  => "get_the_author_meta('email')",
			"the_author_email"                      => "the_author_meta('email')",
			"get_the_author_icq"                    => "get_the_author_meta('icq')",
			"the_author_icq"                        => "the_author_meta('icq')",
			"get_the_author_yim"                    => "get_the_author_meta('yim')",
			"the_author_yim"                        => "the_author_meta('yim')",
			"get_the_author_msn"                    => "get_the_author_meta('msn')",
			"the_author_msn"                        => "the_author_meta('msn')",
			"get_the_author_aim"                    => "get_the_author_meta('aim')",
			"the_author_aim"                        => "the_author_meta('aim')",
			"get_author_name"                       => "get_the_author_meta('display_name')",
			"get_the_author_url"                    => "get_the_author_meta('url')",
			"the_author_url"                        => "the_author_meta('url')",
			"get_the_author_ID"                     => "get_the_author_meta('ID')",
			"the_author_ID"                         => "the_author_meta('ID')",
			"the_content_rss"                       => "the_content_feed()",
			"make_url_footnote"                     => null,
			"_c"                                    => "_x()",
			"translate_with_context"                => "_x()",
			"_nc"                                   => "_nx()",
			"__ngettext"                            => "_n()",
			"__ngettext_noop"                       => "_n_noop()",
			"get_alloptions"                        => "wp_load_alloptions())",
			"get_the_attachment_link"               => "wp_get_attachment_link()",
			"get_attachment_icon_src"               => "wp_get_attachment_image_src()",
			"get_attachment_icon"                   => "wp_get_attachment_image()",
			"get_attachment_innerHTML"              => "wp_get_attachment_image()",
			"get_link"                              => "get_bookmark()",
			"sanitize_url"                          => "esc_url_raw()",
			"clean_url"                             => "esc_url()",
			"js_escape"                             => "esc_js()",
			"wp_specialchars"                       => "esc_html()",
			"attribute_escape"                      => "esc_attr()",
			"register_sidebar_widget"               => "wp_register_sidebar_widget()",
			"unregister_sidebar_widget"             => "wp_unregister_sidebar_widget()",
			"register_widget_control"               => "wp_register_widget_control()",
			"unregister_widget_control"             => "wp_unregister_widget_control()",
			"delete_usermeta"                       => "delete_user_meta()",
			"get_usermeta"                          => "get_user_meta()",
			"update_usermeta"                       => "update_user_meta()",
			"get_users_of_blog"                     => null,
			"automatic_feed_links"                  => "add_theme_support( 'automatic-feed-links' )",
			"get_profile"                           => "get_the_author_meta()",
			"get_usernumposts"                      => "count_user_posts()",
			"funky_javascript_callback"             => null,
			"funky_javascript_fix"                  => null,
			"is_taxonomy"                           => "taxonomy_exists()",
			"is_term"                               => "term_exists()",
			"is_plugin_page"                        => "global \$plugin_page and/or get_plugin_page_hookname() hooks.",
			"update_category_cache"                 => null,
			"wp_timezone_supported"                 => null,
			"the_editor"                            => "wp_editor()",
			"get_user_metavalues"                   => null,
			"sanitize_user_object"                  => null,
			"get_boundary_post_rel_link"            => null,
			"start_post_rel_link"                   => null,
			"get_index_rel_link"                    => null,
			"index_rel_link"                        => null,
			"get_parent_post_rel_link"              => null,
			"parent_post_rel_link"                  => null,
			"wp_admin_bar_dashboard_view_site_menu" => null,
			"is_blog_user"                          => "is_user_member_of_blog()",
			"debug_fopen"                           => "error_log()",
			"debug_fwrite"                          => "error_log() instead.",
			"debug_fclose"                          => "error_log()",
			"get_themes"                            => "wp_get_themes()",
			"get_theme"                             => "wp_get_theme()",
			"get_current_theme"                     => "(string) wp_get_theme()",
			"clean_pre"                             => null,
			"add_custom_image_header"               => "add_theme_support('custom-header', \$args)",
			"remove_custom_image_header"            => "remove_theme_support('custom-header')",
			"add_custom_background"                 => "add_theme_support('custom-background, \$args)",
			"remove_custom_background"              => null,
			"get_theme_data"                        => "wp_get_theme()",
			"update_page_cache"                     => null,
			"clean_page_cache"                      => null,
			"wp_explain_nonce"                      => "wp_nonce_ays()",
			"sticky_class"                          => "post_class()",
			"_get_post_ancestors"                   => null,
			"wp_load_image"                         => null,
			"image_resize"                          => null,
			"wp_get_single_post"                    => null,
			"user_pass_ok"                          => "wp_authenticate()",
			"_save_post_hook"                       => null,
			"gd_edit_image_support"                 => null,
		);

    /**
     * Constructor.
     *
     * Uses the Reflection API to get a list of deprecated functions.
     */
    public function __construct()
    {
    }//end __construct()

    /**
     * Generates the error or wanrning for this sniff.
     *
     * @param File   $phpcsFile The file being scanned.
     * @param int    $stackPtr  The position of the forbidden function
     *                          in the token array.
     * @param string $function  The name of the forbidden function.
     * @param string $pattern   The pattern used for the match.
     *
     * @return void
     */
    protected function addError($phpcsFile, $stackPtr, $function, $pattern=null)
    {
        $data  = array($function);
				$error = 'WordPress Function %s() has been deprecated.';

				if ( $this->forbiddenFunctions[ $function ] ) {
					$error .= ' Use ' . $this->forbiddenFunctions[ $function ] . ' instead.';
				}//end if

        $type  = 'Deprecated';

        if ($this->error === true) {
            $phpcsFile->addError($error, $stackPtr, $type, $data);
        } else {
            $phpcsFile->addWarning($error, $stackPtr, $type, $data);
        }

    }//end addError()

}//end class


text:
package org.tolweb.treegrow.tree;

import java.awt.*;
import java.util.*;
import java.awt.event.*;
import javax.swing.*;
import layout.TableLayout;
import layout.TableLayoutConstants;

import org.tolweb.treegrow.main.*;

/**
 * JFrame that allows the user to enter the text they'd like to search for
 * in the tree window. They should be able to search for successive nodes
 * with a name containing the string they enter
 */
public class FindNodeFrame extends JDialog {
    /**
	 *
	 */
	private static final long serialVersionUID = -2679513434518027342L;
	private JTextField patternField;
    private JButton findButton;
    private int findCounter = 1;
    private TreePanel treePanel;

    /** Creates a new instance of FindNodeFrame */
    public FindNodeFrame() {
        super(Controller.getController().getTreeEditor(), "Find node in this window", false);

        treePanel = TreePanel.getTreePanel();

        Container contentPane = getContentPane();
        setResizable(false);
        setSize(250,100);

        double [][] size = new double[][] {{10, TableLayoutConstants.FILL, 10}, {10, 20, 10, 30, 10}};
        contentPane.setLayout(new TableLayout(size));

        patternField = new JTextField("");
        contentPane.add(patternField, "1,1");

        contentPane.add(buildButtonPanel(), "1,3");
    }

    private JPanel buildButtonPanel() {
        JPanel buttonPanel = new JPanel();
        findButton = new JButton("Find");
        findButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                find();
            }
        });

        JButton closeButton = new JButton("Close");
        closeButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                setVisible(false);
                dispose();
            }
        });
        if (Controller.getController().isMac()) {
            buttonPanel.add(closeButton);
            buttonPanel.add(findButton);
        } else {
            buttonPanel.add(findButton);
            buttonPanel.add(closeButton);
        }

        return buttonPanel;
    }

    private void find() {
        //Not particularly efficient...but it doesn't seem to matter
        ArrayList matches = treePanel.getTree().findNodesPartialName(patternField.getText()) ;
        int matchSize = matches.size();
        Node foundNode = null;

        while (foundNode == null) {
            if (matchSize < findCounter) {
                break;
            }
            foundNode = (Node)(matches.get(findCounter-1));
            if (!foundNode.getCheckedOut()) {
                findCounter++;
                foundNode = null;
            }
        }

        if (foundNode != null) {
            Controller.getController().getTreeEditor().toFront();
            NodeView view = treePanel.bringNodeToViewPort(foundNode);
            view.getTextField().requestFocus();
            findCounter++;
            findButton.setText("Find Again");
        } else {
            String noMatch = (findCounter==1 ? "No match found" : "No more matches");
            JOptionPane.showMessageDialog(
                      this,
                      noMatch,
                      "No Matches",
                      JOptionPane.INFORMATION_MESSAGE);
            findCounter = 1;
            findButton.setText("Find");
        }
        validate();
        repaint();

    }

}


text:
package fpinscala.streamingio

import fpinscala.iomonad.{IO,Monad,Free,unsafePerformIO}

object ImperativeAndLazyIO {

                            /*

  We are going to consider various approaches to the simple task of
  checking whether a file contains more than 40,000 lines.

  Our first implementation is an imperative implementation, embedded
  into `IO`.
                             */

  import java.io._

  def linesGt40k(filename: String): IO[Boolean] = IO {
    // There are a number of convenience functions in scala.io.Source
    // for reading from external sources such as files.
    val src = io.Source.fromFile(filename)
    try {
      var count = 0
      // Obtain a stateful iterator from the Source
      val lines: Iterator[String] = src.getLines
      while (count <= 40000 && lines.hasNext) {
        lines.next // has side effect of advancing to next element
        count += 1
      }
      count > 40000
    }
    finally src.close
  }

                            /*

  The above code is rather low-level, and it's not compositional,
  either. Consider the following scenarios:

  * Check whether the number of _nonempty_ lines in the file exceeds
    40,000
  * Find a line index before 40,000 where the first letter of
    consecutive lines spells out `"abracadabra"`.

  We cannot just compose our existing implementation with some
  other combinator(s) to implement these tasks. Our implementation is
  a monolithic loop, and we must modify this loop directly if we want
  to change its behavior.

  Now imagine if we had a `Stream[String]` for the lines of the file
  and we could assemble functionality using all the `Stream` functions
  we know and love.
                             */

  object Examples {
    val lines: Stream[String] = sys.error("defined elsewhere")
    val ex1 = lines.zipWithIndex.exists(_._2 + 1 >= 40000)
    val ex2 = lines.filter(!_.trim.isEmpty).zipWithIndex.exists(_._2 + 1 >= 40000)
    val ex3 = lines.take(40000).map(_.head).indexOfSlice("abracadabra".toList)
  }

                            /*

  Could we actually write the above? Not quite. We could 'cheat' and
  return an `IO[Stream[String]]` representing the lines of a file:

                             */

  def lines(filename: String): IO[Stream[String]] = IO {
    val src = io.Source.fromFile(filename)
    src.getLines.toStream append { src.close; Stream.empty }
  }
                            /*

  This is called _lazy I/O_, and it's problematic for a number of
  reasons, discussed in the book text. However, it would be nice to
  recover the same high-level, compositional style we are used to
  from our use of `List` and `Stream`.

                             */
}

object SimpleStreamTransducers {

                            /*

  We now introduce a type, `Process`, representing pure, single-input
  stream transducers. It can be in of three states - it can be
  emitting a value to the output (`Emit`), reading a value from its
  input (`Await`) or signaling termination via `Halt`.

                             */

  sealed trait Process[I,O] {
    import Process._

    /*
     * A `Process[I,O]` can be used to transform a `Stream[I]` to a
     * `Stream[O]`.
     */
    def apply(s: Stream[I]): Stream[O] = this match {
      case Halt() => Stream()
      case Await(recv) => s match {
        case h #:: t => recv(Some(h))(t)
        case xs => recv(None)(xs) // Stream is empty
      }
      case Emit(h,t) => h #:: t(s)
    }

    /*
     * `Process` can be thought of as a sequence of values of type `O`
     * and many of the operations that would be defined for `List[O]`
     * can be defined for `Process[I,O]`, for instance `map`, `++` and
     * `flatMap`. The definitions are analogous.
     */

    def map[O2](f: O => O2): Process[I,O2] = this match {
      case Halt() => Halt()
      case Emit(h, t) => Emit(f(h), t map f)
      case Await(recv) => Await(recv andThen (_ map f))
    }
    def ++(p: => Process[I,O]): Process[I,O] = this match {
      case Halt() => p
      case Emit(h, t) => Emit(h, t ++ p)
      case Await(recv) => Await(recv andThen (_ ++ p))
    }
    def flatMap[O2](f: O => Process[I,O2]): Process[I,O2] = this match {
      case Halt() => Halt()
      case Emit(h, t) => f(h) ++ t.flatMap(f)
      case Await(recv) => Await(recv andThen (_ flatMap f))
    }

    /*
     * Exercise 5: Implement `|>`. Let the types guide your implementation.
     */
    def |>[O2](g: Process[O,O2]): Process[I,O2] = g match {
      case Halt() => Halt() //if p2 is a halt, don't bother
      case Emit(h,t) => Emit(h, this |> t) //if p2 emits, emit the value and compose this with the tail
      case Await(f) => this match { //f is Option[I] => Process[I,O]
        case Halt() => Halt() |> f(None) //if this is a halt, pass None to f
        case Emit(h,t) => t |> f(Some(h))
        case Await(r) => Await(i  => r(i) |> g)
      }
    }

    /*
     * Feed `in` to this `Process`. Uses a tail recursive loop as long
     * as `this` is in the `Await` state.
     */
    def feed(in: Seq[I]): Process[I,O] = {
      @annotation.tailrec
      def go(in: Seq[I], cur: Process[I,O]): Process[I,O] =
        cur match {
          case Halt() => Halt()
          case Await(recv) =>
            if (in.nonEmpty) go(in.tail, recv(Some(in.head)))
            else cur
          case Emit(h, t) => Emit(h, t.feed(in))
        }
      go(in, this)
    }

    /*
     * See `Process.lift` for a typical repeating `Process`
     * definition expressed with explicit recursion.
     */

    /*
     * `Process` definitions can often be expressed without explicit
     * recursion, by repeating some simpler `Process` forever.
     */
    def repeat: Process[I,O] = {
      def go(p: Process[I,O]): Process[I,O] = p match {
        case Halt() => go(this)
        case Await(recv) => Await {
          case None => recv(None)
          case i => go(recv(i))
        }
        case Emit(h, t) => Emit(h, go(t))
      }
      go(this)
    }

    def repeatN(n: Int): Process[I,O] = {
      def go(n: Int, p: Process[I,O]): Process[I,O] = p match {
        case Halt() => if (n > 0) go(n-1, this) else Halt()
        case Await(recv) => Await {
          case None => recv(None)
          case i => go(n,recv(i))
        }
        case Emit(h, t) => Emit(h, go(n,t))
      }
      go(n, this)
    }

    /*
     * As an example of `repeat`, see `Process.filter`. We define
     * a convenience function here for composing this `Process`
     * with a `Process` that filters the output type `O`.
     */
    def filter(f: O => Boolean): Process[I,O] =
      this |> Process.filter(f)

    /*
     * Exercise 6: Implement `zipWithIndex`.
     */
    def zipWithIndex: Process[I,(O,Int)] = ???

    /* Add `p` to the fallback branch of this process */
    def orElse(p: Process[I,O]): Process[I,O] = this match {
      case Halt() => p
      case Await(recv) => Await {
        case None => p
        case x => recv(x)
      }
      case _ => this
    }
  }

  object Process {

    case class Emit[I,O](
        head: O,
        tail: Process[I,O] = Halt[I,O]())
      extends Process[I,O]

    case class Await[I,O](
        recv: Option[I] => Process[I,O])
      extends Process[I,O]

    case class Halt[I,O]() extends Process[I,O]

    def emit[I,O](head: O,
                  tail: Process[I,O] = Halt[I,O]()): Process[I,O] =
      Emit(head, tail)

    // Process forms a monad, and we provide monad syntax for it

    import fpinscala.iomonad.Monad

    def monad[I]: Monad[({ type f[x] = Process[I,x]})#f] =
      new Monad[({ type f[x] = Process[I,x]})#f] {
        def unit[O](o: => O): Process[I,O] = emit(o)
        def flatMap[O,O2](p: Process[I,O])(f: O => Process[I,O2]): Process[I,O2] =
          p flatMap f
      }

    // enable monadic syntax for `Process` type
    implicit def toMonadic[I,O](a: Process[I,O]) = monad[I].toMonadic(a)

    /**
     * A helper function to await an element or fall back to another process
     * if there is no input.
     */
    def await[I,O](f: I => Process[I,O],
                   fallback: Process[I,O] = Halt[I,O]()): Process[I,O] =
      Await[I,O] {
        case Some(i) => f(i)
        case None => fallback
      }

    /*
     * We can convert any function `f: I => O` to a `Process[I,O]`. We
     * simply `Await`, then `Emit` the value received, transformed by
     * `f`.
     */
    def liftOne[I,O](f: I => O): Process[I,O] =
      Await {
        case Some(i) => emit(f(i))
        case None => Halt()
      }

    def lift[I,O](f: I => O): Process[I,O] =
      liftOne(f).repeat

    /*
     * As an example of `repeat`, here's a definition of `filter` that
     * uses `repeat`.
     */
    def filter[I](f: I => Boolean): Process[I,I] =
      Await[I,I] {
        case Some(i) if f(i) => emit(i)
        case _ => Halt()
      }.repeat

    /*
     * Here's a typical `Process` definition that requires tracking some
     * piece of state (in this case, the running total):
     */
    def sum: Process[Double,Double] = {
      def go(acc: Double): Process[Double,Double] =
        await(d => emit(d+acc, go(d+acc)))
      go(0.0)
    }

    /*
     * Exercise 1: Implement `take`, `drop`, `takeWhile`, and `dropWhile`.
     */
    def take[I](n: Int): Process[I,I] = {
      if (n == 0) Halt()
      else await(i => emit(i, take(n-1)))
    }

    def drop[I](n: Int): Process[I,I] =
      await(i =>{
        if (n > 0) drop(n-1)
        else emit(i)
      })

    def takeWhile[I](f: I => Boolean): Process[I,I] =
      await(i => {
        if (f(i)) emit(i, takeWhile(f))
        else Halt()
      })

    def dropWhile[I](f: I => Boolean): Process[I,I] =
      await(i => {
        if (f(i)) dropWhile(f)
        else emit(i)
      })

    /* The identity `Process`, just repeatedly echos its input. */
    def id[I]: Process[I,I] = lift(identity)

    /*
     * Exercise 2: Implement `count`.
     */
    def count[I]: Process[I,Int] = {
      def go(n:Int):Process[I,Int] =
        await(i => {
          emit(n+1,go(n+1))
        })
      go(0)
    }

    /* For comparison, here is an explicit recursive implementation. */
    def count2[I]: Process[I,Int] = {
      def go(n: Int): Process[I,Int] =
        await((i: I) => emit(n+1, go(n+1)))
      go(0)
    }

    /*
     * Exercise 3: Implement `mean`.
     */
    def mean: Process[Double,Double] = {
      def go(sum:Double, count:Double):Process[Double,Double] =
        await(d => {
          emit((sum+d)/(count+1),go(sum+d,count+1))
        })
      go(0,0)
    }

    def loop[S,I,O](z: S)(f: (I,S) => (O,S)): Process[I,O] =
      await((i: I) => f(i,z) match {
        case (o,s2) => emit(o, loop(s2)(f))
      })

    /* Exercise 4: Implement `sum` and `count` in terms of `loop` */

    def sum2: Process[Double,Double] =
      loop[Double,Double,Double](0.0)((i,s) => (i+s,i+s))

    def count3[I]: Process[I,Int] =
      loop[Int,I,Int](0)((i,s) => (s+1, s+1))
    /*
     * Exercise 7: Can you think of a generic combinator that would
     * allow for the definition of `mean` in terms of `sum` and
     * `count`?
     *
     * Zip - 5 cases
     *   - both emit => emit the heads of both and zip the tail
     *   - p1 awaits => when p1 is ready, zip with p2
     *   - p2 awaits => when p2 is ready, zip with p1
     *   - p1 halts  => halt, don't care about p2
     *   - p2 halts  => halt, don't care about p1
     */
    def zip[A,B,C](p1: Process[A,B], p2: Process[A,C]): Process[A,(B,C)] = (p1,p2) match {
      case (Emit(h1,t1),Emit(h2,t2)) => Emit((h1,h2),zip(t1,t2))
      case (Await(r1),_) =>
        Await((o:Option[A]) => zip(r1(o),feed(o)(p2)))
      case (_, Await(r2)) =>
        Await((o:Option[A]) => zip(feed(o)(p1),r2(o)))
      case (Halt(),_) => Halt()
      case (_,Halt()) => Halt()
    }

     def feed[A,B](oa: Option[A])(p: Process[A,B]): Process[A,B] =
      p match {
        case Halt() => p
        case Emit(h,t) => Emit(h, feed(oa)(t))
        case Await(recv) => recv(oa)
      }

    /*
     * Exercise 6: Implement `zipWithIndex`.
     *
     * See definition on `Process` above.
     */

    def zipWithIndex[I]: Process[I,(I,Int)] = {
      def go(n:Int):Process[I,(I,Int)] =
        await(i => emit((i,n), go(n+1)))
      go(0)
    }

    /*
     * Exercise 8: Implement `exists`
     *
     * We choose to emit all intermediate values, and not halt.
     * See `existsResult` below for a trimmed version.
     */
    def exists[I](f: I => Boolean): Process[I,Boolean] =
      await(i => emit(f(i),exists(f)))

    /* Awaits then emits a single value, then halts. */
    def echo[I]: Process[I,I] = await(i => emit(i))

    def skip[I,O]: Process[I,O] = await(i => Halt())
    def ignore[I,O]: Process[I,O] = skip.repeat

    def terminated[I]: Process[I,Option[I]] =
      await((i: I) => emit(Some(i), terminated[I]), emit(None))

    def processFile[A,B](f: java.io.File,
                         p: Process[String, A],
                         z: B)(g: (B, A) => B): IO[B] = IO {
      @annotation.tailrec
      def go(ss: Iterator[String], cur: Process[String, A], acc: B): B =
        cur match {
          case Halt() => acc
          case Await(recv) =>
            val next = if (ss.hasNext) recv(Some(ss.next))
                       else recv(None)
            go(ss, next, acc)
          case Emit(h, t) => go(ss, t, g(acc, h))
        }
      val s = io.Source.fromFile(f)
      try go(s.getLines, p, z)
      finally s.close
    }

    /*
     * Exercise 9: Write a program that reads degrees fahrenheit as `Double` values from a file,
     * converts each temperature to celsius, and writes results to another file.
     */
    def processFileToCelsius(f: java.io.File):IO[Double] =
      processFile[Double,Double](f,processToCelsius ,0)((b,a) => b)

    def processToCelsius:Process[String,Double] =
      filter((s:String) => !s.isEmpty) |>
      filter((s:String) => !s.startsWith("#")) |>
      lift((s:String) => s.toDouble) |>
      lift((d:Double) => toCelsius(d))

    def toCelsius(fahrenheit: Double): Double =
      (5.0 / 9.0) * (fahrenheit - 32.0)
  }
}

object GeneralizedStreamTransducers {

                            /*

  Our generalized process type is parameterized on the protocol used for
  communicating with the driver. This works similarly to the `IO` type
  we defined in chapter 13. The `Await` constructor emits a request of
  type `F[A]`, and receives a response of type `Either[Throwable,A]`:

    trait Process[F,A]
    case class Await[F[_],A,O](
      req: F[A],
      recv: Either[Throwable,A] => Process[F,O]) extends Process[F,O]
    case class Halt[F[_],O](err: Throwable) extends Process[F,O]
    case class Emit[F[_],O](head: O, tail: Process[F,O]) extends Process[F,O]

  The `Await` constructor may now receive a successful result or an error.

  The `Halt` constructor now has a _reason_ for termination, which may be
  either normal termination indicated by the special exception `End`,
  forceful terimation, indicated by the special exception `Kill`,
  or some other error.

  We'll use the improved `Await` and `Halt` cases together to ensure
  that all resources get released, even in the event of exceptions.

                             */

  trait Process[F[_],O] {
    import Process._

    /*
     * Many of the same operations can be defined for this generalized
     * `Process` type, regardless of the choice of `F`.
     */

    def map[O2](f: O => O2): Process[F,O2] = this match {
      case Await(req,recv) =>
        Await(req, recv andThen (_ map f))
      case Emit(h, t) => Try { Emit(f(h), t map f) }
      case Halt(err) => Halt(err)
    }

    def ++(p: => Process[F,O]): Process[F,O] =
      this.onHalt {
        case End => Try(p) // we consult `p` only on normal termination
        case err => Halt(err)
      }

    /*
     * Like `++`, but _always_ runs `p`, even if `this` halts with an error.
     */
    def onComplete(p: => Process[F,O]): Process[F,O] =
      this.onHalt {
        case End => p.asFinalizer
        case err => p.asFinalizer ++ Halt(err) // we always run `p`, but preserve any errors
      }

    def asFinalizer: Process[F,O] = this match {
      case Emit(h, t) => Emit(h, t.asFinalizer)
      case Halt(e) => Halt(e)
      case Await(req,recv) => await(req) {
        case Left(Kill) => this.asFinalizer
        case x => recv(x)
      }
    }

    def onHalt(f: Throwable => Process[F,O]): Process[F,O] = this match {
      case Halt(e) => Try(f(e))
      case Emit(h, t) => Emit(h, t.onHalt(f))
      case Await(req,recv) => Await(req, recv andThen (_.onHalt(f)))
    }

    /*
     * Anywhere we _call_ `f`, we catch exceptions and convert them to `Halt`.
     * See the helper function `Try` defined below.
     */
    def flatMap[O2](f: O => Process[F,O2]): Process[F,O2] =
      this match {
        case Halt(err) => Halt(err)
        case Emit(o, t) => Try(f(o)) ++ t.flatMap(f)
        case Await(req,recv) =>
          Await(req, recv andThen (_ flatMap f))
      }

    def repeat: Process[F,O] =
      this ++ this.repeat

    def repeatNonempty: Process[F,O] = {
      val cycle = (this.map(o => Some(o): Option[O]) ++ emit(None)).repeat
      // cut off the cycle when we see two `None` values in a row, as this
      // implies `this` has produced no values during an iteration
      val trimmed = cycle |> window2 |> (takeWhile {
        case (Some(None), None) => false
        case _ => true
      })
      trimmed.map(_._2).flatMap {
        case None => Halt(End)
        case Some(o) => emit(o)
      }
    }

    /*
     * Exercise 10: This function is defined only if given a `MonadCatch[F]`.
     * Unlike the simple `runLog` interpreter defined in the companion object
     * below, this is not tail recursive and responsibility for stack safety
     * is placed on the `Monad` instance.
     */
    def runLog(implicit F: MonadCatch[F]): F[IndexedSeq[O]] = {
      def go(curr:Process[F,O], acc:IndexedSeq[O]):F[IndexedSeq[O]] =
        curr match {
          case Emit(h,t) => go(t, acc :+ h)
          case Halt(End) => F.unit(acc)
          case Halt(err) => F.fail(err)
          case Await(req,recv) => F.flatMap(F.attempt(req)) {e => go(Try(recv(e)), acc) }
        }
      go(this,IndexedSeq())
    }

    /*
     * We define `Process1` as a type alias - see the companion object
     * for `Process` below. Using that, we can then define `|>` once
     * more. The definition is extremely similar to our previous
     * definition. We again use the helper function, `feed`, to take
     * care of the case where `this` is emitting values while `p2`
     * is awaiting these values.
     *
     * The one subtlety is we make sure that if `p2` halts, we
     * `kill` this process, giving it a chance to run any cleanup
     * actions (like closing file handles, etc).
     */
    def |>[O2](p2: Process1[O,O2]): Process[F,O2] = {
      p2 match {
        case Halt(e) => this.kill onHalt { e2 => Halt(e) ++ Halt(e2) }
        case Emit(h, t) => Emit(h, this |> t)
        case Await(req,recv) => this match {
          case Halt(err) => Halt(err) |> recv(Left(err))
          case Emit(h,t) => t |> Try(recv(Right(h)))
          case Await(req0,recv0) => await(req0)(recv0 andThen (_ |> p2))
        }
      }
    }

    @annotation.tailrec
    final def kill[O2]: Process[F,O2] = this match {
      case Await(req,recv) => recv(Left(Kill)).drain.onHalt {
        case Kill => Halt(End) // we convert the `Kill` exception back to normal termination
        case e => Halt(e)
      }
      case Halt(e) => Halt(e)
      case Emit(h, t) => t.kill
    }

    /** Alias for `this |> p2`. */
    def pipe[O2](p2: Process1[O,O2]): Process[F,O2] =
      this |> p2

    final def drain[O2]: Process[F,O2] = this match {
      case Halt(e) => Halt(e)
      case Emit(h, t) => t.drain
      case Await(req,recv) => Await(req, recv andThen (_.drain))
    }

    def filter(f: O => Boolean): Process[F,O] =
      this |> Process.filter(f)

    def take(n: Int): Process[F,O] =
      this |> Process.take(n)

    def once: Process[F,O] = take(1)

    /*
     * Use a `Tee` to interleave or combine the outputs of `this` and
     * `p2`. This can be used for zipping, interleaving, and so forth.
     * Nothing requires that the `Tee` read elements from each
     * `Process` in lockstep. It could read fifty elements from one
     * side, then two elements from the other, then combine or
     * interleave these values in some way, etc.
     *
     * This definition uses two helper functions, `feedL` and `feedR`,
     * which feed the `Tee` in a tail-recursive loop as long as
     * it is awaiting input.
     */
    def tee[O2,O3](p2: Process[F,O2])(t: Tee[O,O2,O3]): Process[F,O3] = {
      t match {
        case Halt(e) => this.kill onComplete p2.kill onComplete Halt(e)
        case Emit(h,t) => Emit(h, (this tee p2)(t))
        case Await(side, recv) => side.get match {
          case Left(isO) => this match {
            case Halt(e) => p2.kill onComplete Halt(e)
            case Emit(o,ot) => (ot tee p2)(Try(recv(Right(o))))
            case Await(reqL, recvL) =>
              await(reqL)(recvL andThen (this2 => this2.tee(p2)(t)))
          }
          case Right(isO2) => p2 match {
            case Halt(e) => this.kill onComplete Halt(e)
            case Emit(o2,ot) => (this tee ot)(Try(recv(Right(o2))))
            case Await(reqR, recvR) =>
              await(reqR)(recvR andThen (p3 => this.tee(p3)(t)))
          }
        }
      }
    }

    def zipWith[O2,O3](p2: Process[F,O2])(f: (O,O2) => O3): Process[F,O3] =
      (this tee p2)(Process.zipWith(f))

    def zip[O2](p2: Process[F,O2]): Process[F,(O,O2)] =
      zipWith(p2)((_,_))

    def to[O2](sink: Sink[F,O]): Process[F,Unit] =
      join { (this zipWith sink)((o,f) => f(o)) }

    def through[O2](p2: Channel[F, O, O2]): Process[F,O2] =
      join { (this zipWith p2)((o,f) => f(o)) }
  }

  object Process {
    case class Await[F[_],A,O](
      req: F[A],
      recv: Either[Throwable,A] => Process[F,O]) extends Process[F,O]

    case class Emit[F[_],O](
      head: O,
      tail: Process[F,O]) extends Process[F,O]

    case class Halt[F[_],O](err: Throwable) extends Process[F,O]

    def emit[F[_],O](
        head: O,
        tail: Process[F,O] = Halt[F,O](End)): Process[F,O] =
      Emit(head, tail)

    def await[F[_],A,O](req: F[A])(recv: Either[Throwable,A] => Process[F,O]): Process[F,O] =
      Await(req, recv)

    /**
     * Helper function to safely produce `p`, or gracefully halt
     * with an error if an exception is thrown.
     */
    def Try[F[_],O](p: => Process[F,O]): Process[F,O] =
      try p
      catch { case e: Throwable => Halt(e) }

    /*
     * Safely produce `p`, or run `cleanup` and halt gracefully with the
     * exception thrown while evaluating `p`.
     */
    def TryOr[F[_],O](p: => Process[F,O])(cleanup: Process[F,O]): Process[F,O] =
      try p
      catch { case e: Throwable => cleanup ++ Halt(e) }

    /*
     * Safely produce `p`, or run `cleanup` or `fallback` if an exception
     * occurs while evaluating `p`.
     */
    def TryAwait[F[_],O](p: => Process[F,O])(fallback: Process[F,O], cleanup: Process[F,O]): Process[F,O] =
      try p
      catch {
        case End => fallback
        case e: Throwable => cleanup ++ Halt(e)
      }

    /* Our generalized `Process` type can represent sources! */

    import fpinscala.iomonad.IO

    /* Special exception indicating normal termination */
    case object End extends Exception

    /* Special exception indicating forceful termination */
    case object Kill extends Exception

    /*
     * A `Process[F,O]` where `F` is a monad like `IO` can be thought of
     * as a source.
     */

    /*
     * Here is a simple tail recursive function to collect all the
     * output of a `Process[IO,O]`. Notice we are using the fact
     * that `IO` can be `run` to produce either a result or an
     * exception.
     */
    def runLog[O](src: Process[IO,O]): IO[IndexedSeq[O]] = IO {
      val E = java.util.concurrent.Executors.newFixedThreadPool(4)
      @annotation.tailrec
      def go(cur: Process[IO,O], acc: IndexedSeq[O]): IndexedSeq[O] =
        cur match {
          case Emit(h,t) => go(t, acc :+ h)
          case Halt(End) => acc
          case Halt(err) => throw err
          case Await(req,recv) =>
            val next =
              try recv(Right(fpinscala.iomonad.unsafePerformIO(req)(E)))
              catch { case err: Throwable => recv(Left(err)) }
            go(next, acc)
        }
      try go(src, IndexedSeq())
      finally E.shutdown
    }

    /*
     * We can write a version of collect that works for any `Monad`.
     * See the definition in the body of `Process`.
     */

    import java.io.{BufferedReader,FileReader}
    val p: Process[IO, String] =
      await(IO(new BufferedReader(new FileReader("lines.txt")))) {
        case Right(b) =>
          lazy val next: Process[IO,String] = await(IO(b.readLine)) {
            case Left(e) => await(IO(b.close))(_ => Halt(e))
            case Right(line) => Emit(line, next)
          }
          next
        case Left(e) => Halt(e)
      }

    /*
     * Generic combinator for producing a `Process[IO,O]` from some
     * effectful `O` source. The source is tied to some resource,
     * `R` (like a file handle) that we want to ensure is released.
     * See `lines` below for an example use.
     */
    def resource[R,O](acquire: IO[R])(
                      use: R => Process[IO,O])(
                      release: R => Process[IO,O]): Process[IO,O] =
      eval(acquire) flatMap { r => use(r).onComplete(release(r)) }

    /*
     * Like `resource`, but `release` is a single `IO` action.
     */
    def resource_[R,O](acquire: IO[R])(
                       use: R => Process[IO,O])(
                       release: R => IO[Unit]): Process[IO,O] =
      resource(acquire)(use)(release andThen (eval_[IO,Unit,O]))

    /*
     * Create a `Process[IO,O]` from the lines of a file, using
     * the `resource` combinator above to ensure the file is closed
     * when processing the stream of lines is finished.
     */
    def lines(filename: String): Process[IO,String] =
      resource
        { IO(io.Source.fromFile(filename)) }
        { src =>
            lazy val iter = src.getLines // a stateful iterator
            def step = if (iter.hasNext) Some(iter.next) else None
            lazy val lines: Process[IO,String] = eval(IO(step)).flatMap {
              case None => Halt(End)
              case Some(line) => Emit(line, lines)
            }
            lines
        }
        { src => eval_ { IO(src.close) } }

    /* Exercise 11: Implement `eval`, `eval_`, and use these to implement `lines`. */
    def eval[F[_],A](a: F[A]): Process[F,A] = await(a){
      case Left(err) => Halt(err)
      case Right(v) => Emit(v,Halt(End))
    }

    /* Evaluate the action purely for its effects. */
    def eval_[F[_],A,B](a: F[A]): Process[F,B] = eval(a).drain

    /* Helper function with better type inference. */
    def evalIO[A](a: IO[A]): Process[IO,A] =
      eval[IO,A](a)

    /*
     * We now have nice, resource safe effectful sources, but we don't
     * have any way to transform them or filter them. Luckily we can
     * still represent the single-input `Process` type we introduced
     * earlier, which we'll now call `Process1`.
     */

    case class Is[I]() {
      sealed trait f[X]
      val Get = new f[I] {}
    }
    def Get[I] = Is[I]().Get

    type Process1[I,O] = Process[Is[I]#f, O]

    /* Some helper functions to improve type inference. */

    def await1[I,O](
        recv: I => Process1[I,O],
        fallback: => Process1[I,O] = halt1[I,O]): Process1[I, O] =
      Await(Get[I], (e: Either[Throwable,I]) => e match {
        case Left(End) => fallback
        case Left(err) => Halt(err)
        case Right(i) => Try(recv(i))
      })

    def emit1[I,O](h: O, tl: Process1[I,O] = halt1[I,O]): Process1[I,O] =
      emit(h, tl)

    def halt1[I,O]: Process1[I,O] = Halt[Is[I]#f, O](End)

    def lift[I,O](f: I => O): Process1[I,O] =
      await1[I,O]((i:I) => emit(f(i))) repeat

    def filter[I](f: I => Boolean): Process1[I,I] =
      await1[I,I](i => if (f(i)) emit(i) else halt1) repeat

    // we can define take, takeWhile, and so on as before

    def take[I](n: Int): Process1[I,I] =
      if (n <= 0) halt1
      else await1[I,I](i => emit(i, take(n-1)))

    def takeWhile[I](f: I => Boolean): Process1[I,I] =
      await1(i =>
        if (f(i)) emit(i, takeWhile(f))
        else      halt1)

    def dropWhile[I](f: I => Boolean): Process1[I,I] =
      await1(i =>
        if (f(i)) dropWhile(f)
        else      emit(i,id))

    def id[I]: Process1[I,I] =
      await1((i: I) => emit(i, id))

    def window2[I]: Process1[I,(Option[I],I)] = {
      def go(prev: Option[I]): Process1[I,(Option[I],I)] =
        await1[I,(Option[I],I)](i => emit(prev -> i) ++ go(Some(i)))
      go(None)
    }

    /** Emits `sep` in between each input received. */
    def intersperse[I](sep: I): Process1[I,I] =
      await1[I,I](i => emit1(i) ++ id.flatMap(i => emit1(sep) ++ emit1(i)))

                            /*

    We sometimes need to construct a `Process` that will pull values
    from multiple input sources. For instance, suppose we want to
    'zip' together two files, `f1.txt` and `f2.txt`, combining
    corresponding lines in some way. Using the same trick we used for
    `Process1`, we can create a two-input `Process` which can request
    values from either the 'left' stream or the 'right' stream. We'll
    call this a `Tee`, after the letter 'T', which looks like a
    little diagram of two inputs being combined into one output.

                             */

    case class T[I,I2]() {
      sealed trait f[X] { def get: Either[I => X, I2 => X] }
      val L = new f[I] { def get = Left(identity) }
      val R = new f[I2] { def get = Right(identity) }
    }
    def L[I,I2] = T[I,I2]().L
    def R[I,I2] = T[I,I2]().R

    type Tee[I,I2,O] = Process[T[I,I2]#f, O]

    /* Again some helper functions to improve type inference. */

    def haltT[I,I2,O]: Tee[I,I2,O] =
      Halt[T[I,I2]#f,O](End)

    def awaitL[I,I2,O](recv: I => Tee[I,I2,O],
                       fallback: => Tee[I,I2,O] = haltT[I,I2,O]): Tee[I,I2,O] =
      await[T[I,I2]#f,I,O](L) {
        case Left(End) => fallback
        case Left(err) => Halt(err)
        case Right(a) => Try(recv(a))
      }

    def awaitR[I,I2,O](recv: I2 => Tee[I,I2,O],
                       fallback: => Tee[I,I2,O] = haltT[I,I2,O]): Tee[I,I2,O] =
      await[T[I,I2]#f,I2,O](R) {
        case Left(End) => fallback
        case Left(err) => Halt(err)
        case Right(a) => Try(recv(a))
      }

    def emitT[I,I2,O](h: O, tl: Tee[I,I2,O] = haltT[I,I2,O]): Tee[I,I2,O] =
      emit(h, tl)

    def zipWith[I,I2,O](f: (I,I2) => O): Tee[I,I2,O] =
      awaitL[I,I2,O](i  =>
      awaitR        (i2 => emitT(f(i,i2)))) repeat

    def zip[I,I2]: Tee[I,I2,(I,I2)] = zipWith((_,_))

    /* Ignores all input from left. */
    def passR[I,I2]: Tee[I,I2,I2] = awaitR(emitT(_, passR))

    /* Ignores input from the right. */
    def passL[I,I2]: Tee[I,I2,I] = awaitL(emitT(_, passL))

    /* Alternate pulling values from the left and the right inputs. */
    def interleaveT[I]: Tee[I,I,I] =
      awaitL[I,I,I](i =>
      awaitR       (i2 => emitT(i) ++ emitT(i2))) repeat

                            /*

    Our `Process` type can also represent effectful sinks (like a file).
    A `Sink` is simply a source of effectful functions! See the
    definition of `to` in `Process` for an example of how to feed a
    `Process` to a `Sink`.

                             */

    type Sink[F[_],O] = Process[F, O => Process[F,Unit]]

    import java.io.FileWriter

    /* A `Sink` which writes input strings to the given file. */
    def fileW(file: String, append: Boolean = false): Sink[IO,String] =
      resource[FileWriter, String => Process[IO,Unit]]
        { IO { new FileWriter(file, append) }}
        { w => constant { (s: String) => eval[IO,Unit](IO(w.write(s))) }}
        { w => eval_(IO(w.close)) }

    /* The infinite, constant stream. */
    def constant[A](a: A): Process[IO,A] =
      eval(IO(a)).flatMap { a => Emit(a, constant(a)) }

    /* Exercise 12: Implement `join`. Notice this is the standard monadic combinator! */
    def join[F[_],A](p: Process[F,Process[F,A]]): Process[F,A] =
      p.flatMap(fa => fa)
    /*
     * An example use of the combinators we have so far: incrementally
     * convert the lines of a file from fahrenheit to celsius.
     */

    import fpinscala.iomonad.IO0.fahrenheitToCelsius

    val converter: Process[IO,Unit] =
      lines("fahrenheit.txt").
      filter(line => !line.startsWith("#") && !line.trim.isEmpty).
      map(line => fahrenheitToCelsius(line.toDouble).toString).
      pipe(intersperse("\n")).
      to(fileW("celsius.txt")).
      drain

                            /*

    More generally, we can feed a `Process` through an effectful
    channel which returns a value other than `Unit`.

                             */

    type Channel[F[_],I,O] = Process[F, I => Process[F,O]]

    /*
     * Here is an example, a JDBC query runner which returns the
     * stream of rows from the result set of the query. We have
     * the channel take a `Connection => PreparedStatement` as
     * input, so code that uses this channel does not need to be
     * responsible for knowing how to obtain a `Connection`.
     */
    import java.sql.{Connection, PreparedStatement, ResultSet}

    def query(conn: IO[Connection]):
        Channel[IO, Connection => PreparedStatement, Map[String,Any]] =
      resource_
        { conn }
        { conn => constant { (q: Connection => PreparedStatement) =>
          resource_
            { IO {
                val rs = q(conn).executeQuery
                val ncols = rs.getMetaData.getColumnCount
                val cols = (1 to ncols).map(rs.getMetaData.getColumnName)
                (rs, cols)
            }}
            { case (rs, cols) =>
                def step =
                  if (!rs.next) None
                  else Some(cols.map(c => (c, rs.getObject(c): Any)).toMap)
                lazy val rows: Process[IO,Map[String,Any]] =
                  eval(IO(step)).flatMap {
                    case None => Halt(End)
                    case Some(row) => Emit(row, rows)
                  }
                rows
            }
            { p => IO { p._1.close } } // close the ResultSet
        }}
        { c => IO(c.close) }

    /*
     * We can allocate resources dynamically when defining a `Process`.
     * As an example, this program reads a list of filenames to process
     * _from another file_, opening each file, processing it and closing
     * it promptly.
     */

    val convertAll: Process[IO,Unit] = (for {
      out <- fileW("celsius.txt").once
      file <- lines("fahrenheits.txt")
      _ <- lines(file).
           map(line => fahrenheitToCelsius(line.toDouble)).
           flatMap(celsius => out(celsius.toString))
    } yield ()) drain

    /*
     * Just by switching the order of the `flatMap` calls, we can output
     * to multiple files.
     */
    val convertMultisink: Process[IO,Unit] = (for {
      file <- lines("fahrenheits.txt")
      _ <- lines(file).
           map(line => fahrenheitToCelsius(line.toDouble)).
           map(_ toString).
           to(fileW(file + ".celsius"))
    } yield ()) drain

    /*
     * We can attach filters or other transformations at any point in the
     * program, for example:
     */
    val convertMultisink2: Process[IO,Unit] = (for {
      file <- lines("fahrenheits.txt")
      _ <- lines(file).
           filter(!_.startsWith("#")).
           map(line => fahrenheitToCelsius(line.toDouble)).
           filter(_ > 0). // ignore below zero temperatures
           map(_ toString).
           to(fileW(file + ".celsius"))
    } yield ()) drain
  }
}

object ProcessTest extends App {
  import GeneralizedStreamTransducers._
  import fpinscala.iomonad.IO
  import Process._

  val p = eval(IO { println("woot"); 1 }).repeat
  val p2 = eval(IO { println("cleanup"); 2 } ).onHalt {
    case Kill => println { "cleanup was killed, instead of bring run" }; Halt(Kill)
    case e => Halt(e)
  }

  println { Process.runLog { p2.onComplete(p2).onComplete(p2).take(1).take(1) } }
  println { Process.runLog(converter) }
  // println { Process.collect(Process.convertAll) }
}


text:
package Example;

/**
* Example/EchoHolder.java .
* Generated by the IDL-to-Java compiler (portable), version "3.2"
* from ../echo.idl
* Dienstag, 13. Januar 2015 13:37 Uhr MEZ
*/

public final class EchoHolder implements org.omg.CORBA.portable.Streamable
{
  public Example.Echo value = null;

  public EchoHolder ()
  {
  }

  public EchoHolder (Example.Echo initialValue)
  {
    value = initialValue;
  }

  public void _read (org.omg.CORBA.portable.InputStream i)
  {
    value = Example.EchoHelper.read (i);
  }

  public void _write (org.omg.CORBA.portable.OutputStream o)
  {
    Example.EchoHelper.write (o, value);
  }

  public org.omg.CORBA.TypeCode _type ()
  {
    return Example.EchoHelper.type ();
  }

}


text:
<?xml version="1.0" encoding="utf-8"?>
<resources>
    <!-- VIDEO TRIMMER-->
    <dimen name="progress_video_line_height">2dp</dimen>
    <dimen name="frames_video_height">40dip</dimen>
</resources>