Skip to content

Commit db5334a

Browse files
committed
Use jldoctest instead of doctest, remove empty lines
1 parent 581161e commit db5334a

File tree

1 file changed

+44
-66
lines changed

1 file changed

+44
-66
lines changed

doc/src/manual/missing.md

Lines changed: 44 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ The behavior of `missing` values follows one basic rule: `missing`
1515
values *propagate* automatically when passed to standard operators and functions,
1616
in particular mathematical functions. Uncertainty about the value of one of the operands
1717
induces uncertainty about the result. In practice, this means an operation involving
18-
a `missing` value generally returns `missing`:
19-
```doctest
18+
a `missing` value generally returns `missing`
19+
```jldoctest
2020
julia> missing + 1
2121
missing
2222
@@ -25,7 +25,6 @@ missing
2525
2626
julia> abs(missing)
2727
missing
28-
2928
```
3029

3130
As `missing` is a normal Julia object, this propagation rule only works
@@ -42,8 +41,8 @@ throws a `MethodError`, just like for any other type.
4241

4342
Standard equality and comparison operators follow the propagation rule presented
4443
above: if any of the operands is `missing`, the result is `missing`.
45-
Here are a few examples:
46-
```doctest
44+
Here are a few examples
45+
```jldoctest
4746
julia> missing == 1
4847
missing
4948
@@ -55,7 +54,6 @@ missing
5554
5655
julia> 2 >= missing
5756
missing
58-
5957
```
6058

6159
In particular, note that `missing == missing` returns `missing`, so `==` cannot
@@ -65,8 +63,8 @@ use [`ismissing(x)`](@ref).
6563
Special comparison operators [`isequal`](@ref) and [`===`](@ref) are exceptions
6664
to the propagation rule: they always return a `Bool` value, even in the presence
6765
of `missing` values, considering `missing` as equal to `missing` and as different
68-
from any other value. They can therefore be used to test whether a value is `missing`:
69-
```doctest
66+
from any other value. They can therefore be used to test whether a value is `missing`
67+
```jldoctest
7068
julia> missing === 1
7169
false
7270
@@ -78,14 +76,12 @@ true
7876
7977
julia> isequal(missing, missing)
8078
true
81-
8279
```
8380

8481
The [`isless`](@ref) operator is another exception: `missing` is considered
8582
as greater than any other value. This operator is used by [`sort`](@ref),
8683
which therefore places `missing` values after all other values.
87-
88-
```doctest
84+
```jldoctest
8985
julia> isless(1, missing)
9086
true
9187
@@ -94,7 +90,6 @@ false
9490
9591
julia> isless(missing, missing)
9692
false
97-
9893
```
9994

10095
## Logical operators
@@ -111,8 +106,8 @@ via concrete examples.
111106
Let us illustrate this principle with the logical "or" operator [`|`](@ref).
112107
Following the rules of boolean logic, if one of the operands is `true`,
113108
the value of the other operand does not have an influence on the result,
114-
which will always be `true`:
115-
```doctest
109+
which will always be `true`
110+
```jldoctest
116111
julia> true | true
117112
true
118113
@@ -121,28 +116,26 @@ true
121116
122117
julia> false | true
123118
true
124-
125119
```
126120

127121
Based on this observation, we can conclude that if one of the operands is `true`
128122
and the other `missing`, we know that the result is `true` in spite of the
129123
uncertainty about the actual value of one of the operands. If we had
130124
been able to observe the actual value of the second operand, it could only be
131125
`true` or `false`, and in both cases the result would be `true`. Therefore,
132-
in this particular case, missingness does *not* propagate:
133-
```doctest
126+
in this particular case, missingness does *not* propagate
127+
```jldoctest
134128
julia> true | missing
135129
true
136130
137131
julia> missing | true
138132
true
139-
140133
```
141134

