fpinscala
7a43335a0467
Merge pull request #501 from guardian/modernise-project
RĂșnar
3 months ago
Modernise project build

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
val commonSettings = Seq( scalaVersion := "2.12.1" ) lazy val root = (project in file(".")) .aggregate(exercises, answers) .settings(commonSettings) .settings( name := "fpinscala" ) lazy val exercises = (project in file("exercises")) .settings(commonSettings) .settings( name := "exercises" ) lazy val answers = (project in file("answers")) .settings(commonSettings) .settings( name := "answers" )

48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
def product2(ns: List[Double]) = foldRight(ns, 1.0)(_ * _) // `_ * _` is more concise notation for `(x,y) => x * y`; see sidebar
def tail[A](l: List[A]): List[A] = sys.error("todo")
def tail[A](l: List[A]): List[A] = ???
def setHead[A](l: List[A], h: A): List[A] = sys.error("todo")
def setHead[A](l: List[A], h: A): List[A] = ???
def drop[A](l: List[A], n: Int): List[A] = sys.error("todo")
def drop[A](l: List[A], n: Int): List[A] = ???
def dropWhile[A](l: List[A], f: A => Boolean): List[A] = sys.error("todo")
def dropWhile[A](l: List[A], f: A => Boolean): List[A] = ???
def init[A](l: List[A]): List[A] = sys.error("todo")
def init[A](l: List[A]): List[A] = ???
def length[A](l: List[A]): Int = sys.error("todo")
def length[A](l: List[A]): Int = ???
def foldLeft[A,B](l: List[A], z: B)(f: (B, A) => B): B = sys.error("todo")
def foldLeft[A,B](l: List[A], z: B)(f: (B, A) => B): B = ???
def map[A,B](l: List[A])(f: A => B): List[B] = sys.error("todo")
def map[A,B](l: List[A])(f: A => B): List[B] = ???
}

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
import scala.{Option => _, Either => _, Left => _, Right => _, _} // hide std library `Option` and `Either`, since we are writing our own in this chapter sealed trait Either[+E,+A] {
def map[B](f: A => B): Either[E, B] = sys.error("todo")
def map[B](f: A => B): Either[E, B] = ???
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] = sys.error("todo")
def flatMap[EE >: E, B](f: A => Either[EE, B]): Either[EE, B] = ???
def orElse[EE >: E, B >: A](b: => Either[EE, B]): Either[EE, B] = sys.error("todo")
def orElse[EE >: E, B >: A](b: => Either[EE, B]): Either[EE, B] = ???
def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] = sys.error("todo")
def map2[EE >: E, B, C](b: Either[EE, B])(f: (A, B) => C): Either[EE, C] = ???
} case class Left[+E](get: E) extends Either[E,Nothing] case class Right[+A](get: A) extends Either[Nothing,A] object Either {
def traverse[E,A,B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = sys.error("todo")
def traverse[E,A,B](es: List[A])(f: A => Either[E, B]): Either[E, List[B]] = ???
def sequence[E,A](es: List[Either[E,A]]): Either[E,List[A]] = sys.error("todo")
def sequence[E,A](es: List[Either[E,A]]): Either[E,List[A]] = ???
def mean(xs: IndexedSeq[Double]): Either[String, Double] = if (xs.isEmpty) Left("mean of empty list!") else

2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
import scala.{Option => _, Some => _, Either => _, _} // hide std library `Option`, `Some` and `Either`, since we are writing our own in this chapter sealed trait Option[+A] {
def map[B](f: A => B): Option[B] = sys.error("todo")
def map[B](f: A => B): Option[B] = ???
def getOrElse[B>:A](default: => B): B = sys.error("todo")
def getOrElse[B>:A](default: => B): B = ???
def flatMap[B](f: A => Option[B]): Option[B] = sys.error("todo")
def flatMap[B](f: A => Option[B]): Option[B] = ???
def orElse[B>:A](ob: => Option[B]): Option[B] = sys.error("todo")
def orElse[B>:A](ob: => Option[B]): Option[B] = ???
def filter(f: A => Boolean): Option[A] = sys.error("todo")
def filter(f: A => Boolean): Option[A] = ???
} case class Some[+A](get: A) extends Option[A] case object None extends Option[Nothing] object Option {
...
36 37 38 39 40 41 42 43 44 45 46 47 48 36 37 38 39 40 41 42 43 44 45 46 47 48
} def mean(xs: Seq[Double]): Option[Double] = if (xs.isEmpty) None else Some(xs.sum / xs.length)
def variance(xs: Seq[Double]): Option[Double] = sys.error("todo")
def variance(xs: Seq[Double]): Option[Double] = ???
def map2[A,B,C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = sys.error("todo")
def map2[A,B,C](a: Option[A], b: Option[B])(f: (A, B) => C): Option[C] = ???
def sequence[A](a: List[Option[A]]): Option[List[A]] = sys.error("todo")
def sequence[A](a: List[Option[A]]): Option[List[A]] = ???
def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = sys.error("todo")
def traverse[A, B](a: List[A])(f: A => Option[B]): Option[List[B]] = ???
}

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
@annotation.tailrec final def find(f: A => Boolean): Option[A] = this match { case Empty => None case Cons(h, t) => if (f(h())) Some(h()) else t().find(f) }
def take(n: Int): Stream[A] = sys.error("todo")
def take(n: Int): Stream[A] = ???
def drop(n: Int): Stream[A] = sys.error("todo")
def drop(n: Int): Stream[A] = ???
def takeWhile(p: A => Boolean): Stream[A] = sys.error("todo")
def takeWhile(p: A => Boolean): Stream[A] = ???
def forAll(p: A => Boolean): Boolean = sys.error("todo")
def forAll(p: A => Boolean): Boolean = ???
def headOption: Option[A] = sys.error("todo")
def headOption: Option[A] = ???
// 5.7 map, filter, append, flatmap using foldRight. Part of the exercise is // writing your own function signatures.
def startsWith[B](s: Stream[B]): Boolean = sys.error("todo")
def startsWith[B](s: Stream[B]): Boolean = ???
} case object Empty extends Stream[Nothing] case class Cons[+A](h: () => A, t: () => Stream[A]) extends Stream[A] object Stream {
...
47 48 49 50 51 52 53 54 55 47 48 49 50 51 52 53 54 55
def apply[A](as: A*): Stream[A] = if (as.isEmpty) empty else cons(as.head, apply(as.tail: _*)) val ones: Stream[Int] = Stream.cons(1, ones)
def from(n: Int): Stream[Int] = sys.error("todo")
def from(n: Int): Stream[Int] = ???
def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = sys.error("todo")
def unfold[A, S](z: S)(f: S => Option[(A, S)]): Stream[A] = ???
}

