Code Monkey home page Code Monkey logo

mercator-old's Introduction

GitHub Workflow

Mercator

Mercator is a macro for automatically constructing evidence that a known type may be used in a for-comprehension, for abstracting over monad-like types with no impact on performance. This allows us to write code against generic type constructors which can assume that they are usable in a for-comprehension, without having the evidence that they are until the application point when the type constructor is known, at which point, Mercator will construct it.

Features

  • abstracts over monad-like types
  • constructs a monad typeclass instance for any type with flatMap, map and a "point" constructor
  • makes intelligent guesses about identifying the "point" constructor for a type
  • constructs a functor instance if only map and "point" are available

Getting Started

It is not possible to write code such as,

// does not compile
def increment[F[_]](xs: F[Int]) = for(x <- xs) yield x + 1

because the compiler is not able to enforce the constraint that the type constructor F[_] provides the methods map and flatMap (with the correct signatures) which are necessary for the for-comprehension to compile.

With Mercator, it is possible to demand an implicit instance of Monadic[F] to enforce this constraint. Mercator will automatically instantiate such an instance at the use-site for any type which has the required methods, like so,

import mercator._
def increment[F[_]: Monadic](xs: F[Int]) = for(x <- xs) yield x + 1

The methods flatMap and map will be provided to the instance of F[_] as extension methods, using an implicit value class in the mercator package. This incurs no allocations at runtime, and the performance overhead should be zero or negligible.

Point

An instance of Monadic[F] will generate an implementation of point, which constructs a new instance of the type from a single value. This implementation assumes the existence of an apply method on the type's companion object, and that applying the value to it will produce a result of the correct type.

If this is not the case, Mercator will try to find a unique subtype of F[_] whose companion object has an apply method taking a single value and returning the correct type. In the case of Either or Scalaz's \/, this will do the right thing.

Status

Mercator is classified as fledgling. Propensive defines the following five stability levels for open-source projects:

  • embryonic: for experimental or demonstrative purposes only, without guarantee of longevity
  • fledgling: of proven utility, seeking contributions, but liable to significant redesigns
  • maturescent: major design decisions broady settled, seeking probatory adoption and refinement of designs
  • dependable: production-ready, subject to controlled ongoing maintenance and enhancement; tagged as version 1.0 or later
  • adamantine: proven, reliable and production-ready, with no further breaking changes ever anticipated

Availability

Mercator’s source is available on GitHub, and may be built with Fury by cloning the layer propensive/mercator.

fury layer clone -i propensive/mercator

or imported into an existing layer with,

fury layer import -i propensive/mercator

A binary is available on Maven Central as com.propensive:mercator-core_<scala-version>:0.4.0. This may be added to an sbt build with:

libraryDependencies += "com.propensive" %% "mercator-core" % "0.4.0"

Contributing

Contributors to Mercator are welcome and encouraged. New contributors may like to look for issues marked label: good first issue.

We suggest that all contributors read the Contributing Guide to make the process of contributing to Mercator easier.

Please do not contact project maintainers privately with questions, as other users cannot then benefit from the answers.

Author

Mercator was designed and developed by Jon Pretty, and commercial support and training is available from Propensive OÜ.

License

Mercator is copyright © 2018-20 Jon Pretty & Propensive OÜ, and is made available under the Apache 2.0 License.

mercator-old's People

Contributors

adamw avatar andyscott avatar fommil avatar ghostbuster91 avatar jatcwang avatar philippus avatar propensive avatar xuwei-k avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

mercator-old's Issues

explicit instances cause ambiguous implicits

I wanted to create an optimised, universally quantified, instance (which can be cached across the E parameter)

  implicit def mercatorDisjunction[E]: mercator.Monadic[E \/ ?] = new mercator.Monadic[E \/ ?] {
    def point[A](a: A): E \/ A = \/-(a)
    def flatMap[A, B](fa: E \/ A, f: A => E \/ B): E \/ B = fa.flatMap(f)
    def map[A, B](fa: E \/ A, f: A => B): E \/ B = fa.map(f)
  }

but using this I get

[error] /home/fommil/Projects/scalaz-deriving/examples/jsonformat/src/main/scala/jsonformat/JsMagnolia.scala:140:32: ambiguous implicit values:
[error]  both method mercatorDisjunction in object JsMagnoliaDecoder of type [E]=> mercator.Monadic[[β$0$]scalaz.\/[E,β$0$]]
[error]  and macro method monadicEvidence in package mercator of type [F[_]]=> mercator.Monadic[F]
[error]  match expected type mercator.Monadic[[+B]scalaz.\/[String,B]]
[error]           ctx.constructMonadic { p =>
[error]                                ^

master branch doesn't compile

seen in the Scala 2.12 community build, reproducible independently:

[error] /Users/tisue/mercator/tests/src/main/scala/tests.scala:28:20: object creation impossible, since method filter in trait MonadicFilter of type [A](value: Option[A])(fn: A => Boolean)Option[A] is not defined
[error]     monadicEvidence[Option]
[error]                    ^
[error] /Users/tisue/mercator/tests/src/main/scala/tests.scala:30:20: object creation impossible, since method filter in trait MonadicFilter of type [A](value: Seq[A])(fn: A => Boolean)Seq[A] is not defined
[error]     monadicEvidence[Seq]
[error]                    ^
[error] /Users/tisue/mercator/tests/src/main/scala/tests.scala:34:14: object creation impossible, since method filter in trait MonadicFilter of type [A](value: List[A])(fn: A => Boolean)List[A] is not defined
[error]     increment(List(1, 2, 3))
[error]              ^
[error] /Users/tisue/mercator/tests/src/main/scala/tests.scala:35:14: object creation impossible, since method filter in trait MonadicFilter of type [A](value: Option[A])(fn: A => Boolean)Option[A] is not defined
[error]     increment(Option(4))
[error]              ^
[error] /Users/tisue/mercator/tests/src/main/scala/tests.scala:36:14: object creation impossible, since method filter in trait MonadicFilter of type [A](value: Traversable[A])(fn: A => Boolean)Traversable[A] is not defined
[error]     increment(Traversable(5))
[error]              ^
[error] 5 errors found

in the community build, I guess I'll freeze it at the v0.1.1 tag

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.