alvinalexander.com | career | drupal | java | mac | mysql | perl | scala | uml | unix  

Scala example source code file (Monad.scala)

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

Java - Scala tags/keywords

a, a, b, functor, m, m, monad, monad, monads, nothing, s, s, statereader, zeromonad

The Scala Monad.scala source code

// -----------------------------------------------------------------------------
//
//  Scalax - The Scala Community Library
//  Copyright (c) 2005-8 The Scalax Project. All rights reserved.
//
//  The primary distribution site is http://scalax.scalaforge.org/
//
//  This software is released under the terms of the Revised BSD License.
//  There is NO WARRANTY.  See the file LICENSE for the full text.
//
// -----------------------------------------------------------------------------

package scala.tools.scalap
package scalax
package rules

trait Monad[+A] extends Functor[A] { 
  type M[+A] <: Monad[A]
  def flatMap[B](f : A => M[B]) : M[B]
}

trait Monads extends UnitFunctors {
  type M[+A] <: Monad[A]
  
  trait Monad[+A] extends Functor[A] with rules.Monad[A] { this : M[A] =>
    def map[B](f : A => B) = flatMap { a => unit(f(a)) }
  }
  
  trait ZeroMonad extends Monad[Nothing] with ZeroFunctor { this : M[Nothing] =>
    def flatMap[B](f : Nothing => M[B]) : M[B] = this
  }
}


trait StateReader extends Monads {
  type S
  
  def get : M[S]
  def read[A](f : S => A) : M[A]
  def set(s : => S) : M[S]
  def update(f : S => S) : M[S]
}




Other Scala examples (source code examples)

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

... this post is sponsored by my books ...

#1 New Release!

FP Best Seller

 

new blog posts

 

Copyright 1998-2021 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.