Skip to content

Commit

Permalink
Clean up applicative syntax doc (#2036)
Browse files Browse the repository at this point in the history
The code was updated to use the new syntax, but the text no longer matched up with the code. This brings them in sync once again.
  • Loading branch information
bkirwi authored and johnynek committed Nov 23, 2017
1 parent ab5433e commit 9020258
Showing 1 changed file with 12 additions and 15 deletions.
27 changes: 12 additions & 15 deletions docs/src/main/tut/typeclasses/applicative.md
Original file line number Diff line number Diff line change
Expand Up @@ -251,30 +251,27 @@ pair up (or apply functions to) values with the same key. Hence `Map[K, ?]` has
## Syntax

Syntax for `Applicative` (or `Apply`) is available under the `cats.implicits._` import. The most
interesting syntax is focused on composing independent effects - there are two options for this.
interesting syntax is focused on composing independent effects: it works just like the methods
for composition we saw above (`map3`, `tuple3`, etc.), but achieves a slightly friendlier syntax
by enriching Scala's standard tuple types.

The first is the builder syntax which incrementally builds up a collection of effects until a
function is applied to compose them.

```tut:book:silent
import cats.implicits._
val o1: Option[Int] = Some(42)
val o2: Option[String] = Some("hello")
```
For example, we've already seen this code for mapping over three options together:

```tut:book
(o1, o2).mapN((i: Int, s: String) => i.toString ++ s)
(o1, o2).tupled
Applicative[Option].map3(username, password, url)(attemptConnect)
```

The second expects the effects in a tuple and works by enriching syntax on top of the existing
`TupleN` types.
With the applicative syntax, we can change this to the slightly shorter:

```tut:book
(o1, o2).mapN((i: Int, s: String) => i.toString ++ s)
import cats.implicits._
(username, password, url).mapN(attemptConnect)
```

We don't have to mention the type or specify the number of values we're composing
together, so there's a little less boilerplate here.

## Further Reading

* [Applicative Programming with Effects][applicativePaper] - McBride, Patterson. JFP 2008.
Expand Down

0 comments on commit 9020258

Please sign in to comment.