142135
On the contrary, if one of the operands is `false`, the result could be either
143136
`true` or `false` depending on the value of the other operand. Therefore,
144-
if that operand is `missing`, the result has to be `missing` too:
145-
```doctest
137+
if that operand is `missing`, the result has to be `missing` too
138+
```jldoctest
146139
julia> false | true
147140
true
148141
@@ -157,14 +150,13 @@ missing
157150
158151
julia> missing | false
159152
missing
160-
161153
```
162154

163155
The behavior of the logical "and" operator [`&`](@ref) is similar to that of the
164156
`|` operator, with the difference that missingness does not propagate when
165157
one of the operands is `false`. For example, when that is the case of the first
166-
operand:
167-
```doctest
158+
operand
159+
```jldoctest
168160
julia> false & false
169161
false
170162
@@ -173,12 +165,11 @@ false
173165
174166
julia> false & missing
175167
false
176-
177168
```
178169

179170
On the other hand, missingness propagates when one of the operands is `true`,
180-
for example the first one:
181-
```doctest
171+
for example the first one
172+
```jldoctest
182173
julia> true & true
183174
true
184175
@@ -187,7 +178,6 @@ false
187178
188179
julia> true & missing
189180
missing
190-
191181
```
192182

193183
Finally, the "exclusive or" logical operator [`xor`](@ref) always propagates
@@ -202,20 +192,19 @@ Control flow operators including [`if`](@ref), [`while`](@ref) and the
202192
do not allow for missing values. This is because of the uncertainty about whether
203193
the actual value would be `true` or `false` if we could observe it,
204194
which implies that we do not know how the program should behave. A `TypeError`
205-
is thrown as soon as a `missing` value is encountered in this context:
206-
```doctest
195+
is thrown as soon as a `missing` value is encountered in this context
196+
```jldoctest
207197
julia> if missing
208198
println("here")
209199
end
210200
ERROR: TypeError: non-boolean (Missing) used in boolean context
211-
212201
```
213202

214203
For the same reason, contrary to logical operators presented above,
215204
the short-circuiting boolean operators [`&&`](@ref) and [`||`](@ref) do not
216205
allow for `missing` values in situations where the value of the operand
217-
determines whether the next operand is evaluated or not. For example:
218-
```doctest
206+
determines whether the next operand is evaluated or not. For example
207+
```jldoctest
219208
julia> missing || false
220209
ERROR: TypeError: non-boolean (Missing) used in boolean context
221210
@@ -224,31 +213,28 @@ ERROR: TypeError: non-boolean (Missing) used in boolean context
224213
225214
julia> true && missing && false
226215
ERROR: TypeError: non-boolean (Missing) used in boolean context
227-
228216
```
229217

230218
On the other hand, no error is thrown when the result can be determined without
231219
the `missing` values. This is the case when the code short-circuits
232220
before evaluating the `missing` operand, and when the `missing` operand is the
233-
last one:
234-
```doctest
221+
last one
222+
```jldoctest
235223
julia> true && missing
236224
missing
237225
238226
julia> false && missing
239227
false
240-
241228
```
242229

243230
## Arrays With Missing Values
244231

245-
Arrays containing missing values can be created like other arrays:
246-
```doctest
232+
Arrays containing missing values can be created like other arrays
233+
```jldoctest
247234
julia> [1, missing]
248235
2-element Array{Union{Missing, Int64},1}:
249236
1
250237
missing
251-
252238
```
253239

254240
As this example shows, the element type of such arrays is `Union{Missing, T}`,
@@ -260,20 +246,19 @@ of the entry (i.e. whether it is `Missing` or `T`).
260246

261247
Uninitialized arrays allowing for missing values can be constructed with the
262248
standard syntax. By default, arrays with an [`isbits`](@ref) element type are
263-
filled with `missing` values:
264-
```doctest
249+
filled with `missing` values
250+
```jldoctest
265251
julia> Array{Union{Missing, Int}}(uninitialized, 2, 3)
266252
2×3 Array{Union{Missing, Int64},2}:
267253
missing missing missing
268254
missing missing missing
269-
270255
```
271256