19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
def listMonoid[A] = new Monoid[List[A]] { def op(a1: List[A], a2: List[A]) = a1 ++ a2 val zero = Nil }
val intAddition: Monoid[Int] = sys.error("todo")
val intAddition: Monoid[Int] = ???
val intMultiplication: Monoid[Int] = sys.error("todo")
val intMultiplication: Monoid[Int] = ???
val booleanOr: Monoid[Boolean] = sys.error("todo")
val booleanOr: Monoid[Boolean] = ???
val booleanAnd: Monoid[Boolean] = sys.error("todo")
val booleanAnd: Monoid[Boolean] = ???
def optionMonoid[A]: Monoid[Option[A]] = sys.error("todo")
def optionMonoid[A]: Monoid[Option[A]] = ???
def endoMonoid[A]: Monoid[A => A] = sys.error("todo")
def endoMonoid[A]: Monoid[A => A] = ???
// TODO: Placeholder for `Prop`. Remove once you have implemented the `Prop` // data type from Part 2. trait Prop {} // TODO: Placeholder for `Gen`. Remove once you have implemented the `Gen` // data type from Part 2. import fpinscala.testing._ import Prop._
def monoidLaws[A](m: Monoid[A], gen: Gen[A]): Prop = sys.error("todo")
def monoidLaws[A](m: Monoid[A], gen: Gen[A]): Prop = ???
def trimMonoid(s: String): Monoid[String] = sys.error("todo")
def trimMonoid(s: String): Monoid[String] = ???
def concatenate[A](as: List[A], m: Monoid[A]): A =
sys.error("todo")
???
def foldMap[A, B](as: List[A], m: Monoid[B])(f: A => B): B =
sys.error("todo")
???
def foldRight[A, B](as: List[A])(z: B)(f: (A, B) => B): B =
sys.error("todo")
???
def foldLeft[A, B](as: List[A])(z: B)(f: (B, A) => B): B =
sys.error("todo")
???
def foldMapV[A, B](as: IndexedSeq[A], m: Monoid[B])(f: A => B): B =
sys.error("todo")
???
def ordered(ints: IndexedSeq[Int]): Boolean =
sys.error("todo")
???
sealed trait WC case class Stub(chars: String) extends WC case class Part(lStub: String, words: Int, rStub: String) extends WC def par[A](m: Monoid[A]): Monoid[Par[A]] =
sys.error("todo")
???
def parFoldMap[A,B](v: IndexedSeq[A], m: Monoid[B])(f: A => B): Par[B] =
sys.error("todo")
???
val wcMonoid: Monoid[WC] = sys.error("todo")
val wcMonoid: Monoid[WC] = ???
def count(s: String): Int = sys.error("todo")
def count(s: String): Int = ???
def productMonoid[A,B](A: Monoid[A], B: Monoid[B]): Monoid[(A, B)] =
sys.error("todo")
???
def functionMonoid[A,B](B: Monoid[B]): Monoid[A => B] =
sys.error("todo")
???
def mapMergeMonoid[K,V](V: Monoid[V]): Monoid[Map[K, V]] =
sys.error("todo")
???
def bag[A](as: IndexedSeq[A]): Map[A, Int] =
sys.error("todo")
???
} trait Foldable[F[_]] { import Monoid._ def foldRight[A, B](as: F[A])(z: B)(f: (A, B) => B): B =
sys.error("todo")
???
def foldLeft[A, B](as: F[A])(z: B)(f: (B, A) => B): B =
sys.error("todo")
???
def foldMap[A, B](as: F[A])(f: A => B)(mb: Monoid[B]): B =
sys.error("todo")
???
def concatenate[A](as: F[A])(m: Monoid[A]): A =
sys.error("todo")
???
def toList[A](as: F[A]): List[A] =
sys.error("todo")
???
} object ListFoldable extends Foldable[List] { override def foldRight[A, B](as: List[A])(z: B)(f: (A, B) => B) =
sys.error("todo")
???
override def foldLeft[A, B](as: List[A])(z: B)(f: (B, A) => B) =
sys.error("todo")
???
override def foldMap[A, B](as: List[A])(f: A => B)(mb: Monoid[B]): B =
sys.error("todo")
???
} object IndexedSeqFoldable extends Foldable[IndexedSeq] { override def foldRight[A, B](as: IndexedSeq[A])(z: B)(f: (A, B) => B) =
sys.error("todo")
???
override def foldLeft[A, B](as: IndexedSeq[A])(z: B)(f: (B, A) => B) =
sys.error("todo")
???
override def foldMap[A, B](as: IndexedSeq[A])(f: A => B)(mb: Monoid[B]): B =
sys.error("todo")
???
} object StreamFoldable extends Foldable[Stream] { override def foldRight[A, B](as: Stream[A])(z: B)(f: (A, B) => B) =
sys.error("todo")
???
override def foldLeft[A, B](as: Stream[A])(z: B)(f: (B, A) => B) =
sys.error("todo")
???
} 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] object TreeFoldable extends Foldable[Tree] { override def foldMap[A, B](as: Tree[A])(f: A => B)(mb: Monoid[B]): B =
sys.error("todo")
???
override def foldLeft[A, B](as: Tree[A])(z: B)(f: (B, A) => B) =
sys.error("todo")
???
override def foldRight[A, B](as: Tree[A])(z: B)(f: (A, B) => B) =
sys.error("todo")
???
} object OptionFoldable extends Foldable[Option] { override def foldMap[A, B](as: Option[A])(f: A => B)(mb: Monoid[B]): B =
sys.error("todo")
???
override def foldLeft[A, B](as: Option[A])(z: B)(f: (B, A) => B) =
sys.error("todo")
???
override def foldRight[A, B](as: Option[A])(z: B)(f: (A, B) => B) =
sys.error("todo")
???
}

