Scala at the Sea: 8 Jan 2019
Adam Rosien @arosien Inner Product LLC
Can you read this? How about you over there? :thumbs_up:
What is type-level programming?
@startuml | |
concise "Client" as Client | |
concise "Server" as Server | |
concise "Response freshness" as Cache | |
Server is idle | |
Client is idle | |
@Client | |
0 is send |
import cats._ | |
import cats.implicits._ | |
/* | |
Box a: forall b. (a -> b) -> b | |
Codensity f a: forall b. (a -> f b) -> f b | |
Yoneda f a: forall b. (a -> b) -> f b | |
https://stackoverflow.com/questions/45287954/is-having-a-a-b-b-equivalent-to-having-an-a | |
*/ |
import cats._ | |
import cats.data._ | |
import cats.implicits._ | |
object KleisliReader { | |
val k = Kleisli[Id, Int, String](i => (i + 1).toString) | |
Contravariant[Reader[?, String]] // not found | |
Contravariant[Kleisli[Id, ?, String]] // not found |
Adam Rosien @arosien Inner Product LLC
Can you read this? How about you over there? :thumbs_up:
What is type-level programming?
import $ivy.`org.typelevel::cats-core:1.4.0` | |
import cats._ | |
import cats.data._ | |
import cats.implicits._ | |
// compose "regular" functions: | |
def a(s: String): Int = s.length | |
def b(i: Int): Int = i + 1 | |
def c(i: Int): String = i.toString + "!" |
/* | |
* Algebraic Data Types | |
*/ | |
// A has a B and C | |
case class A(b: B, c: C) | |
// A is a B or C | |
sealed trait A | |
case class B() extends A |
@gfixler, free monoid is the easiest and helps generalize the whole "free" thing | |
let's make a monoid that is a monoid for *anything* | |
but wait, that doesn't sound possible, everything that can be a monoid has its own rule, that's the whole point | |
but if you wanted to make a monoid for anything, you can just be really lazy, and instead of actually adding things together, you just collect all the things you want to add | |
"you want to add an A and an A, sure, the answer is: here's your two A's" HA HA HA free monoids rule |
// JsonFormat is from spray-json, reproduced here | |
trait JsonFormat[A] { | |
def write(a: A): JsValue | |
def read(js: JsValue): A | |
} | |
implicit class FormatInvariantFunctor[A](format: JsonFormat[A]) { | |
def xmap[B](f: A => B, g: B => A): JsonFormat[B] = | |
new JsonFormat[B] { | |
def write(b: B): JsValue = format.write(g(b)) |
package net.rosien.lattice | |
import scalaz._ | |
import scalaz.syntax.order._ | |
/* http://db.cs.berkeley.edu/papers/UCB-lattice-tr.pdf */ | |
sealed trait LValue[A] { | |
def value: A | |
} |