272257
An array allowing for `missing` values but which does not contain any such value
273258
can be converted back to an array which does not allow for missing values using
274259
[`convert`](@ref). If the array contains `missing` values, a `MethodError` is thrown
275-
during conversion:
276-
```doctest
260+
during conversion
261+
```jldoctest
277262
julia> x = Union{Missing, String}["a", "b"]
278263
2-element Array{Union{Missing, String},1}:
279264
"a"
@@ -294,28 +279,26 @@ ERROR: MethodError: Cannot `convert` an object of type Missing to an object of t
294279
This may have arisen from a call to the constructor String(...),
295280
since type constructors fall back to convert methods.
296281
Stacktrace:
297-
[...]
282+
[...
298283
```
299284
## Skipping Missing Values
300285

301286
Since `missing` values propagate with standard mathematical operators, reduction
302-
functions return `missing` when called on arrays which contain missing values:
303-
```doctest
287+
functions return `missing` when called on arrays which contain missing values
288+
```jldoctest
304289
julia> sum([1, missing])
305290
missing
306-
307291
```
308292

309-
In this situation, use the [`skipmissing`](@ref) function to skip missing values:
310-
```doctest
293+
In this situation, use the [`skipmissing`](@ref) function to skip missing values
294+
```jldoctest
311295
julia> sum(skipmissing([1, missing]))
312296
1
313-
314297
```
315298

316299
This convenience function returns an iterator which filters out `missing` values
317-
efficiently. It can therefore be used with any function which supports iterators:
318-
```doctest
300+
efficiently. It can therefore be used with any function which supports iterators
301+
```jldoctest
319302
julia> maximum(skipmissing([3, missing, 2, 1]))
320303
3
321304
@@ -324,17 +307,15 @@ julia> mean(skipmissing([3, missing, 2, 1]))
324307
325308
julia> mapreduce(sqrt, +, skipmissing([3, missing, 2, 1]))
326309
4.146264369941973
327-
328310
```
329311

330-
Use [`collect`](@ref) to extract non-`missing` values and store them in an array:
331-
```doctest
312+
Use [`collect`](@ref) to extract non-`missing` values and store them in an array
313+
```jldoctest
332314
julia> collect(skipmissing([3, missing, 2, 1]))
333315
3-element Array{Int64,1}:
334316
3
335317
2
336318
1
337-
338319
```
339320

340321
## Logical Operations on Arrays
@@ -345,8 +326,8 @@ the [`==`](@ref) operator return `missing` whenever the result cannot be
345326
determined without knowing the actual value of the `missing` entry. In practice,
346327
this means that `missing` is returned if all non-missing values of the compared
347328
arrays are equal, but one or both arrays contain missing values (possibly at
348-
different positions):
349-
```doctest
329+
different positions)
330+
```jldoctest
350331
julia> [1, missing] == [2, missing]
351332
false
352333
@@ -355,23 +336,21 @@ missing
355336
356337
julia> [1, 2, missing] == [1, missing, 2]
357338
missing
358-
359339
```
360340

361341
As for single values, use [`isequal`](@ref) to treat `missing` values as equal
362-
to other `missing` values but different from non-missing values:
363-
```doctest
342+
to other `missing` values but different from non-missing values
343+
```jldoctest
364344
julia> isequal([1, missing], [1, missing])
365345
true
366346
367347
julia> isequal([1, 2, missing], [1, missing, 2])
368348
false
369-
370349
```
371350

372351
Functions [`any`](@ref) and [`all`](@ref) also follow the rules of
373-
three-valued logic, returning `missing` when the result cannot be determined:
374-
```doctest
352+
three-valued logic, returning `missing` when the result cannot be determined
353+
```jldoctest
375354
julia> all([true, missing])
376355
missing
377356
@@ -383,5 +362,4 @@ true
383362
384363
julia> any([false, missing])
385364
missing
386-
387365
```

0 commit comments

Comments
 (0)