Stream

摘要: package stream import Stream._ sealed trait Stream[+A] { def toList0: List[A] = this match { case Empty => Nil case Cons(h, t) => h() :: t().toList0 } def toList: List[A] = { ... 阅读全文
posted @ 2016-04-30 08:30 JonkeyGuan 阅读(125) 评论(0) 推荐(0) 编辑

Either Map2Ext

摘要: package either case class Person(name: Name, age: Age) sealed class Name(val value: String) sealed class Age(val value: Int) object Map2Ext { def mkName(name: String): Either[List[String], Nam... 阅读全文
posted @ 2016-04-25 20:38 JonkeyGuan 阅读(271) 评论(0) 推荐(0) 编辑

Either Traverse

摘要: package either object Traverse { def traverse[E, A, B](as: List[A])(f: A => Either[E, B]): Either[E, List[B]] = as match { case Nil => Right(Nil) case h :: t => f(h) flatMap (hh => tra... 阅读全文
posted @ 2016-04-25 20:37 JonkeyGuan 阅读(243) 评论(0) 推荐(0) 编辑

Either Sequence

摘要: package either object Sequence { def sequence[E, A](es: List[Either[E, A]]): Either[E, List[A]] = es match { case Nil => Right(Nil) case h :: t => h flatMap (hh => sequence(t) map (tt ... 阅读全文
posted @ 2016-04-25 20:36 JonkeyGuan 阅读(170) 评论(0) 推荐(0) 编辑

Either

摘要: package either trait Either[+E, +A] { def map[B](f: A => B): Either[E, B] = this match { case Left(e) => Left(e) case Right(a) => Right(f(a)) } def flatMap[EE >: E, B](f: A => Eithe... 阅读全文
posted @ 2016-04-25 20:35 JonkeyGuan 阅读(395) 评论(0) 推荐(0) 编辑

Option Traverse

摘要: package option object Traverse { def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = a match { case Nil => Some(Nil) case h :: t => Map2.map2(f(h), traverse(t)(f))(_ :... 阅读全文
posted @ 2016-04-24 09:59 JonkeyGuan 阅读(163) 评论(0) 推荐(0) 编辑

Option Sequence

摘要: package option object Sequence { def sequence[A](a: List[Option[A]]): Option[List[A]] = a match { case Nil => Some(Nil) case h :: t => h.flatMap(hh => sequence(t) map (hh :: _)) } ... 阅读全文
posted @ 2016-04-24 09:58 JonkeyGuan 阅读(206) 评论(0) 推荐(0) 编辑

Option Map2

摘要: package option object Map2 { def map2[A, B, C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = a flatMap (a1 => b map (b1 => f(a1, b1))) def main(args: Array[String]): Unit = { va... 阅读全文
posted @ 2016-04-24 09:57 JonkeyGuan 阅读(261) 评论(0) 推荐(0) 编辑

Option Variance

摘要: package option object Variance { def mean(xs: Seq[Double]): Option[Double] = { if (xs.isEmpty) None else Some(xs.sum / xs.length) } def variance(xs: Seq[Double]): Option[Double] = m... 阅读全文
posted @ 2016-04-24 09:56 JonkeyGuan 阅读(194) 评论(0) 推荐(0) 编辑

Option

摘要: package option sealed trait Option[+A] { def map[B](f: A => B): Option[B] = this match { case None => None case Some(a) => Some(f(a)) } def flatMap[B](f: A => Option[B]): Option[B... 阅读全文
posted @ 2016-04-24 09:54 JonkeyGuan 阅读(170) 评论(0) 推荐(0) 编辑

Tree MapByFold

摘要: package tree object MapByFold { def map[A, B](t: Tree[A])(f: A => B): Tree[B] = Fold.fold(t)(a => Leaf(f(a)): Tree[B])(Branch(_, _)) def main(args: Array[String]): Unit = { val tree = Bran... 阅读全文
posted @ 2016-04-23 16:00 JonkeyGuan 阅读(149) 评论(0) 推荐(0) 编辑

Tree DepthByFold

摘要: package tree object DepthByFold { def depth[A](t: Tree[A]): Int = Fold.fold(t)(_ => 0)((a, b) => (a max b) + 1) def main(args: Array[String]): Unit = { val tree = Branch(Leaf(1), Branch(Br... 阅读全文
posted @ 2016-04-23 15:59 JonkeyGuan 阅读(155) 评论(0) 推荐(0) 编辑

Tree MaximumByFold

摘要: package tree object MaximumByFold { def maximum(t: Tree[Int]): Int = Fold.fold(t)(a => a)(_ max _) def main(args: Array[String]): Unit = { val tree = Branch(Leaf(1), Branch(Branch(Branch(L... 阅读全文
posted @ 2016-04-23 15:58 JonkeyGuan 阅读(205) 评论(0) 推荐(0) 编辑

Tree SizeByFold

摘要: package tree object SizeByFold { def size[A](t: Tree[A]) = Fold.fold(t)(_ => 1)(_ + _ + 1) def main(args: Array[String]): Unit = { val tree = Branch(Leaf(1), Branch(Branch(Branch(Leaf(3),... 阅读全文
posted @ 2016-04-23 15:58 JonkeyGuan 阅读(156) 评论(0) 推荐(0) 编辑

Tree Fold

摘要: package tree object Fold { def fold[A, B](t: Tree[A])(f: A => B)(g: (B, B) => B): B = t match { case Leaf(a) => f(a) case Branch(l, r) => g(fold(l)(f)(g), fold(r)(f)(g)) } def m... 阅读全文
posted @ 2016-04-23 15:57 JonkeyGuan 阅读(242) 评论(0) 推荐(0) 编辑

Tree Map

摘要: package tree object Map { def map[A, B](t: Tree[A])(f: A => B): Tree[B] = t match { case Leaf(a) => Leaf(f(a)) case Branch(l, r) => Branch(map(l)(f), map(r)(f)) } def main(args:... 阅读全文
posted @ 2016-04-23 15:56 JonkeyGuan 阅读(288) 评论(0) 推荐(0) 编辑

Tree Depth

摘要: package tree object Depth { def depth[A](t: Tree[A]): Int = t match { case Leaf(_) => 0 case Branch(l, r) => (depth(l) max depth(r)) + 1 } def main(args: Array[String]): Unit = ... 阅读全文
posted @ 2016-04-23 15:55 JonkeyGuan 阅读(365) 评论(0) 推荐(0) 编辑

Tree Maximum

摘要: package tree object Maximum { def maximum(t: Tree[Int]): Int = t match { case Leaf(n) => n case Branch(l, r) => maximum(l) max maximum(r) } def main(args: Array[String]): Unit =... 阅读全文
posted @ 2016-04-23 15:54 JonkeyGuan 阅读(150) 评论(0) 推荐(0) 编辑

Tree Size

摘要: package tree object Size { def size[A](t: Tree[A]): Int = t match { case Leaf(_) => 1 case Branch(l, r) => 1 + size(l) + size(r) } def main(args: Array[String]): Unit = { va... 阅读全文
posted @ 2016-04-23 15:52 JonkeyGuan 阅读(394) 评论(0) 推荐(0) 编辑

Tree

摘要: package tree sealed trait Tree[+A] case class Leaf[A](value: A) extends Tree[A] case class Branch[A](left: Tree[A], right: Tree[A]) extends Tree[A] 阅读全文
posted @ 2016-04-23 15:51 JonkeyGuan 阅读(201) 评论(0) 推荐(0) 编辑

List HasSubsequence

摘要: object HasSubsequence { def startWith[T](a: List[T], b: List[T]): Boolean = (a, b) match { case (_, Nil) => true case (h1 :: t1, h2 :: t2) if (h1 == h2) => startW... 阅读全文
posted @ 2016-04-23 08:28 JonkeyGuan 阅读(212) 评论(0) 推荐(0) 编辑

List ZipWith

摘要: object ZipWith { def zipWith[T](l1: List[T], l2: List[T])(f: (T, T) => T): List[T] = (l1, l2) match { case (_, Nil) => Nil case (Nil, _) => Nil case (h1 :: t1, ... 阅读全文
posted @ 2016-04-23 08:27 JonkeyGuan 阅读(189) 评论(0) 推荐(0) 编辑

List AddList

摘要: object AddList { def add(a: List[Int], b: List[Int]): List[Int] = (a, b) match { case (_, Nil) => Nil case (Nil, _) => Nil case (h1 :: t1, h2 :: t2) => (h1 + h2... 阅读全文
posted @ 2016-04-23 08:26 JonkeyGuan 阅读(253) 评论(0) 推荐(0) 编辑

List FlatMap

摘要: object FlatMap { import FoldRight._ import AppendByFoldLeft._ def flatMap[A, B](as: List[A])(f: A => List[B]): List[B] = foldRight(as, Nil: List[B])((a: A, b: List[B]) => append(f(a), b)) ... 阅读全文
posted @ 2016-04-23 08:25 JonkeyGuan 阅读(526) 评论(0) 推荐(0) 编辑

List Map

摘要: object Map { def map[A, B](as: List[A])(f: A => B): List[B] = FoldRight.foldRight(as, Nil: List[B])((a: A, b: List[B]) => f(a) :: b) def main(args: Array[String]): Unit = { val l = List(1.0... 阅读全文
posted @ 2016-04-23 08:24 JonkeyGuan 阅读(227) 评论(0) 推荐(0) 编辑

List Filter

摘要: object Filter { def filter[A](as: List[A])(f: A => Boolean): List[A] = FoldRight.foldRight(as, Nil: List[A])((a: A, b: List[A]) => if (f(a)) a :: b else b) def main(args: Array[String]): Unit =... 阅读全文
posted @ 2016-04-23 08:24 JonkeyGuan 阅读(287) 评论(0) 推荐(0) 编辑

List ToString

摘要: object ToString { def toString(l: List[Double]): List[String] = FoldRight.foldRight(l, Nil: List[String])((a: Double, b: List[String]) => a.toString() :: b) def main(args: Array[String]): Unit ... 阅读全文
posted @ 2016-04-23 08:23 JonkeyGuan 阅读(325) 评论(0) 推荐(0) 编辑

List Add1

摘要: object Add1 { def add1(l: List[Int]): List[Int] = FoldRight.foldRight(l, Nil: List[Int])((a: Int, b: List[Int]) => a + 1 :: b) def main(args: Array[String]): Unit = { println(add1(List(1, ... 阅读全文
posted @ 2016-04-23 08:22 JonkeyGuan 阅读(177) 评论(0) 推荐(0) 编辑

List Concatenate

摘要: object Concatenate { def concat[T](l: List[List[T]]): List[T] = FoldLeft.foldLeft(l, Nil: List[T])(AppendByFoldLeft.append) def main(args: Array[String]): Unit = { println(concat(List(List... 阅读全文
posted @ 2016-04-22 20:27 JonkeyGuan 阅读(298) 评论(0) 推荐(0) 编辑

List AppendByFoldLeft

摘要: object AppendByFoldLeft { def append[T](l: List[T], z: List[T]): List[T] = FoldLeft.foldLeft(Reverse.reverse(l), z)((a: List[T], b: T) => b :: a) def main(args: Array[String]): Unit = { pr... 阅读全文
posted @ 2016-04-21 22:04 JonkeyGuan 阅读(212) 评论(0) 推荐(0) 编辑

List AppendByFoldRight

摘要: object AppendByFoldRight { def append[T](l: List[T], z: List[T]): List[T] = FoldRight.foldRight(l, z)(_ :: _) def main(args: Array[String]): Unit = { println(append(List("1", "2", "3"), Li... 阅读全文
posted @ 2016-04-21 22:03 JonkeyGuan 阅读(213) 评论(0) 推荐(0) 编辑

List FoldRightByFoldLeft

摘要: object FoldRightByFoldLeft { def foldRight[A, B](as: List[A], z: B)(f: (A, B) => B): B = FoldLeft.foldLeft(Reverse.reverse(as), z)((b: B, a: A) => f(a, b)) def main(args: Array[String]): Unit =... 阅读全文
posted @ 2016-04-20 22:02 JonkeyGuan 阅读(177) 评论(0) 推荐(0) 编辑

List FoldLeftByFoldRight

摘要: object FoldLeftByFoldRight { def foldLeft[A, B](l: List[A], z: B)(f: (B, A) => B): B = FoldRight.foldRight(Reverse.reverse(l), z)((a: A, b: B) => f(b, a)) def main(args: Array[String]): Unit = ... 阅读全文
posted @ 2016-04-20 22:01 JonkeyGuan 阅读(227) 评论(0) 推荐(0) 编辑

List Reverse

摘要: object Reverse { def reverse[T](l: List[T]): List[T] = FoldLeft.foldLeft(l, Nil: List[T])((b, a) => a :: b) def main(args: Array[String]): Unit = { println(reverse(Nil)) println(revers... 阅读全文
posted @ 2016-04-20 22:00 JonkeyGuan 阅读(190) 评论(0) 推荐(0) 编辑

List LengthByFoldLeft

摘要: object LengthByFoldLeft { def length[T](as: List[T]): Int = FoldLeft.foldLeft(as, 0)((acc, _) => acc + 1) def main(args: Array[String]): Unit = { println(length(Nil)) println(length(Lis... 阅读全文
posted @ 2016-04-19 22:26 JonkeyGuan 阅读(183) 评论(0) 推荐(0) 编辑

List ProductByFoldLeft

摘要: object ProductByFoldLeft { def product(as: List[Double]): Double = FoldLeft.foldLeft(as, 1.0)(_ * _) def main(args: Array[String]): Unit = { println(product(List(1.0, 2, 3, 4, 5))) prin... 阅读全文
posted @ 2016-04-19 22:18 JonkeyGuan 阅读(204) 评论(0) 推荐(0) 编辑

List SumByFoldLeft

摘要: object Sum { def sum(as: List[Int]): Int = FoldLeft.foldLeft(as, 0)(_ + _) def main(args: Array[String]): Unit = { println(sum(Nil)) println(sum(List(2))) println(sum(List(1, 2, 3, ... 阅读全文
posted @ 2016-04-19 22:11 JonkeyGuan 阅读(119) 评论(0) 推荐(0) 编辑

List FoldLeft

摘要: object FoldLeft { def foldLeft[A, B](as: List[A], z: B)(f: (B, A) => B): B = as match { case Nil => z case h :: t => foldLeft(t, f(z, h))(f) } def main(args: Array[String... 阅读全文
posted @ 2016-04-19 21:54 JonkeyGuan 阅读(167) 评论(0) 推荐(0) 编辑

List Length

摘要: object Length { def length[T](ls: List[T]): Int = { def loop(xs: List[T], acc: Int): Int = xs match { case Nil => acc case h :: t => loop(t, acc + 1) } loop(... 阅读全文
posted @ 2016-04-19 21:43 JonkeyGuan 阅读(397) 评论(0) 推荐(0) 编辑

List FoldRight

摘要: object FoldRight { def foldRight[A, B](as: List[A], z: B)(f: (A, B) => B): B = as match { case Nil => z case ::(x, xs) => f(x, foldRight(xs, z)(f)) } def main(args: Ar... 阅读全文
posted @ 2016-04-19 21:28 JonkeyGuan 阅读(166) 评论(0) 推荐(0) 编辑