Skip to content

Instantly share code, notes, and snippets.

;; global variables
(setq
inhibit-startup-screen t
create-lockfiles nil
make-backup-files nil
column-number-mode t
scroll-error-top-bottom t
show-paren-delay 0.5
use-package-always-ensure t
sentence-end-double-space nil)
@yilinwei
yilinwei / GL.scala
Last active June 1, 2016 18:36
constants.scala
package iliad
package platform
package gl
package es3
import java.nio._
import Constant._
import cats._
import cats.data._, cats.std.all._
trait Functor[F[_]] {
def map[B](fa: F[A])(f: A => B): F[B]
}
trait Monoid[F[_]] {
//boils down to F.pure(identity)
def identity[A]: F[A => A]
//This combine is 'higher' order http://stackoverflow.com/questions/3870088/a-monad-is-just-a-monoid-in-the-category-of-endofunctors-whats-the-issue'
def combine[F[_] : Functor, A](ffa: F[F[A]]): F[A]
}
package iliad
import shapeless._
import scala.annotation.implicitNotFound
import scala.reflect.ClassTag
@implicitNotFound(msg = "Cannot find PolyTC typeclass for ${TC} in list ${L}.")
trait PolyTC[L <: HList, F[_], TC[_[_]], A, B] {
def apply(a: F[A])(f: (F[A], TC[F]) => B): B
trait Foo[A] {
type Out
def apply(): Out
}
object Foo {
type Aux[A, B] = Foo[A] { type Out = B }
implicit val bfoo: Aux[Bar, Int] = new Foo[Bar] {
type Out = Int
@yilinwei
yilinwei / Free.scala
Last active September 29, 2016 22:09
Free circumvention
//Consider the following DSL for a representation of something which can have a get and a put
case class Get(key: String) extends DSL[String]
case class Put(key: String, value: String) extends DSL[String]
//When using it like,
for {
v <- lift(Get(key))
_ <- lift(Put(key, v))
//Say we have a function which returns a custom error
def process(line: String, max: Int): Try[Int] = {
if(line.length < max) Success(line.toInt) else Failure(new IllegalArgumentException(s"$line is longer than $max"))
}
//and we want to process a stream of them, but stop when there is an error
def pipe[F[_]](max: Int)(implicit F: Async[F]): Pipe[F, String, Int] = {
stream => stream.flatMap { line =>
process(line, max) match {
case Success(value) => Stream.pure(value)
object Test {
type Paged[A] = Single[(A, Option[Single[_]])]
val s: Paged[Int]= Single((1, Some(Single((2, None)))))
def recurse[A](paged: Paged[A]): Observable[A] = paged
.toObservable
.flatMap {
case (i, None) => Observable(i)
case (i, Some(page: Paged[A])) => Observable.pure(i) ++ recurse(page)
}
def main(args: Array[String]): Unit = {
@yilinwei
yilinwei / free.scala
Last active April 2, 2017 13:25
Free example
object MTL {
import cats._
import cats.free._
import cats.data._
import cats.implicits._
sealed trait DSL[A]
case class Get[A]() extends DSL[A]
package fs2
trait Chunk2[@specialized(Boolean, Int, Long, Float) +A] {
def map[B](f: A => B): Chunk2[B]
}
object Chunk2 {
object Booleans {