Skip to content

Commit 930e22c

Browse files
wbolsterbehackett
authored andcommitted
Improve legibility of map/reduce code examples
Use triple-quoted multiline strings in the examples, so that the examples are more readable. It also makes copy/pasting (e.g. to a mongo shell) a lot easier.
1 parent 49ebd97 commit 930e22c

1 file changed

Lines changed: 17 additions & 13 deletions

File tree

doc/examples/map_reduce.rst

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -49,23 +49,27 @@ the array:
4949
.. doctest::
5050

5151
>>> from bson.code import Code
52-
>>> mapper = Code("function () {"
53-
... " this.tags.forEach(function(z) {"
54-
... " emit(z, 1);"
55-
... " });"
56-
... "}")
52+
>>> mapper = Code("""
53+
... function () {
54+
... this.tags.forEach(function(z) {
55+
... emit(z, 1);
56+
... });
57+
... }
58+
... """)
5759

5860
The **reduce** function sums over all of the emitted values for a given key:
5961

6062
.. doctest::
6163

62-
>>> reducer = Code("function (key, values) {"
63-
... " var total = 0;"
64-
... " for (var i = 0; i < values.length; i++) {"
65-
... " total += values[i];"
66-
... " }"
67-
... " return total;"
68-
... "}")
64+
>>> reducer = Code("""
65+
... function (key, values) {
66+
... var total = 0;
67+
... for (var i = 0; i < values.length; i++) {
68+
... total += values[i];
69+
... }
70+
... return total;
71+
... }
72+
... """)
6973

7074
.. note:: We can't just return ``values.length`` as the **reduce** function
7175
might be called iteratively on the results of other reduce steps.
@@ -75,7 +79,7 @@ iterate over the result collection:
7579

7680
.. doctest::
7781

78-
>>> result = db.things.map_reduce(mapper, reduce, "myresults")
82+
>>> result = db.things.map_reduce(mapper, reducer, "myresults")
7983
>>> for doc in result.find():
8084
... print doc
8185
...

0 commit comments

Comments
 (0)