Skip to content

Instantly share code, notes, and snippets.

@ShaneDelmore
Created December 8, 2015 23:05
Show Gist options
  • Select an option

  • Save ShaneDelmore/76a0cf4e6bb89f4306ff to your computer and use it in GitHub Desktop.

Select an option

Save ShaneDelmore/76a0cf4e6bb89f4306ff to your computer and use it in GitHub Desktop.
Ammonite predef
load.ivy("com.lihaoyi" %% "ammonite-shell" % ammonite.Constants.version)
load.ivy("org.scalaj" %% "scalaj-http" % "2.1.0")
@
val sess = ammonite.shell.ShellSession()
import sess._
import ammonite.ops._
import ammonite.shell._
import scalaj.http._
ammonite.shell.Configure(repl, wd)
def splitOnWs(strs: Seq[String]): Seq[Array[String]] = strs.map(_.split("\\s+"))
object Catnip {
implicit class IdOp[A](val a: A) extends AnyVal {
def some: Option[A] = Some(a)
}
def none[A]: Option[A] = None
def kind[A: scala.reflect.runtime.universe.TypeTag]: String = {
import scala.reflect.runtime.universe.{typeOf => _, _}
def typeKind(sig: Type): String = sig match {
case PolyType(params, resultType) =>
(params map { p =>
typeKind(p.typeSignature) match {
case "*" => "*"
case s => "(" + s + ")"
}
}).mkString(" -> ") + " -> *"
case _ => "*"
}
def typeSig(tpe: Type): Type = tpe match {
case SingleType(pre, sym) => sym.companionSymbol.typeSignature
case ExistentialType(q, TypeRef(pre, sym, args)) => sym.typeSignature
case TypeRef(pre, sym, args) => sym.typeSignature
}
val sig = typeSig(typeOf[A])
val s = typeKind(sig)
sig.typeSymbol.name + "'s kind is " + s + ". " + (s match {
case "*" =>
"This is a proper type."
case x if !(x contains "(") =>
"This is a type constructor: a 1st-order-kinded type."
case x =>
"This is a type constructor that takes type constructor(s): a higher-kinded type."
})
}
}
import Catnip._
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment