No description
  • F# 97.4%
  • Shell 2.6%
Find a file
2016-01-03 19:42:41 +02:00
.paket Toward PCL PPrint 2.0.0. 2016-01-01 21:30:18 +02:00
Libs Docs 2016-01-03 19:42:41 +02:00
Tests/Examples <3 2016-01-02 13:04:56 +02:00
.gitignore Flags... Sigh. 2016-01-02 11:11:15 +02:00
.travis.yml Using a separate build script. 2014-09-14 19:27:01 +03:00
appveyor.yml Flags... Sigh. 2016-01-02 11:11:15 +02:00
build.sh Separate PPrint.Console nupkg. 2016-01-02 14:22:38 +02:00
LICENSE.md Toward PCL PPrint 2.0.0. 2016-01-01 21:30:18 +02:00
paket.dependencies <3 2016-01-02 13:04:56 +02:00
paket.lock <3 2016-01-02 13:04:56 +02:00
PPrint.Console.paket.template Separate PPrint.Console nupkg. 2016-01-02 14:22:38 +02:00
PPrint.fsx Xamarin <3 PCL 2016-01-03 14:51:39 +02:00
PPrint.paket.template Docs 2016-01-03 14:45:18 +02:00
PPrint.sln Toward PCL PPrint 2.0.0. 2016-01-01 21:30:18 +02:00
README.md Docs 2016-01-03 14:50:29 +02:00

PPrint is a pretty printing combinator library for text documents that can be rendered to a desired maximum width.

Docs

Background

Example

Here is an example of how one could define a function with signature

module Json =
  val pretty: JsonValue -> Doc

for pretty printing JSON values:

open System.Json
open PPrint

let inline (^) x = x

module Json =
  let private prettySeq (l, r) toDoc xs =
    if Seq.isEmpty xs
    then l <^> r
    else let ds = xs |> Seq.map toDoc |> punctuate comma |> vsep
         l <..> ds |> nest 2 <..> r |> group

  let rec pretty (json: JsonValue) =
    match json with
     | :? JsonObject as json ->
       json
       |> prettySeq lrbrace ^ fun kv ->
            pretty ^ JsonPrimitive kv.Key <^> colon <+> pretty kv.Value
     | :? JsonArray as json ->
       json
       |> prettySeq lrbracket pretty
     | null ->
       txt "null"
     | _ ->
       json
       |> string
       |> txt

Given a JSON value

let json = JsonValue.Parse """
{
  "firstName": "John",
  "lastName": "Smith",
  "isAlive": true,
  "age": 25,
  "address": {
    "streetAddress": "21 2nd Street",
    "city": "New York",
    "state": "NY",
    "postalCode": "10021-3100"
  },
  "phoneNumbers": [
    {
      "type": "home",
      "number": "212 555-1234"
    },
    {
      "type": "office",
      "number": "646 555-4567"
    }
  ],
  "children": [],
  "spouse": null
}
"""

we can render it to different desired maximum widths. For example,

json |> Json.pretty |> println ^ Some 40

produces

{
  "address": {
    "city": "New York",
    "postalCode": "10021-3100",
    "state": "NY",
    "streetAddress": "21 2nd Street"
  },
  "age": 25,
  "children": [],
  "firstName": "John",
  "isAlive": true,
  "lastName": "Smith",
  "phoneNumbers": [
    {
      "number": "212 555-1234",
      "type": "home"
    },
    {
      "number": "646 555-4567",
      "type": "office"
    }
  ],
  "spouse": null
}

and

json |> Json.pretty |> println ^ Some 80

produces

{
  "address": {
    "city": "New York",
    "postalCode": "10021-3100",
    "state": "NY",
    "streetAddress": "21 2nd Street"
  },
  "age": 25,
  "children": [],
  "firstName": "John",
  "isAlive": true,
  "lastName": "Smith",
  "phoneNumbers": [
    {"number": "212 555-1234", "type": "home"},
    {"number": "646 555-4567", "type": "office"}
  ],
  "spouse": null
}