home | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Distributive.scala)

This example Scala source code file (Distributive.scala) is included in the alvinalexander.com "Java Source Code Warehouse" project. The intent of this project is to help you "Learn Scala by Example" TM.

Learn more about this Scala project at its project page.

Java - Scala tags/keywords

compositiondistributive, cotraverse, distribution, distributive, functor, productdistributive

The Distributive.scala Scala example source code

package scalaz

/** Dual of [[scalaz.Traverse]].  To transform `F[G[B]]` to `G[F[B]]`,
  * you may use `Traverse[F]` and `Applicative[G]`, but alternatively
  * `Functor[F]` and `Distributive[G]`, which permits greater sharing
  * and nonstrictness.
trait Distributive[F[_]] extends Functor[F] { self =>
  def distributeImpl[G[_]:Functor,A,B](fa: G[A])(f: A => F[B]): F[G[B]]

  /**The composition of Distributives `F` and `G`, `[x]F[G[x]]`, is a Distributive */
  def compose[G[_]](implicit G0: Distributive[G]): Distributive[λ[α => F[G[α]]]] =
    new CompositionDistributive[F, G] {
      implicit def F = self
      implicit def G = G0

  /**The product of Distributives `F` and `G`, `[x](F[x], G[x]])`, is a Distributive */
  def product[G[_]](implicit G0: Distributive[G]): Distributive[λ[α => (F[α], G[α])]] =
    new ProductDistributive[F, G] {
      implicit def F = self
      implicit def G = G0

  class Distribution[G[_]](implicit G: Functor[G]) {
    def run[A,B](fa: G[A])(f: A => F[B]): F[G[B]] = distributeImpl(fa)(f)

  def distribution[G[_]:Functor]: Distribution[G] =
    new Distribution[G]

  def distribute[G[_]:Functor,A,B](fa: G[A])(f: A => F[B]): F[G[B]] =

  def cosequence[G[_]:Functor,A](fa: G[F[A]]): F[G[A]] =
    distributeImpl(fa)(x => x)


object Distributive {
  @inline def apply[F[_]](implicit F: Distributive[F]): Distributive[F] = F

  // Distributive is the dual of Traverse.
  type Cotraverse[F[_]] =

Other Scala examples (source code examples)

Here is a short list of links related to this Scala Distributive.scala source code file:

my book on functional programming


new blog posts


Copyright 1998-2019 Alvin Alexander, alvinalexander.com
All Rights Reserved.

A percentage of advertising revenue from
pages under the /java/jwarehouse URI on this website is
paid back to open source projects.