<commit_before>package scalaz.contrib
package std

import scalaz._

import scala.concurrent.{Future, ExecutionContext, CanAwait, Await}
import scala.concurrent.duration.Duration

trait FutureInstances1 {

  private[scalaz] class FutureMonoid[A](implicit m: Monoid[A], executionContext: ExecutionContext) extends Monoid[Future[A]] {

    def zero: Future[A] = Future(m.zero)

    def append(f1: Future[A], f2: => Future[A]): Future[A] = for {
      first <- f1
      second <- f2
    } yield m.append(first, second)

  }

  implicit def FutureMonoid[A](implicit m: Monoid[A], executionContext: ExecutionContext): Monoid[Future[A]] = new FutureMonoid[A]

}

trait FutureInstances extends FutureInstances1 {

  private[scalaz] class FutureInstances(implicit ec: ExecutionContext) extends Monad[Future] with Cobind[Future] with Cojoin[Future] with Each[Future] {

    def point[A](a: => A): Future[A] = Future(a)
    def bind[A, B](fa: Future[A])(f: A => Future[B]): Future[B] = fa flatMap f
    override def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa map f
    def cobind[A, B](fa: Future[A])(f: Future[A] => B): Future[B] = Future(f(fa))
    def cojoin[A](a: Future[A]): Future[Future[A]] = Future(a)
    def each[A](fa: Future[A])(f: A => Unit) = fa foreach f

  }

  implicit def futureInstance(implicit executionContext: ExecutionContext): Monad[Future] with Cobind[Future] with Cojoin[Future] with Each[Future] = 
    new FutureInstances


  /**
   * Requires explicit usage as the use of `Await.result`.
   * Can throw an exception, which is inherently bad.
   */
  def futureComonad(duration: Duration)(implicit executionContext: ExecutionContext): Comonad[Future] = new FutureInstances with Comonad[Future] {
    def copoint[A](f: Future[A]): A = Await.result(f, duration)
  }


  implicit def FutureGroup[A](implicit g: Group[A], executionContext: ExecutionContext): Group[Future[A]] = new FutureMonoid[A] with Group[Future[A]] {

    def inverse(f: Future[A]): Future[A] = f.map(value => g.inverse(value))

  }

}

object scalaFuture extends FutureInstances

// vim: expandtab:ts=2:sw=2<commit_msg>tuned<commit_after>package scalaz.contrib
package std

import scalaz._

import scala.concurrent.{Future, ExecutionContext, CanAwait, Await}
import scala.concurrent.duration.Duration

trait FutureInstances1 {

  private[scalaz] class FutureMonoid[A](implicit m: Monoid[A], executionContext: ExecutionContext) extends Monoid[Future[A]] {

    def zero: Future[A] = Future(m.zero)

    def append(f1: Future[A], f2: => Future[A]): Future[A] = for {
      first <- f1
      second <- f2
    } yield m.append(first, second)

  }

  implicit def FutureMonoid[A](implicit m: Monoid[A], executionContext: ExecutionContext): Monoid[Future[A]] = new FutureMonoid[A]

}

trait FutureInstances extends FutureInstances1 {

  private[scalaz] class FutureInstances(implicit ec: ExecutionContext) extends Monad[Future] with Cobind[Future] with Cojoin[Future] with Each[Future] {

    def point[A](a: => A): Future[A] = Future(a)
    def bind[A, B](fa: Future[A])(f: A => Future[B]): Future[B] = fa flatMap f
    override def map[A, B](fa: Future[A])(f: A => B): Future[B] = fa map f
    def cobind[A, B](fa: Future[A])(f: Future[A] => B): Future[B] = Future(f(fa))
    def cojoin[A](a: Future[A]): Future[Future[A]] = Future(a)
    def each[A](fa: Future[A])(f: A => Unit) = fa foreach f

  }

  implicit def futureInstance(implicit executionContext: ExecutionContext): Monad[Future] with Cobind[Future] with Cojoin[Future] with Each[Future] = 
    new FutureInstances


  /**
   * Requires explicit usage as the use of `Await.result`. Can throw an exception, which is inherently bad.
   */
  def futureComonad(duration: Duration)(implicit executionContext: ExecutionContext): Comonad[Future] = new FutureInstances with Comonad[Future] {
    def copoint[A](f: Future[A]): A = Await.result(f, duration)
  }


  implicit def FutureGroup[A](implicit g: Group[A], executionContext: ExecutionContext): Group[Future[A]] = new FutureMonoid[A] with Group[Future[A]] {
    def inverse(f: Future[A]): Future[A] = f.map(value => g.inverse(value))
  }

}

object scalaFuture extends FutureInstances

// vim: expandtab:ts=2:sw=2
{'fraction_non_alphanumeric': 0.14217896985521006, 'fraction_numerical': 0.0037977688108236413, 'mean_word_length': 4.844660194174757, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 17, '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': '1064728', 'n_tokens_mistral': 1450, 'n_tokens_neox': 1408, 'n_words': 371}