Skip to content

Commit

Permalink
Merge pull request #1047 from danicheg/mdoc
Browse files Browse the repository at this point in the history
Migrate on mdoc
  • Loading branch information
armanbilge authored Aug 25, 2021
2 parents afa10a0 + 2d81545 commit 41f69b8
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 69 deletions.
8 changes: 4 additions & 4 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,10 @@ lazy val docs = project
.settings(spireSettings: _*)
.settings(docSettings: _*)
.settings(noPublishSettings)
.enablePlugins(TutPlugin)
.enablePlugins(MdocPlugin)
.settings(
mdocIn := (Compile / sourceDirectory).value / "mdoc"
)
.settings(commonJvmSettings: _*)

lazy val examples = project
Expand Down Expand Up @@ -262,7 +265,6 @@ lazy val commonJvmSettings = Seq()
lazy val docsMappingsAPIDir = settingKey[String]("Name of subdirectory in site target directory for api docs")

lazy val docSettings = Seq(
Tut / scalacOptions := (Tut / scalacOptions).value.filterNot(Set("-Ywarn-unused-imports", "-Xlint").contains),
micrositeName := "Spire",
micrositeDescription := "Powerful new number types and numeric abstractions for Scala",
micrositeAuthor := "Spire contributors",
Expand Down Expand Up @@ -324,7 +326,6 @@ lazy val docSettings = Seq(
ghpagesNoJekyll := false,
fork := true,
javaOptions += "-Xmx4G", // to have enough memory in forks
// fork in tut := true,
// fork in (ScalaUnidoc, unidoc) := true,
ScalaUnidoc / unidoc / scalacOptions ++= Seq(
"-groups",
Expand All @@ -334,7 +335,6 @@ lazy val docSettings = Seq(
(LocalRootProject / baseDirectory).value.getAbsolutePath,
"-diagrams"
),
Tut / scalacOptions ~= (_.filterNot(Set("-Ywarn-unused-import", "-Ywarn-dead-code"))),
git.remoteRepo := "git@github.com:typelevel/spire.git",
makeSite / includeFilter := "*.html" | "*.css" | "*.png" | "*.jpg" | "*.gif" | "*.js" | "*.swf" | "*.yml" | "*.md" | "*.svg",
Jekyll / includeFilter := (makeSite / includeFilter).value
Expand Down
44 changes: 22 additions & 22 deletions docs/src/main/tut/guide.md → docs/src/main/mdoc/guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ operations.

These code examples all assume the following imports:

```tut
```scala mdoc:silent:nest
import spire.algebra._ // all type class definitions
import spire.implicits._ // all type class instances and syntax
```
Expand All @@ -39,15 +39,15 @@ in the case of `plus`, it corresponds with `+`. When using these type
classes, users have the option of using the symbolic syntax on the
values directly or calling the method on the type class instance:

```tut
```scala mdoc:silent
def usingSymbols[A: Ring](x: A, y: A): A = x + y
def usingNames[A](x: A, y: A)(implicit r: Ring[A]): A = r.plus(x, y)
```

Some methods (e.g. `sqrt`) do not have corresponding symbols. In those
cases, the method name itself can be used with the values:

```tut
```scala mdoc:silent
def sqrt[A: NRoot](x: A): A = x.sqrt
```

Expand Down Expand Up @@ -88,12 +88,12 @@ and for when you're sure there won't be a conflict.
Most of the time, you'll be using type classes as context bounds. For
instance:

```tut
import spire.algebra._
import spire.std.any._
import spire.syntax.ring._
```scala mdoc
object Demo {
import spire.algebra._
import spire.std.any._
import spire.syntax.ring._

def double[A: Ring](x: A): A = x + x
def triple[A: Ring](x: A): A = x * 3
println((double(3), triple(4)))
Expand All @@ -102,8 +102,8 @@ object Demo {

This code ends up being equivalent to:

```tut
object Demo {
```scala mdoc
object Demo2 {
def double[A](x: A)(implicit ev: Ring[A]): A = ev.plus(x, x)
def triple[A](x: A)(implicit ev: Ring[A]): A = ev.times(x, ev.fromInt(3))
println((double(3)(IntAlgebra), triple(4)(IntAlgebra)))
Expand All @@ -127,14 +127,14 @@ to use specialization. The good news is that most of Spire's code is
already specialized (and tested for proper performance). The bad news
is that you'll have to annotate all your generic code like so:

```tut
import spire.algebra._
import spire.std.any._
import spire.syntax.ring._
```scala mdoc
object Demo3 {
import spire.algebra._
import spire.std.any._
import spire.syntax.ring._

import scala.{specialized => sp}

import scala.{specialized => sp}
object Demo {
def double[@sp A: Ring](x: A): A = x + x
def triple[@sp A: Ring](x: A): A = x * 3
println((double(3), triple(4)))
Expand Down Expand Up @@ -610,7 +610,7 @@ Writing literal unsigned values is slightly more cumbersome than their
signed counterparts (consider `UInt(7)` versus `7`). Spire provides
syntax imports which make these slightly easier to write:

```tut
```scala mdoc:nest
import spire.syntax.literals._

ui"7" // equivalent to UInt(7)
Expand Down Expand Up @@ -711,8 +711,9 @@ importing `spire.syntax.literals._` (or just `spire.implicits._`) you
can use the `poly` string interpolator to create
`Polynomial[Rational]` instances:

```tut
```scala mdoc:nest
import spire.syntax.literals._

poly"3x^2 - 5x + 1"
poly"5/4x^6 - 7x - 2"
poly"1.2x^3 - 6.1x^2 + 9x - 3.33"
Expand Down Expand Up @@ -777,14 +778,13 @@ obey the relevant algebraic identities. But unlike `Rational`, `Real`
supports roots and trigonometric functions. Furthermore, important
trig identities are also preserved:

```tut
import spire.implicits._
```scala mdoc:silent
import spire.math.Real

import Real.{sin, cos}

// will return Real(1) no matter what value is provided
def circle(a: Real): Real = (cos(a).pow(2) + sin(a).pow(2)).sqrt
def circle(a: Real): Real = (cos(a).pow(2) + sin(a).pow(2)).sqrt()
```

One interesting consequence of the design of computable real numbers
Expand Down
92 changes: 50 additions & 42 deletions docs/src/main/tut/index.md → docs/src/main/mdoc/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ Detailed treatment of these type classes can be found in the
Spire contains a lot of types, as well as other machinery to provide a nice
user experience. The easiest way to use spire is via wildcard imports:

```tut
```scala mdoc:silent
import spire.algebra._ // provides algebraic type classes
import spire.math._ // provides functions, types, and type classes
import spire.implicits._ // provides infix operators, instances and conversions
Expand Down Expand Up @@ -358,33 +358,39 @@ encounter will occur at compile-time.

For example:

```tut
import spire.syntax.literals._
// bytes and shorts
val x = b"100" // without type annotation!
val y = h"999"
val mask = b"255" // unsigned constant converted to signed (-1)
// rationals
val n1 = r"1/3"
val n2 = r"1599/115866" // simplified at compile-time to 13/942
// support different radix literals
import spire.syntax.literals.radix._
// representations of the number 23
val a = x2"10111" // binary
val b = x8"27" // octal
val c = x16"17" // hex
```scala mdoc:silent
object LiteralsDemo {
import spire.syntax.literals._

// bytes and shorts
val x = b"100" // without type annotation!
val y = h"999"
val mask = b"255" // unsigned constant converted to signed (-1)

// rationals
val n1 = r"1/3"
val n2 = r"1599/115866" // simplified at compile-time to 13/942
}

// SI notation for large numbers
import spire.syntax.literals.si._ // .us and .eu also available
object RadixDemo {
// support different radix literals
import spire.syntax.literals.radix._

// representations of the number 23
val a = x2"10111" // binary
val b = x8"27" // octal
val c = x16"17" // hex
}

val w = i"1 944 234 123" // Int
val x = j"89 234 614 123 234 772" // Long
val y = big"123 234 435 456 567 678 234 123 112 234 345" // BigInt
val z = dec"1 234 456 789.123456789098765" // BigDecimal
object SIDemo {
// SI notation for large numbers
import spire.syntax.literals.si._ // .us and .eu also available

val w = i"1 944 234 123" // Int
val x = j"89 234 614 123 234 772" // Long
val y = big"123 234 435 456 567 678 234 123 112 234 345" // BigInt
val z = dec"1 234 456 789.123456789098765" // BigDecimal
}
```

Spire also provides a loop macro called `cfor` whose syntax bears a slight
Expand All @@ -394,15 +400,15 @@ tail-recursive function, which will inline literal function arguments.
The macro can be nested in itself and compares favorably with other looping
constructs in Scala such as `for` and `while`:

```tut
```scala mdoc:silent

// print numbers 1 through 10
cfor(0)(_ < 10, _ + 1) { i =>
println(i)
}

// naive sorting algorithm
def selectionSort(ns: Array[Int]) {
def selectionSort(ns: Array[Int]) = {
val limit = ns.length -1
cfor(0)(_ < limit, _ + 1) { i =>
var k = i
Expand Down Expand Up @@ -473,20 +479,22 @@ the `spire.random.Dist[A]` type class. These instances represent a
strategy for getting random values using a `Generator` instance. For
instance:

```tut
import spire.random.Dist
val rng = spire.random.rng.Cmwc5()
// produces a double in [0.0, 1.0)
val n = rng.next[Double]
// produces a complex number, with real and imaginary parts in [0.0, 1.0)
val c = rng.next[Complex[Double]]
// produces a map with ~10-20 entries
implicit val nextmap = Dist.map[Int, Complex[Double]](10, 20)
val m = rng.next[Map[Int, Complex[Double]]]
```scala mdoc:silent
object DistDemo {
import spire.random.Dist

val rng = spire.random.rng.Cmwc5()

// produces a map with ~10-20 entries
implicit val nextmap = Dist.map[Int, Complex[Double]](10, 20)
val m = rng.next[Map[Int, Complex[Double]]]

// produces a double in [0.0, 1.0)
val n = rng.next[Double]

// produces a complex number, with real and imaginary parts in [0.0, 1.0)
val q = rng.next[Complex[Double]]
}
```

Unlike generators, `Dist[A]` instances are immutable and composable,
Expand Down
2 changes: 1 addition & 1 deletion project/plugins.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ addSbtPlugin("org.scalastyle" %% "scalastyle-sbt-plugin" % "1.0.0")
addSbtPlugin("com.typesafe.sbt" % "sbt-git" % "1.0.1")
addSbtPlugin("org.xerial.sbt" % "sbt-sonatype" % "3.9.9")
addSbtPlugin("org.scala-js" % "sbt-scalajs" % "1.7.0")
addSbtPlugin("org.tpolecat" % "tut-plugin" % "0.6.13")
addSbtPlugin("org.scalameta" % "sbt-mdoc" % "2.2.21")
addSbtPlugin("net.virtual-void" % "sbt-dependency-graph" % "0.9.2")
addSbtPlugin("org.portable-scala" % "sbt-scalajs-crossproject" % "1.1.0")
addSbtPlugin("com.47deg" % "sbt-microsites" % "1.3.3")
Expand Down

0 comments on commit 41f69b8

Please sign in to comment.