Skip to content
Snippets Groups Projects
Commit 1bc4881e authored by Alexander Gehrke's avatar Alexander Gehrke
Browse files

[09] update for 2022

parent 1abf2157
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,9 @@ usually marked with `???`. Lectures not listed here are not migrated to Scala
| 4: Laziness | [`laziness`](src/main/scala/laziness/) | `testOnly laziness.*`
| 5: Algebras, Laws and monoids | [`algebra`](src/main/scala/algebra/) | `testOnly algebra.*`
| 6: Foldables and Functors | [`foldfunc`](src/main/scala/foldfunc/) | `testOnly foldfunc.*`
| 7: Monads | [`monads`](src/main/scala/monads/) | `testOnly monads.*`
| 8: Applicative Functors | [`applicative`](src/main/scala/applicative/) | `testOnly applicative.*`
| 9: An algebraic View on Monads | [`readerwriter`](src/main/scala/readerwriter/) | `testOnly readerwriter.*`
## Usage tips:
To keep your local solutions to the exercises when pulling from the repository,
......
package readerwriter
import readerwriter.internal.State, State._
import util._
import readerwriter.internal.State, State.*
object Randoms:
def threeInts: State[RNG, (Int, Int, Int)] = ???
/*@formatter:off*/
def randomInt: State[RNG, Int] = for
rng <- get[RNG]
(rng2, i) = rng.nextInt
_ <- set(rng2)
yield i
/*@formatter:on*/
def nonNegativeInt: State[RNG, Int] = for
i <- randomInt
......@@ -23,6 +19,8 @@ trait RNG:
def nextInt: (RNG, Int)
case class Simple(seed: Long) extends RNG:
/** Basically the same formula as in java.util.Random
* See https://docs.oracle.com/en/java/javase/11/docs/api/java.base/java/util/Random.html#next(int) */
def nextInt: (RNG, Int) =
val newSeed = (seed * 0x5DEECE66DL + 0xBL) & 0xFFFFFFFFFFFFL
val nextRNG = Simple(newSeed)
......
......@@ -2,8 +2,7 @@ package readerwriter
import java.time.LocalDate
import readerwriter.internal.Reader, Reader._
import util._
import readerwriter.internal.Reader, Reader.*
final case class Request(
user: Option[String],
......
package readerwriter
import readerwriter.internal.Writer, Writer._
import util._
import readerwriter.internal.Writer, Writer.{*, given}
object Writers:
/* everything required to use tell and the monad operations
......@@ -13,7 +11,6 @@ object Writers:
def collatzSearch(start: Int, limit: Int): Writer[List[String], Int] = ???
object CollatzWithoutWriter:
def collatzDepth(n: Int): (List[String], Int) =
if n == 1 then
......
......@@ -2,7 +2,7 @@ package readerwriter.internal
import applicative.Monad
case class Reader[R, A](run: R => A)
case class Reader[-R, A](run: R => A)
object Reader:
def ask[R]: Reader[R, R] = Reader(x => x)
......
......@@ -8,6 +8,9 @@ import applicative.Monad
case class State[S, A] private (run: S => (S, A))
case object State:
def get[S]: State[S, S] = State(s => (s, s))
def set[S](s: S): State[S, Unit] = State(_ => (s, ()))
given stateMonad[S]: Monad[[a] =>> State[S, a]] with
def pure[A](a: A): State[S, A] = State(s => (s, a))
extension [A](fa: State[S, A])
......@@ -15,6 +18,3 @@ case object State:
val (s1, a) = fa.run(s)
f(a).run(s1)
)
def get[S]: State[S, S] = State(s => (s, s))
def set[S](s: S): State[S, Unit] = State(_ => (s, ()))
......@@ -6,6 +6,7 @@ import applicative.Monad
final case class Writer[L, A](v: (L, A))
object Writer:
given [A]: Monoid[List[A]] with
def zero: List[Nothing] = List.empty
def combine(l1: List[A], l2: List[A]): List[A] = l1 ++ l2
......
import applicative.Monad
package object util:
implicit class MonadOps[F[_] : Monad, A](fa: F[A]):
def flatMap[B](f: A => F[B]): F[B] = implicitly[Monad[F]].flatMap(fa)(f)
def map[B](f: A => B): F[B] = implicitly[Monad[F]].map(fa)(f)
......@@ -27,6 +27,6 @@ class ReaderSpec extends AnyFlatSpec with Matchers with AppendedClues with Pendi
}
"sayBye" should "contain the correct user name and date" in {
Readers.sayBye.run(exampleRequest) shouldBe "Goodbye Mister X, today is 2019-06-26"
Readers.sayBye.run(exampleRequest) shouldBe "Goodbye Mister X, now is 2019-06-26"
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment