A law is a group of two or more expressions which are required to be the same. The expressions will usually involve one or more typed holes ("inputs") which vary.
Some examples:
x.map(id) === x
This is a proposal for a lightning talk at the Reactive 2016 conference.
NOTE: If you like this, star ⭐ the Gist - the amount of stars decides whether it makes the cut! You could also Retweet if you want :)
JavaScript is a dynamic language, and there's nothing wrong with that. It allows quick iteration and lowers barriers. However, sometimes some compile-time type checking is just what you need to keep your code in line and give yourself the confidence to build bigger and faster. Flow gives the best of both worlds. You can have normal JavaScript but you can also add types where they're helpful, and it adds zero cost at runtime. In this talk I'll show Flow as it applies to a Redux & React codebase.
import cats._ | |
import cats.data._ | |
import shapeless._ | |
import shapeless.ops.hlist.Selector | |
implicit class ReaderOps[A, B, L <: HList](f: Reader[A, B]) { | |
def liftD[AA](implicit ga: Generic.Aux[AA, L], sel: Selector[L, A]): Reader[AA, B] = | |
f.local[AA](aa => sel.apply(ga.to(aa))) |
import shapeless._ | |
import shapeless.labelled._ | |
import shapeless.ops.record._ | |
import scala.annotation.implicitNotFound | |
@implicitNotFound("Cannot prove that ${A} has an 'id: Int' field.") | |
trait HasId[A] { | |
def apply(a: A): Int | |
} |
scala> import scala.tools.nsc._, reporters._; val reporter = new StoreReporter; val settings = new Settings(); settings.processArgumentString("-usejavacp -Ystop-after:refchecks"); val global = new Global(settings, reporter) | |
import scala.tools.nsc._ | |
import reporters._ | |
reporter: scala.tools.nsc.reporters.StoreReporter = scala.tools.nsc.reporters.StoreReporter@5e18a6a7 | |
settings: scala.tools.nsc.Settings = | |
Settings { | |
-d = . | |
-Ystop-after = List(refchecks) | |
-usejavacp = true | |
-encoding = UTF-8 |
import shapeless._ | |
trait Foldable[F[_]] { | |
def foldLeft[A, B](fa: F[A], b: B)(f: (B, A) => B): B | |
} | |
object Foldable extends Foldable0 { | |
def apply[F[_]](implicit F: Lazy[Foldable[F]]): Foldable[F] = F.value | |
implicit val idFoldable: Foldable[Id] = |
trait Test { | |
type Mapped[F[_]] | |
type sx[T] = T => String | |
trait fx[X] { | |
type λ[T] = T => X | |
} | |
def summon[F[_]]: Mapped[F] | |
// okay. Inference can unify the higher kinded type variable ?F with sx | |
summon : Mapped[sx] |
package mapreduce | |
/** | |
* This is an attempt to find a minimal set of type classes that describe the map-reduce programming model | |
* (the underlying model of Google map/reduce, Hadoop, Spark and others) | |
* The idea is to have: | |
* 1) lawful types that fully constrain correctness | |
* 2) a minimal set of laws (i.e. we can't remove any laws, | |
* 3) able to fully express existing map/reduce in terms of these types | |
* |
class ext extends StaticAnnotation { | |
def macroTransform(annottees: Any*) = macro extension.impl | |
} | |
object extension { | |
def impl(c: Context)(annottees: c.Expr[Any]*): c.Expr[Any] = { | |
import c.universe._ | |
annottees.map{ _.tree }.head match { | |
case q"def $name[..$tp](...$params): $ret = $b" => | |
val Seq(Seq(thiz, rest @ _*), rest2 @ _*) = params |