Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make code samples in the documentation copy-paste friendly #1057

Merged
merged 2 commits into from May 26, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions docs/src/main/tut/applicative.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ obvious. For `Option`, the `pure` operation wraps the value in
`Some`. For `List`, the `pure` operation returns a single element
`List`:

```tut
```tut:book
import cats._
import cats.std.all._

Expand All @@ -34,7 +34,7 @@ you compose one `Applicative` with another, the resulting `pure`
operation will lift the passed value into one context, and the result
into the other context:

```tut
```tut:book
(Applicative[List] compose Applicative[Option]).pure(1)
```

Expand Down
18 changes: 9 additions & 9 deletions docs/src/main/tut/apply.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ implicit val listApply: Apply[List] = new Apply[List] {

Since `Apply` extends `Functor`, we can use the `map` method from `Functor`:

```tut
```tut:book
Apply[Option].map(Some(1))(intToString)
Apply[Option].map(Some(1))(double)
Apply[Option].map(None)(double)
Expand All @@ -50,7 +50,7 @@ Apply[Option].map(None)(double)

And like functors, `Apply` instances also compose:

```tut
```tut:book
val listOpt = Apply[List] compose Apply[Option]
val plusOne = (x:Int) => x + 1
listOpt.ap(List(Some(plusOne)))(List(Some(1), None, Some(3)))
Expand All @@ -59,7 +59,7 @@ listOpt.ap(List(Some(plusOne)))(List(Some(1), None, Some(3)))
### ap
The `ap` method is a method that `Functor` does not have:

```tut
```tut:book
Apply[Option].ap(Some(intToString))(Some(1))
Apply[Option].ap(Some(double))(Some(1))
Apply[Option].ap(Some(double))(None)
Expand All @@ -74,7 +74,7 @@ accept `N` arguments where `ap` accepts `1`:

For example:

```tut
```tut:book
val addArity2 = (a: Int, b: Int) => a + b
Apply[Option].ap2(Some(addArity2))(Some(1), Some(2))

Expand All @@ -86,7 +86,7 @@ Note that if any of the arguments of this example is `None`, the
final result is `None` as well. The effects of the context we are operating on
are carried through the entire computation:

```tut
```tut:book
Apply[Option].ap2(Some(addArity2))(Some(1), None)
Apply[Option].ap4(None)(Some(1), Some(2), Some(3), Some(4))
```
Expand All @@ -95,7 +95,7 @@ Apply[Option].ap4(None)(Some(1), Some(2), Some(3), Some(4))

Similarly, `mapN` functions are available:

```tut
```tut:book
Apply[Option].map2(Some(1), Some(2))(addArity2)

Apply[Option].map3(Some(1), Some(2), Some(3))(addArity3)
Expand All @@ -105,7 +105,7 @@ Apply[Option].map3(Some(1), Some(2), Some(3))(addArity3)

And `tupleN`:

```tut
```tut:book
Apply[Option].tuple2(Some(1), Some(2))

Apply[Option].tuple3(Some(1), Some(2), Some(3))
Expand All @@ -118,7 +118,7 @@ functions (`apN`, `mapN` and `tupleN`).
In order to use it, first import `cats.syntax.all._` or `cats.syntax.cartesian._`.
Here we see that the following two functions, `f1` and `f2`, are equivalent:

```tut
```tut:book
import cats.syntax.cartesian._

def f1(a: Option[Int], b: Option[Int], c: Option[Int]) =
Expand All @@ -133,7 +133,7 @@ f2(Some(1), Some(2), Some(3))

All instances created by `|@|` have `map`, `ap`, and `tupled` methods of the appropriate arity:

```tut
```tut:book
val option2 = Option(1) |@| Option(2)
val option3 = option2 |@| Option.empty[Int]

Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/tut/contravariant.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ If we want to show a `Salary` instance, we can just convert it to a `Money` inst

Let's use `Show`'s `Contravariant`:

```tut
```tut:book
implicit val showSalary: Show[Salary] = showMoney.contramap(_.size)

Salary(Money(1000)).show
Expand All @@ -52,7 +52,7 @@ Salary(Money(1000)).show

`scala.math.Ordering` typeclass defines comparison operations, e.g. `compare`:

