Skip to content

Instantly share code, notes, and snippets.

View blast-hardcheese's full-sized avatar

Devon Stewart blast-hardcheese

View GitHub Profile
trait Semigroup[A] { def combine(a: A, b: A): A }
object Semigroup { def apply[A](a: A, b: A)(implicit ev: Semigroup[A]): A = ev.combine(a, b) }
implicit object StrSemi extends Semigroup[String] { def combine(a: String, b: String): String = a + b }
implicit def SemiTuple2[A, B](implicit ev1: Semigroup[A], ev2: Semigroup[B]): Semigroup[(A, B)] = new Semigroup[(A, B)] {
def combine(a: (A, B), b: (A, B)): (A, B) = (ev1.combine(a._1, b._1), ev2.combine(a._2, b._2))
}
def buildInstance(size: Int) = {
val instanceName = Term.Name(s"SemiTuple${size}")
val (tparms, types) = (1 to size).toList.map({ i =>
val tname = Type.Name(s"A${i}")
@blast-hardcheese
blast-hardcheese / git-stash-rebase
Last active February 19, 2019 23:01
A bit of infrastructure to permit stashing a current rebase, permitting performing any number of rebases while working towards the larger rebase.
#!/bin/sh
rebase_head_exists() {
[ -f "${GIT_ROOT}/REBASE_HEAD${1}" ]
}
rebase_merge_exists() {
[ -d "${GIT_ROOT}/rebase-merge${1}" ]
}
package blep
import cats.{ Id, Monad }
import scala.meta._
sealed trait BlepTerm[T]
case class WritePackageObject() extends BlepTerm[Long]
object BlepGenerator {
def wat[T](value: BlepTerm[T]): Id[T] = value match {
import cats._, cats.arrow._, cats.data._, cats.free._, cats.implicits._
import scala.meta.{ Import, Source }
package object foo {
type Target[T] = Either[String, T]
val Target = Monad[Target]
}
package foo {
@blast-hardcheese
blast-hardcheese / error.txt
Created November 6, 2018 06:54
Pathed types
> compile
[info] Compiling 1 Scala source to /Users/dstewart/Projects/guardrail/modules/codegen/target/scala-2.12/classes...
[error] /Users/dstewart/Projects/guardrail/modules/codegen/src/main/scala/com/twilio/guardrail/CLI.scala:17: type mismatch;
[error] found : CLICommon.algebra.CoreTerms
[error] required: CLICommon.common.A.CoreTerms
[error] common.runM(algebra.coreTerm)
[error] ^
[error] one error found
[error] (codegen/compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed Nov 5, 2018 10:53:10 PM
scala> Foo.buildBar.baz(_, _)
res0: (Int, Int) => Int = <function2>
scala> res0(1,2)
Hello!
res1: Int = 3
scala> res0(1,2)
Hello!
res2: Int = 3
publish:
./scripts/publish-to-gollum --prepare-docs-target --overwrite
sbt -no-colors tut
./scripts/publish-to-gollum --publish-docs --overwrite
[error] .../App2.scala:9: type mismatch;
[error] found : input.type (with underlying type H#Input)
[error] required: RouteService.this.Holder.Input
[error] def extractFoo(input: H#Input): Foo = Holder.input2Foo(input)
[error] ^
[error] one error found
[error] (core/compile:compileIncremental) Compilation failed
[error] Total time: 0 s, completed Oct 23, 2016 11:15:51 PM
@blast-hardcheese
blast-hardcheese / App.scala
Created October 24, 2016 04:16
Flexible genericism
import scala.concurrent.Future
object App extends App {
val play = new RouteService[PlaySpec] {
def handle(req: PlayRequest) = Future.successful(new PlayResponse)
def addCookie(request: PlayRequest, cookie: PlayCookieAtom): PlayRequest = request.copy(cookie :: request.cookies)
}
val akka = new RouteService[AkkaSpec] {
def handle(req: AkkaRequest) = Future.successful(new AkkaResponse)
@blast-hardcheese
blast-hardcheese / Algebras.scala
Last active July 4, 2016 05:29
Type inference question in Cats
sealed trait ControlPanel[T]
case object ButtonPressed extends ControlPanel[Button]
case object StopEnabled extends ControlPanel[Unit]
case object StopDisabled extends ControlPanel[Unit]
case object CurrentFloor extends ControlPanel[Int]
sealed trait ElevatorControl[T]
case object GetFloor extends ElevatorControl[Int]
case class QueueFloor(x: Int) extends ElevatorControl[Unit]