Code Monkey home page Code Monkey logo

naturally's Introduction

Naturally

This is just an experiment to generate and deploy the monocle Lenses associated to a case class as implicit values, using shapeless instead of macros.

Consider the following hierarchy of nested case classes as an example:

case class City(population: Int, name: String, university: University)
case class University(name: String, math: Department)
case class Department(budget: Int)

We could use Shapelens to generate monocle's Lenses for each field. For instance, we could generate the lens that points at the name field of a City as follows:

import monocle.Lens
val nameLn: Lens[City, String] = Shapelens[City, 'name :: HNil]

(*) The current encodings use Witness.`'name`.T instead of 'name, so we need to provide ad hoc aliases to make the invocation readable.

As you can see, beyond the involved case class, we need to provide the context where we expect to find the field. Thereby, if we were interested in pointing at a nested field we'd need to reflect it in the path:

val univBudgetLn = Shapelens[University, 'math :: 'budget :: HNil]

We find the context useful to disambiguate lenses that share the same type. For instance, there are two possible lenses with type Lens[City, String]: the one that points at the city name and the one that points at the university name of a city. We use the context path (instead of using the field type) to be precise about which field we are interested in:

val cityNameLn = Shapelens[City, 'name :: HNil]
val univNameLn = Shapelens[City, 'university :: 'name :: HNil]

You can find the whole example here.

Finally, we must say that we are exploiting this idea to provide automatic instances of state-based algebraic theories in the upcoming new version of Stateless. However, we find this technique general enough to publish it on its own. Having said so, we hope you find a crazy scenario where you can apply it!

(*) As @julien-truffaut pointed out, you can use GenLens[City](_.university.name) to get the same behaviour in Monocle. Anyway, the Shapelens type class is still useful when you require implicit lens evidences in scope.

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.