This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
We going to create random circles inside a circle. (This is called "generative art". If you own a beret now is a good time to put it on.) | |
We're going to use `map` and a new tool, `flatMap`, in a different context. The idea is to broaden how we think about these tools. | |
Step 1. | |
- How could we create a point that is randomly located within a circle? There are many ways to do this. Think of one. | |
- Use the following methods to create random numbers (you'll need to `import doodle.random._`) | |
`Random.double` | |
`Random.natural` | |
`Random.int` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Functions | |
import cats.Monoid | |
import cats.implicits._ | |
implicit object pointInstance extends Monoid[Point] { | |
def empty = Point.zero | |
def combine(x: Point, y: Point): Point = | |
Point(x.x + y.x, x.y + y.y) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Push vs pull evaluation | |
// Interpreters | |
// Reification | |
// Generalised algebraic data types | |
import java.util.concurrent.ArrayBlockingQueue | |
import scalaz.Applicative | |
import scalaz.syntax.applicative._ // for |@| | |
final case class Sink[A,B](source: EventStream[A], initialValue: B, f: (B, A) => B) { | |
def run: B = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz.stream._ | |
import scalaz.concurrent.Task | |
import scalaz.stream.async | |
object ChatBot { | |
val queue = async.boundedQueue[String](10) | |
val reader = | |
new Thread { | |
override def run(): Unit = { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import scalaz.Monoid | |
import scalaz.syntax.monoid._ | |
import scalaz.syntax.traverse._ | |
import scalaz.std.map._ | |
import scalaz.std.anyVal._ | |
import scalaz.std.string._ | |
import scalaz.std.iterable._ | |
import scala.language.higherKinds | |
/* | |
/* A Bounded Semi-lattice is a Monoid that is commutative and idempotent */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
object ReaderExample { | |
import scalaz.Monad | |
import scalaz.syntax.monad._ | |
type AccountId = Int | |
type Account = Int | |
type BetId = Int | |
type Bet = Int | |
final case class Config( |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package underscoreio | |
package enum | |
import shapeless._ | |
import shapeless.labelled._ | |
import shapeless.syntax.singleton._ | |
// The intention of this code is to extract the names of the types at the leaves of an algebraic data type. | |
// E.g. for Color at the bottom of this file, | |
// |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package nl.mdj | |
import cats.arrow.NaturalTransformation | |
import cats.functor.Invariant | |
import cats._ | |
import cats.syntax.all._ | |
import scala.language.higherKinds | |
sealed abstract class FreeInvariant[F[_], A] extends Product with Serializable { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Much software claims to scale horizontally | |
But don't take to these claims too wantonly | |
Too often things fail | |
To properly scale | |
And marketers talk irresponsibily |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
sealed trait Observer[A] { | |
def observe(in :A): Unit = | |
this match { | |
case Map(f, o) => | |
// This is completely unsound. b should be of type B, which we don't | |
// have available at this point. The compiler allows us to insert | |
// anything here, causing a runtime crash. | |
val b = () | |
o.observe(b) | |
case Sink(f) => |