Skip to content

Instantly share code, notes, and snippets.

View Fristi's full-sized avatar

Mark Fristi

View GitHub Profile
@aaronlevin
aaronlevin / cats-mtl.scala
Last active May 26, 2017 17:38
Does the cats-mtl design support for-comprehensions with multiple Monad constraints? Looks like it does!
package cats
import cats.data.{StateT, WriterT}
import cats.implicits._
/**
* TypeLevel is working on some alternative encodings of the Monad Transformer
* Library (MTL). The existing solution in cats was broken for a few reasons.
* One annoying issue made it imposisible to use for-comprehension with more
* than one Monad$FOO constraint owing to implicit resolution ambiguities.
@tel
tel / Lenses.scala
Created April 18, 2017 04:38
Pure profunctor lenses in Scala
import scala.language.higherKinds
object Lenses {
trait Profunctor[P[_, _]] {
def dimap[A, B, C, D](f: C => A, g: B => D)(p: P[A, B]): P[C, D]
}
object Profunctor {
import scalaz._, Scalaz._
/** Abstraction over functions of the shape `A => F[A]`. */
case class EndoT[F[_], A](run: A => F[A]) {
def apply(a: A): F[A] =
run(a)
def andThen(e: EndoT[F, A])(implicit ev: Bind[F]): EndoT[F, A] =
e.compose(this)
package cats.bench
import cats.data.Xor
import cats.instances.either._
import cats.syntax.all._
import org.openjdk.jmh.annotations.{ Benchmark, Scope, State }
@State(Scope.Benchmark)
class EitherBench {
val ea: Either[String, Int] = Right(1)
@lihaoyi
lihaoyi / play.scala
Last active April 18, 2017 08:18
play.scala
/**
* Single-file play framework application! Make sure everything
* works, as this is the test case that un-earthed #371
*/
load.ivy("com.typesafe.play" %% "play" % "2.5.0")
load.ivy("com.typesafe.play" %% "play-netty-server" % "2.5.0")
load.ivy("org.scalaj" %% "scalaj-http" % "2.2.1")
@
package akka.kafka.scaladsl
import java.util.concurrent.TimeUnit
import java.util.{Properties, UUID}
import akka.actor.ActorSystem
import akka.kafka.{ConsumerSettings, ProducerSettings}
import akka.stream.ActorMaterializer
import akka.stream.scaladsl.Sink
import akka.testkit.TestKit
def treeDoc(t: Tree): Doc = t match {
case PackageDef(mods, pid, xs) => mods <> "package" <+> pid <+> xs.inBracesBlock
case Import(expr, selectors) => "import" <+> expr <> dot <> selectors
case NamedImport(name) => name
case TypeIdent(pre, name) => (pre :+ name).joinDotted
case TermIdent(pre, name) => (pre :+ name).joinDotted
case ClassDef(mods, keyword, name, tparams, vparamss, impl) => linebreak <> mods <> keyword <+> name <> tparams <> tight(vparamss) <> doTemplate(line <> "extends", impl)
case TypeParam(mods, name, tparams, constraints) => mods <> name <> tparams <> constraints
case ValueParam(mods, name, tpt, defaultValue) => mods <> name <> tpt <> opt(space <> assign, defaultValue)
case x: Name => x.value
@octonato
octonato / type-classs-derivation.scala
Created June 11, 2015 16:34
Writer/Reader (Shapeless workshop ScalaDays 2015)
/*
* Copyright (c) 2015 Miles Sabin
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
@pchiusano
pchiusano / finallytagless.scala
Last active March 13, 2018 11:28
Finally tagless encoding of GADTs in Scala
trait ConsoleAlg[F[_]] {
def readLine: F[Option[String]]
def printLine(line: String): F[Unit]
}
trait Console[+A] {
def run[F[+_]](F: ConsoleAlg[F]): F[A]
}
object Console {
@nh2
nh2 / time-natural.hs
Created March 1, 2014 01:33
Natural time in Haskell: `2 hours + 4 seconds`
{-# LANGUAGE FlexibleInstances, GeneralizedNewtypeDeriving #-}
newtype TimeUnit = TimeUnit Integer -- how many microseconds
deriving (Eq, Show, Num)
instance Num (TimeUnit -> TimeUnit) where
fromInteger n = \(TimeUnit scale) -> TimeUnit (n * scale)
-- a + b = ... -- task for you