Giter VIP home page Giter VIP logo

paiges's Introduction

Paiges

Overview

Paiges is an implementation of Wadler's "A Prettier Printer".

The library is useful any time you find yourself generating text or source code where you'd like to control the length of lines (e.g. paragraph wrapping).

The name Paiges is a reference to the Paige compositor and the fact that it helps you layout pages.

Build Status codecov.io Latest version

Quick Start

Paiges supports Scala 2.10, 2.11, and 2.12. It supports both the JVM and JS platforms.

To use Paiges in your own project, you can include this snippet in your build.sbt file:

// use this snippet for the JVM
libraryDependencies += "org.typelevel" %% "paiges-core" % "0.2.1"

// use this snippet for JS, or cross-building
libraryDependencies += "org.typelevel" %%% "paiges-core" % "0.2.1"

Paiges also provides types to work with Cats via the paiges-cats module:

// use this snippet for the JVM
libraryDependencies += "org.typelevel" %% "paiges-cats" % "0.2.1"

// use this snippet for JS, or cross-building
libraryDependencies += "org.typelevel" %%% "paiges-cats" % "0.2.1"

Description

This code is a direct port of the code in section 7 of this paper, with an attempt to be idiomatic in scala while preserving the original code's properties, including laziness.

This algorithm is optimal and bounded. From the paper:

Say that a pretty printing algorithm is optimal if it chooses line breaks so as to avoid overflow whenever possible; say that it is bounded if it can make this choice after looking at no more than the next w characters, where w is the line width. Hughes notes that there is no algorithm to choose line breaks for his combinators that is optimal and bounded, while the layout algorithm presented here has both properties.

Some selling points of this code:

  1. Lazy, O(1) concatenation
  2. Competitive performance (e.g. 3-5x slower than mkString)
  3. Elegantly handle indentation
  4. Flexible line-wrapping strategies
  5. Functional cred ;)

Examples

Here's an example of using Paiges to generate the source code for a case class:

import org.typelevel.paiges._

/**
 * Produces a case class given a name and zero-or-more
 * field/type pairs.
 */
def mkCaseClass(name: String, fields: (String, String)*): Doc = {
  val prefix = Doc.text("case class ") + Doc.text(name) + Doc.char('(')
  val suffix = Doc.char(')')
  val types = fields.map { case (k, v) =>
    Doc.text(k) + Doc.char(':') + Doc.space + Doc.text(v)
  }
  val body = Doc.intercalate(Doc.char(',') + Doc.line, types)
  body.tightBracketBy(prefix, suffix)
}

val c = mkCaseClass(
  "Dog", "name" -> "String", "breed" -> "String",
  "height" -> "Int", "weight" -> "Int")

c.render(80)
// case class Dog(name: String, breed: String, height: Int, weight: Int)

c.render(60)
// case class Dog(
//   name: String,
//   breed: String,
//   height: Int,
//   weight: Int
// )

For more examples, see the tutorial.

Benchmarks

The Paiges benchmarks are written against JMH. To run them, you'll want to use a command like this from SBT:

benchmark/jmh:run -wi 5 -i 5 -f1 -t1 bench.PaigesBenchmark

By default the values reported are ops/ms (operations per millisecond), so higher numbers are better.

The parameters used here are:

  • -wi: the number of times to run during warmup
  • -i: the number of times to benchmark
  • -f: the number of processes to use during benchmarking
  • -t: the number of threads to use during benchmarking

In other words, the example command-line runs one thread in one process, with a relatively small number of warmups + runs (so that it will finish relatively quickly).

Organization

The current Paiges maintainers are:

People are expected to follow the Typelevel Code of Conduct when discussing Paiges on the Github page or other official venues.

Concerns or issues can be sent to any of Paiges' maintainers, or to the Typelevel organization.

License

Paiges is licensed under the Apache License, Version 2.0 (the "License"); you may not use this software except in compliance with the License.

Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License.

paiges's People

Contributors

andyscott avatar coltfred avatar erik-stripe avatar johnynek avatar non avatar oscar-stripe avatar seanmcl avatar sethtisue avatar xuwei-k 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.