```tut
```tut:book
Ordering.Int.compare(2, 1)
Ordering.Int.compare(1, 2)
```
Expand All @@ -67,7 +67,7 @@ In fact, it is just `contramap`, defined in a slightly different way! We supply

So let's use it in our advantage and get `Ordering[Money]` for free:

```tut
```tut:book
// we need this for `<` to work
import scala.math.Ordered._

Expand Down
8 changes: 4 additions & 4 deletions docs/src/main/tut/foldable.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import cats.implicits._

And examples.

```tut
```tut:book
Foldable[List].fold(List("a", "b", "c"))
Foldable[List].foldMap(List(1, 2, 4))(_.toString)
Foldable[List].foldK(List(List(1,2,3), List(2,3,4)))
Expand Down Expand Up @@ -95,7 +95,7 @@ Scala's standard library might expect. This will prevent operations
which are lazy in their right hand argument to traverse the entire
structure unnecessarily. For example, if you have:

```tut
```tut:book
val allFalse = Stream.continually(false)
```

Expand All @@ -108,7 +108,7 @@ value. Using `foldRight` from the standard library *will* try to
consider the entire stream, and thus will eventually cause a stack
overflow:

```tut
```tut:book
try {
allFalse.foldRight(true)(_ && _)
} catch {
Expand All @@ -119,6 +119,6 @@ try {
With the lazy `foldRight` on `Foldable`, the calculation terminates
after looking at only one value:

```tut
```tut:book
Foldable[Stream].foldRight(allFalse, Eval.True)((a,b) => if (a) b else Eval.now(false)).value
```
4 changes: 2 additions & 2 deletions docs/src/main/tut/freeapplicative.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ val compiler =
}
```

```tut
```tut:book
val validator = prog.foldMap[FromString](compiler)
validator("1234")
validator("12345")
Expand Down Expand Up @@ -142,7 +142,7 @@ def logValidation[A](validation: Validation[A]): List[String] =
validation.foldMap[Log](logCompiler).getConst
```

```tut
```tut:book
logValidation(prog)
logValidation(size(5) *> hasNumber *> size(10))
logValidation((hasNumber |@| size(3)).map(_ || _))
Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/tut/freemonad.md
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ under `Monad`). As `Id` is a `Monad`, we can use `foldMap`.

To run your `Free` with previous `impureCompiler`:

```tut
```tut:book
val result: Option[Int] = program.foldMap(impureCompiler)
```

Expand Down Expand Up @@ -291,7 +291,7 @@ val pureCompiler: KVStoreA ~> KVStoreState = new (KVStoreA ~> KVStoreState) {
support for pattern matching is limited by the JVM's type erasure, but
it's not too hard to get around.)

```tut
```tut:book
val result: (Map[String, Any], Option[Int]) = program.foldMap(pureCompiler).run(Map.empty).value
```

Expand Down Expand Up @@ -403,7 +403,7 @@ Now if we run our program and type in "snuggles" when prompted, we see something
import DataSource._, Interacts._
```

```tut
```tut:book
val evaled: Unit = program.foldMap(interpreter)
```

Expand Down
12 changes: 6 additions & 6 deletions docs/src/main/tut/functor.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ This method takes a function `A => B` and turns an `F[A]` into an
method that exists on many classes in the Scala standard library, for
example:

```tut
```tut:book
Option(1).map(_ + 1)
List(1,2,3).map(_ + 1)
Vector(1,2,3).map(_.toString)
Expand Down Expand Up @@ -72,15 +72,15 @@ Without kind-projector, we'd have to write this as something like

`List` is a functor which applies the function to each element of the list:

```tut
```tut:book
val len: String => Int = _.length
Functor[List].map(List("qwer", "adsfg"))(len)
```

`Option` is a functor which only applies the function when the `Option` value
is a `Some`:

```tut
```tut:book
Functor[Option].map(Some("adsf"))(len) // Some(x) case: function is applied to x; result is wrapped in Some
Functor[Option].map(None)(len) // None case: simply returns None (function is not applied)
```
Expand All @@ -91,7 +91,7 @@ Functor[Option].map(None)(len) // None case: simply returns None (function is no

We can use `Functor` to "lift" a function from `A => B` to `F[A] => F[B]`:

```tut
```tut:book
val lenOption: Option[String] => Option[Int] = Functor[Option].lift(len)
lenOption(Some("abcd"))
```
Expand All @@ -101,7 +101,7 @@ lenOption(Some("abcd"))
`Functor` provides an `fproduct` function which pairs a value with the
result of applying a function to that value.

```tut
```tut:book
val source = List("a", "aa", "b", "ccccc")
Functor[List].fproduct(source)(len).toMap
```
Expand All @@ -111,7 +111,7 @@ Functor[List].fproduct(source)(len).toMap
Functors compose! Given any functor `F[_]` and any functor `G[_]` we can
create a new functor `F[G[_]]` by composing them:

```tut
```tut:book
val listOpt = Functor[List] compose Functor[Option]
listOpt.map(List(Some(1), None, Some(3)))(_ + 1)
val optList = Functor[Option] compose Functor[List]
Expand Down
8 changes: 4 additions & 4 deletions docs/src/main/tut/id.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ That is to say that the type Id[A] is just a synonym for A. We can
freely treat values of type `A` as values of type `Id[A]`, and
vice-versa.

```tut
```tut:book
import cats._

val x: Id[Int] = 1
Expand All @@ -34,7 +34,7 @@ method, which has type `A => Id[A]` just becomes the identity
function. The `map` method from `Functor` just becomes function
application:

```tut
```tut:book
import cats.Functor

val one: Int = 1
Expand All @@ -56,7 +56,7 @@ two functions the same, and, in fact, they can have the same
implementation, meaning that for `Id`, `flatMap` is also just function
application:

```tut
```tut:book
import cats.Monad

val one: Int = 1
Expand All @@ -66,7 +66,7 @@ Monad[Id].flatMap(one)(_ + 1)

And that similarly, coflatMap is just function application:

```tut
```tut:book
import cats.Comonad

Comonad[Id].coflatMap(one)(_ + 1)
Expand Down
2 changes: 1 addition & 1 deletion docs/src/main/tut/invariant.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,6 @@ val today: Date = longToDate(1449088684104l)
val timeLeft: Date = longToDate(1900918893l)
```

```tut
```tut:book
today |+| timeLeft
```
2 changes: 1 addition & 1 deletion docs/src/main/tut/kleisli.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ val twiceAsManyCats: Int => String =

Thus.

```tut
```tut:book
twiceAsManyCats(1) // "2 cats"
```

Expand Down
6 changes: 3 additions & 3 deletions docs/src/main/tut/monad.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ that we have a single context (ie. `F[A]`).
The name `flatten` should remind you of the functions of the same name on many
classes in the standard library.

```tut
```tut:book
Option(Option(1)).flatten
Option(None).flatten
List(List(1),List(2,3)).flatten
Expand Down Expand Up @@ -63,7 +63,7 @@ Part of the reason for this is that name `flatMap` has special significance in
scala, as for-comprehensions rely on this method to chain together operations
in a monadic context.

```tut
```tut:book
import scala.reflect.runtime.universe

universe.reify(
Expand All @@ -80,7 +80,7 @@ universe.reify(
the results of earlier ones. This is embodied in `ifM`, which lifts an `if`
statement into the monadic context.

```tut
```tut:book
Monad[List].ifM(List(true, false, true))(List(1, 2), List(3, 4))
```

Expand Down
8 changes: 4 additions & 4 deletions docs/src/main/tut/monoid.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ import cats.implicits._

Examples.

```tut
```tut:book
Monoid[String].empty
Monoid[String].combineAll(List("a", "b", "c"))
Monoid[String].combineAll(List())
Expand All @@ -44,7 +44,7 @@ The advantage of using these type class provided methods, rather than the
specific ones for each type, is that we can compose monoids to allow us to
operate on more complex types, e.g.

```tut
```tut:book
Monoid[Map[String,Int]].combineAll(List(Map("a" -> 1, "b" -> 2), Map("a" -> 3)))
Monoid[Map[String,Int]].combineAll(List())
```
Expand All @@ -53,7 +53,7 @@ This is also true if we define our own instances. As an example, let's use
[`Foldable`](foldable.html)'s `foldMap`, which maps over values accumulating
the results, using the available `Monoid` for the type mapped onto.

```tut
```tut:book
val l = List(1, 2, 3, 4, 5)
l.foldMap(identity)
l.foldMap(i => i.toString)
Expand All @@ -64,7 +64,7 @@ with a function that produces a tuple, cats also provides a `Monoid` for a tuple
that will be valid for any tuple where the types it contains also have a
`Monoid` available, thus.

```tut
```tut:book
l.foldMap(i => (i, i.toString)) // do both of the above in one pass, hurrah!
```

Expand Down
Loading