Skip to content

Instantly share code, notes, and snippets.

@betehess
Created May 26, 2015 16:43
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save betehess/cff074888d9d7d50f2dd to your computer and use it in GitHub Desktop.
Save betehess/cff074888d9d7d50f2dd to your computer and use it in GitHub Desktop.
Using shapeless to drive Ammonite's PPrinter derivation, used to print shapeless values
> runMain ammonite.repl.Repl
[info] Running ammonite.repl.Repl
Loading Ammonite Repl...
@ load.ivy("com.chuusai" %% "shapeless" % "2.1.0")
@ import shapeless._
import shapeless._
@ 1 :: "lol" :: List(1, 2, 3) :: HNil
res2: Int :: String :: List[Int] :: HNil = ::(
1,
::("lol", ::(List(1, 2, 3), HNil))
)
@ import ammonite.pprint._
import ammonite.pprint._
@ implicit val hnilPPrinter: PPrinter[HNil] = PPrinter[HNil] { (hnil, cfg) => Iterator("HNil") }
hnilPPrinter: PPrinter[HNil] = ammonite.pprint.PPrinter$$anon$1@3e8db096
@ implicit def hlistPPrinter[T <: HList, H](implicit
tailPPrinter: PPrinter[T],
headPPrint: PPrint[H]
): PPrinter[H :: T] = PPrinter { case (h :: t, cfg) =>
headPPrint.render(h) ++ Iterator(" :: ") ++ tailPPrinter.render(t, cfg)
}
defined function hlistPPrinter
@ 1 :: "lol" :: List(1, 2, 3) :: HNil
res6: Int :: String :: List[Int] :: HNil = 1 :: "lol" :: List(1, 2, 3) :: HNil
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment