Code Monkey home page Code Monkey logo

shapeless-contrib's Introduction

shapeless-contrib

Interoperability libraries for Shapeless

Build Status

Usage

This library is currently available for Scala 2.11.

To use the latest version, include the following in your build.sbt:

libraryDependencies ++= Seq(
  "org.typelevel" %% "shapeless-scalacheck" % "0.3",
  "org.typelevel" %% "shapeless-spire" % "0.3",
  "org.typelevel" %% "shapeless-scalaz" % "0.3"
)

What does this library do?

shapeless-contrib aims to provide smooth interoperability between Shapeless, Scalaz and Spire. The most prominent feature is automatic derivation of type class instances for case classes, but there is more. If you've got a cool idea for more stuff, please open an issue.

Examples

Scalaz + Shapeless = Profit

Note: The current 0.3.x series depends on shapeless 2.0.0.

The combination of these two libraries allows for some nifty utility functions related to scalaz.Applicative:

  • lifting arbitrary functions (i.e. a generalized liftA1, liftA2, ...)
  • sequencing and traversing an HList (just like sequencing and traversing a List)
import shapeless._
import shapeless.contrib.scalaz._

import scalaz.Applicative
import scalaz.std.option._

// define a function with arbitrarily many parameters
def foo(x: Int, y: String, z: Float) = s"$x - $y - $z"

// lift it into `Option`
val lifted = Applicative[Option].liftA(foo _)

// resulting type: `(Option[Int], Option[String], Option[Float]) => Option[String]`


// define an `HList` consisting of `Option`s
val in = Option(1) :: Option("foo") :: HNil

val sequenced = sequence(in)

// resulting type: `Option[Int :: String :: HNil]`

// works for `Validation`, too:
import scalaz._
import scalaz.std.string._

val v1: Validation[String, Int] = Success(3)
val v2: Validation[String, Float] = Failure("foo")
sequence(v1 :: v2 :: HNil)

// resulting type: `Validation[String, Int :: Float :: HNil]`

Traversing works the same way, but you will also have to specify a shapeless.Poly which maps over the HList first.

In addition to that, it also provides a conversion between their lens types:

import shapeless._
import shapeless.contrib.scalaz._

case class TwoElem(n: Int, x: String)

// Generate a `shapeless.Lens`
val sLens = lens[TwoElem] >> 'n

// Convert it to a `scalaz.Lens`
val zsLens = sLens.asScalaz

// The other way round:
import scalaz.Lens

val zLens = Lens.lensId[Int]
val szLens = zLens.asShapeless

Derive type classes

Consider a simple case class with an addition operation:

case class Vector3(x: Int, y: Int, z: Int) {
  def +(other: Vector3) = Vector3(this.x + other.x, this.y + other.y, this.z + other.z)
}

If we want to use that in a generic setting, e.g. in an algorithm which requires a Monoid, we can define an instance for spire.algebra.Monoid like so:

implicit object Vector3Monoid extends Monoid[Vector3] {
  def id = Vector3(0, 0, 0)
  def op(x: Vector3, y: Vector3) = x + y
}

This will work nicely for that particular case. However, it requires repetition: addition on Vector3 is just pointwise addition of its elements, and the null vector consists of three zeroes. We do not want to repeat that sort of code for all our case classes, and want to derive that automatically. Luckily, this library provides exactly that:

import spire.implicits._
import shapeless.contrib.spire._

// Define the `Vector3` case class without any operations
case class Vector3(x: Int, y: Int, z: Int)

// That's it! `Vector3` is an `AdditiveMonoid` now.
Vector3(1, 2, 3) + Vector3(-1, 3, 0)

shapeless-contrib's People

Contributors

ceedubs avatar jedesah avatar larsrh avatar s11001001 avatar stew avatar xuwei-k avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.