49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
def flatMap[A,B](f: Rand[A])(g: A => Rand[B]): Rand[B] = ??? } case class State[S,+A](run: S => (A, S)) { def map[B](f: A => B): State[S, B] =
sys.error("todo")
???
def map2[B,C](sb: State[S, B])(f: (A, B) => C): State[S, C] =
sys.error("todo")
???
def flatMap[B](f: A => State[S, B]): State[S, B] =
sys.error("todo")
???
} sealed trait Input case object Coin extends Input case object Turn extends Input

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
import sbt._ import Keys._ object FPInScalaBuild extends Build { val opts = Project.defaultSettings ++ Seq( scalaVersion := "2.11.7", resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/" ) lazy val root = Project(id = "fpinscala", base = file("."), settings = opts ++ Seq( onLoadMessage ~= (_ + nio2check()) )) aggregate (chapterCode, exercises, answers) lazy val chapterCode = Project(id = "chapter-code", base = file("chaptercode"), settings = opts) lazy val exercises = Project(id = "exercises", base = file("exercises"), settings = opts) lazy val answers = Project(id = "answers", base = file("answers"), settings = opts) def nio2check(): String = { val cls = "java.nio.channels.AsynchronousFileChannel" try {Class.forName(cls); ""} catch {case _: ClassNotFoundException => ("\nWARNING: JSR-203 \"NIO.2\" (" + cls + ") not found.\n" + "You are probably running Java < 1.7; answers will not compile.\n" + "You seem to be running " + System.getProperty("java.version") + ".\n" + "Try `project exercises' before compile, or upgrading your JDK.") } } }

1
sbt.version=0.13.13

1 2 1 2 3
#!/bin/bash java -Xmx1024M -Xss8m -XX:PermSize=300m -jar `dirname $0`/sbt-launch.jar "$@"
#!/bin/bash SBT_OPTS="-Xms512M -Xmx1536M -Xss1M -XX:+CMSClassUnloadingEnabled -XX:MaxPermSize=256M" java $SBT_OPTS -jar `dirname $0`/sbt-launch.jar "$@"

About FluentSend Feedback