612人阅读 评论(0)

trait Monad[F[_]] extends Applicative[F] with Bind[F] { self =>

override def map[A,B](fa: F[A])(f: A => B) = bind(fa)(a => point(f(a)))
...
trait Applicative[F[_]] extends Apply[F] { self =>
//// scalaz/Applicative.scala
def point[A](a: => A): F[A]
...
trait Apply[F[_]] extends Functor[F] { self =>
//// scalaz/Apply.scala
def ap[A,B](fa: => F[A])(f: => F[A => B]): F[B]
...
trait Bind[F[_]] extends Apply[F] { self =>
//// scalaz/Bind.scala

/** Equivalent to join(map(fa)(f)). */
def bind[A, B](fa: F[A])(f: A => F[B]): F[B]

override def ap[A, B](fa: => F[A])(f: => F[A => B]): F[B] = {
lazy val fa0 = fa
bind(f)(map(fa0))
}
...

final class BindOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Bind[F]) extends Ops[F[A]] {
////
import Liskov.<~<, Leibniz.===

def flatMap[B](f: A => F[B]) = F.bind(self)(f)

def >>=[B](f: A => F[B]) = F.bind(self)(f)

def ∗[B](f: A => F[B]) = F.bind(self)(f)
...

final class MonadOps[F[_],A] private[syntax](val self: F[A])(implicit val F: Monad[F]) extends Ops[F[A]] {
////

def liftM[G[_[_], _]](implicit G: MonadTrans[G]): G[F, A] = G.liftM(self)

def whileM[G[_]](p: F[Boolean])(implicit G: MonadPlus[G]): F[G[A]] = F.whileM(p, self)

def whileM_(p: F[Boolean]): F[Unit] = F.whileM_(p, self)

def untilM[G[_]](p: => F[Boolean])(implicit G: MonadPlus[G]): F[G[A]] = F.untilM(self, p)

def untilM_(p: => F[Boolean]): F[Unit] = F.untilM_(self, p)

def iterateWhile(p: A => Boolean): F[A] = F.iterateWhile(self)(p)

def iterateUntil(p: A => Boolean): F[A] = F.iterateUntil(self)(p)

////
}


// Functor    :  map[A,B]    (F[A])(f:   A => B):  F[B]
// Applicative:  ap[A,B]     (F[A])(f: F[A => B]): F[B]
// Monad      :  flatMap[A,B](F[A])(f: A => F[B]): F[B]


//   fa.flatMap(a => fb.flatMap(b => fc.flatMap(c => fd.map(...))))


//   for {
//      a <- (fa: F[A])
//      b <- (fb: F[A])
//      c <- (fc: F[A])
//   } yield { ... }


class Foo { def bar: Option[Bar] }
class Bar { def baz: Option[Baz] }
class Bar { def baz: Option[Baz] }

def compute(maybeFoo: Option[Foo]): Option[Int] =
maybeFoo.flatMap { foo =>
foo.bar.flatMap { bar =>
bar.baz.map { baz =>
baz.compute
}
}
}
def compute2(maybeFoo: Option[Foo]): Option[Int] =
for {
foo <- maybeFoo
bar <- foo.bar
baz <- bar.baz
}  yield baz.compute


// ap[A,B](ma: F[A])(mf: F[A => B]): F[B] = mf.flatMap(f => ma.flatMap(a => point(f(a)))


// map[A,B](fa: F[A])(f: A => B): F[B] = fa.flatMap(a => point(f(a)))


2.some flatMap {x => (x + 3).some }               //> res0: Option[Int] = Some(5)
2.some >>= { x => (x + 3).some }                  //> res1: Option[Int] = Some(5)
(none: Option[Int]) >>= {x => (x + 3).some }      //> res2: Option[Int] = None


Monad[Option].point(2)                            //> res3: Option[Int] = Some(2)
//> res4: Option[Int] = Some(5)
(None: Option[Int]) >>= {x => Monad[Option].point(x + 3)}
//> res5: Option[Int] = None


type Discs = Int  //杠铃片数量
case class Barbell(left: Discs, right: Discs) {
def loadLeft(n: Discs): Barbell = copy(left = left + n)
def loadRight(n: Discs): Barbell = copy(right = right + n)
}


Barbell(0,0).loadLeft(1).loadRight(2).loadRight(100).loadLeft(2).loadRight(-99)


type Discs = Int  //杠铃片数量
case class Barbell(left: Discs, right: Discs) {
def loadLeft(n: Discs): Option[Barbell] = copy(left = left + n) match {
case Barbell(left,right) => if ( (left+right <= 20) && math.abs(left-right) <=3 ) Some(Barbell(left,right)) else None
case _ => None
}
def loadRight(n: Discs): Option[Barbell] = copy(right = right + n) match {
case Barbell(left,right) => if ( (left+right <= 20) && math.abs(left-right) <=3 ) Some(Barbell(left,right)) else None
case _ => None
}
}
Barbell(15,1).loadRight(15)                       //> res12: Option[Exercises.monad.Barbell] = None

Barbell(0,0).loadLeft(3) >>= {_.loadRight(3)}     //> res13: Option[Exercises.monad.Barbell] = Some(Barbell(3,3))


def addWeight: Option[Barbell] = for {

def addWeight1: Option[Barbell] = for {


0
0

* 以上用户言论只代表其个人观点，不代表CSDN网站的观点或立场
个人资料
• 访问：73506次
• 积分：2469
• 等级：
• 排名：第17236名
• 原创：180篇
• 转载：0篇
• 译文：0篇
• 评论：2条
最新评论