Giter VIP home page Giter VIP logo

totalitarian's Introduction

Latest version Join the chat at https://gitter.im/propensive/totalitarian Maven Central

Totalitarian

Totalitarian provides typesafe data structures for working with total (not partial) functions.

Disjunctions

It primarily provides the Disjunct type, a typesafe representation of a union type, utilizing a type intersection to represent the branches of the disjunction.

For example, a disjunction of an Int and a String can be constructed with

  val disj = Disjunct.of[Int with String](1)

or

  val disj: Disjunct[Int with String] = Disjunct("two")

whereas the following would fail to compile:

  val disj: Disjunct[Int with String] = Disjunct('three)

Appropriate least upper-bound types will also be inferred, for example,

scala> val disjs = List(Disjunct(1), Disjunct('two), Disjunct("three"))
disjs: List[Disjunct[Int with Symbol with String]] = List(1, 'two, three)

Transforming disjunctions

It is also possible to perform transformations on the values in a Disjunct. This is most commonly done using the when method, which takes handlers for each branch of the disjunction as repeated parameters, taking either a lambda on the disjunction's value, or just a value, like so:

val disj: Disjunct[Int with String] = Disjunct("two")
disj.when(
  on[Int](42),
  on[String] { s => s"$s!" }
)

This returns another new Disjunct[Int with String], in this example, containing the string "two!". Failure to handle any branch of the disjunction would result in a compile error, and it is this requirement of totality which gives Totalitarian its name.

The return type does not need to be the same, and the lambdas or values for each on-case may return any type. For example, this,

val disj: Disjunct[Int with String] = Disjunct("two")
disj.when(
  on[Int] { i => i/2.0 },
  on[String] { s => Symbol(s) }
)

would return a new Disjunct[Double with Symbol], in this example, containing the value 'two.

If, however, a when method transforms a Disjunct such that the same type is returned regardless of its input type, the return value is no longer a disjunction, and the return type will be the raw type. For example,

val disj: Disjunct[Int with String] = Disjunct("two")
val s: String = disj.when(
  on[Int]("an integer"),
  on[String] { s => s }
)

totalitarian's People

Contributors

andyscott avatar jan0sch avatar propensive avatar

Watchers

 avatar  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.