• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

JuliaLang / julia / #37527

pending completion
#37527

push

local

web-flow
make `IRShow.method_name` inferrable (#49607)

18 of 18 new or added lines in 3 files covered. (100.0%)

68710 of 81829 relevant lines covered (83.97%)

33068903.12 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

92.47
/base/operators.jl
1
# This file is a part of Julia. License is MIT: https://julialang.org/license
2

3
## types ##
4

5
"""
6
    <:(T1, T2)
7

8
Subtype operator: returns `true` if and only if all values of type `T1` are
9
also of type `T2`.
10

11
# Examples
12
```jldoctest
13
julia> Float64 <: AbstractFloat
14
true
15

16
julia> Vector{Int} <: AbstractArray
17
true
18

19
julia> Matrix{Float64} <: Matrix{AbstractFloat}
20
false
21
```
22
"""
23
(<:)
24

25
"""
26
    >:(T1, T2)
27

28
Supertype operator, equivalent to `T2 <: T1`.
29
"""
30
(>:)(@nospecialize(a), @nospecialize(b)) = (b <: a)
13✔
31

32
"""
33
    supertype(T::DataType)
34

35
Return the supertype of DataType `T`.
36

37
# Examples
38
```jldoctest
39
julia> supertype(Int32)
40
Signed
41
```
42
"""
43
supertype(T::DataType) = (@_total_meta; T.super)
4,862,739✔
44
supertype(T::UnionAll) = (@_total_meta; UnionAll(T.var, supertype(T.body)))
14✔
45

46
## generic comparison ##
47

48
"""
49
    ==(x, y)
50

51
Generic equality operator. Falls back to [`===`](@ref).
52
Should be implemented for all types with a notion of equality, based on the abstract value
53
that an instance represents. For example, all numeric types are compared by numeric value,
54
ignoring type. Strings are compared as sequences of characters, ignoring encoding.
55
For collections, `==` is generally called recursively on all contents,
56
though other properties (like the shape for arrays) may also be taken into account.
57

58
This operator follows IEEE semantics for floating-point numbers: `0.0 == -0.0` and
59
`NaN != NaN`.
60

61
The result is of type `Bool`, except when one of the operands is [`missing`](@ref),
62
in which case `missing` is returned
63
([three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic)).
64
For collections, `missing` is returned if at least one of the operands contains
65
a `missing` value and all non-missing values are equal.
66
Use [`isequal`](@ref) or [`===`](@ref) to always get a `Bool` result.
67

68
# Implementation
69
New numeric types should implement this function for two arguments of the new type, and
70
handle comparison to other types via promotion rules where possible.
71

72
[`isequal`](@ref) falls back to `==`, so new methods of `==` will be used by the
73
[`Dict`](@ref) type to compare keys. If your type will be used as a dictionary key, it
74
should therefore also implement [`hash`](@ref).
75

76
If some type defines `==`, [`isequal`](@ref), and [`isless`](@ref) then it should
77
also implement [`<`](@ref) to ensure consistency of comparisons.
78
"""
79
==
80

81
"""
82
    isequal(x, y)
83

84
Similar to [`==`](@ref), except for the treatment of floating point numbers
85
and of missing values. `isequal` treats all floating-point `NaN` values as equal
86
to each other, treats `-0.0` as unequal to `0.0`, and [`missing`](@ref) as equal
87
to `missing`. Always returns a `Bool` value.
88

89
`isequal` is an equivalence relation - it is reflexive (`===` implies `isequal`), symmetric
90
(`isequal(a, b)` implies `isequal(b, a)`) and transitive (`isequal(a, b)` and
91
`isequal(b, c)` implies `isequal(a, c)`).
92

93
# Implementation
94
The default implementation of `isequal` calls `==`, so a type that does not involve
95
floating-point values generally only needs to define `==`.
96

97
`isequal` is the comparison function used by hash tables (`Dict`). `isequal(x,y)` must imply
98
that `hash(x) == hash(y)`.
99

100
This typically means that types for which a custom `==` or `isequal` method exists must
101
implement a corresponding [`hash`](@ref) method (and vice versa). Collections typically
102
implement `isequal` by calling `isequal` recursively on all contents.
103

104
Furthermore, `isequal` is linked with [`isless`](@ref), and they work together to
105
define a fixed total ordering, where exactly one of `isequal(x, y)`, `isless(x, y)`, or
106
`isless(y, x)` must be `true` (and the other two `false`).
107

108
Scalar types generally do not need to implement `isequal` separate from `==`, unless they
109
represent floating-point numbers amenable to a more efficient implementation than that
110
provided as a generic fallback (based on `isnan`, `signbit`, and `==`).
111

112
# Examples
113
```jldoctest
114
julia> isequal([1., NaN], [1., NaN])
115
true
116

117
julia> [1., NaN] == [1., NaN]
118
false
119

120
julia> 0.0 == -0.0
121
true
122

123
julia> isequal(0.0, -0.0)
124
false
125

126
julia> missing == missing
127
missing
128

129
julia> isequal(missing, missing)
130
true
131
```
132
"""
133
isequal(x, y) = (x == y)::Bool # all `missing` cases are handled in missing.jl
3,502,413✔
134

135
signequal(x, y) = signbit(x)::Bool == signbit(y)::Bool
337,863✔
136
signless(x, y) = signbit(x)::Bool & !signbit(y)::Bool
29,859✔
137

138
isequal(x::AbstractFloat, y::AbstractFloat) = (isnan(x) & isnan(y)) | signequal(x, y) & (x == y)::Bool
243,071✔
139
isequal(x::Real,          y::AbstractFloat) = (isnan(x) & isnan(y)) | signequal(x, y) & (x == y)::Bool
93,328✔
140
isequal(x::AbstractFloat, y::Real         ) = (isnan(x) & isnan(y)) | signequal(x, y) & (x == y)::Bool
1,496✔
141

142
"""
143
    isless(x, y)
144

145
Test whether `x` is less than `y`, according to a fixed total order (defined together with
146
[`isequal`](@ref)). `isless` is not defined on all pairs of values `(x, y)`. However, if it
147
is defined, it is expected to satisfy the following:
148
- If `isless(x, y)` is defined, then so is `isless(y, x)` and `isequal(x, y)`,
149
  and exactly one of those three yields `true`.
150
- The relation defined by `isless` is transitive, i.e.,
151
  `isless(x, y) && isless(y, z)` implies `isless(x, z)`.
152

153
Values that are normally unordered, such as `NaN`,
154
are ordered after regular values.
155
[`missing`](@ref) values are ordered last.
156

157
This is the default comparison used by [`sort`](@ref).
158

159
# Implementation
160
Non-numeric types with a total order should implement this function.
161
Numeric types only need to implement it if they have special values such as `NaN`.
162
Types with a partial order should implement [`<`](@ref).
163
See the documentation on [Alternate orderings](@ref) for how to define alternate
164
ordering methods that can be used in sorting and related functions.
165

166
# Examples
167
```jldoctest
168
julia> isless(1, 3)
169
true
170

171
julia> isless("Red", "Blue")
172
false
173
```
174
"""
175
function isless end
176

177
isless(x::AbstractFloat, y::AbstractFloat) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)
3,482✔
178
isless(x::Real,          y::AbstractFloat) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)
19,279✔
179
isless(x::AbstractFloat, y::Real         ) = (!isnan(x) & (isnan(y) | signless(x, y))) | (x < y)
8,070✔
180

181
# Performance optimization to reduce branching
182
# This is useful for sorting tuples of integers
183
# TODO: remove this when the compiler can optimize the generic version better
184
# See #48724 and #48753
185
isless(a::Tuple{BitInteger, BitInteger}, b::Tuple{BitInteger, BitInteger}) =
28,442✔
186
    isless(a[1], b[1]) | (isequal(a[1], b[1]) & isless(a[2], b[2]))
187

188
"""
189
    isgreater(x, y)
190

191
Not the inverse of `isless`! Test whether `x` is greater than `y`, according to
192
a fixed total order compatible with `min`.
193

194
Defined with `isless`, this function is usually `isless(y, x)`, but `NaN` and
195
[`missing`](@ref) are ordered as smaller than any regular value with `missing`
196
smaller than `NaN`.
197

198
So `isless` defines an ascending total order with `NaN` and `missing` as the
199
largest values and `isgreater` defines a descending total order with `NaN` and
200
`missing` as the smallest values.
201

202
!!! note
203

204
    Like `min`, `isgreater` orders containers (tuples, vectors, etc)
205
    lexicographically with `isless(y, x)` rather than recursively with itself:
206

207
    ```jldoctest
208
    julia> Base.isgreater(1, NaN) # 1 is greater than NaN
209
    true
210

211
    julia> Base.isgreater((1,), (NaN,)) # But (1,) is not greater than (NaN,)
212
    false
213

214
    julia> sort([1, 2, 3, NaN]; lt=Base.isgreater)
215
    4-element Vector{Float64}:
216
       3.0
217
       2.0
218
       1.0
219
     NaN
220

221
    julia> sort(tuple.([1, 2, 3, NaN]); lt=Base.isgreater)
222
    4-element Vector{Tuple{Float64}}:
223
     (NaN,)
224
     (3.0,)
225
     (2.0,)
226
     (1.0,)
227
    ```
228

229
# Implementation
230
This is unexported. Types should not usually implement this function. Instead, implement `isless`.
231
"""
232
isgreater(x, y) = isunordered(x) || isunordered(y) ? isless(x, y) : isless(y, x)
129,541✔
233

234
"""
235
    isunordered(x)
236

237
Return `true` if `x` is a value that is not orderable according to [`<`](@ref), such as `NaN`
238
or `missing`.
239

240
The values that evaluate to `true` with this predicate may be orderable with respect to other
241
orderings such as [`isless`](@ref).
242

243
!!! compat "Julia 1.7"
244
    This function requires Julia 1.7 or later.
245
"""
246
isunordered(x) = false
157✔
247
isunordered(x::AbstractFloat) = isnan(x)
128,792✔
248
isunordered(x::Missing) = true
170✔
249

250
==(T::Type, S::Type) = (@_total_meta; ccall(:jl_types_equal, Cint, (Any, Any), T, S) != 0)
6,285,151✔
251
!=(T::Type, S::Type) = (@_total_meta; !(T == S))
38,571✔
252
==(T::TypeVar, S::Type) = false
×
253
==(T::Type, S::TypeVar) = false
×
254

255
## comparison fallbacks ##
256

257
"""
258
    !=(x, y)
259
    ≠(x,y)
260

261
Not-equals comparison operator. Always gives the opposite answer as [`==`](@ref).
262

263
# Implementation
264
New types should generally not implement this, and rely on the fallback definition
265
`!=(x,y) = !(x==y)` instead.
266

267
# Examples
268
```jldoctest
269
julia> 3 != 2
270
true
271

272
julia> "foo" ≠ "foo"
273
false
274
```
275
"""
276
!=(x, y) = !(x == y)
7,175,505,117✔
277
const ≠ = !=
278

279
"""
280
    ===(x,y) -> Bool
281
    ≡(x,y) -> Bool
282

283
Determine whether `x` and `y` are identical, in the sense that no program could distinguish
284
them. First the types of `x` and `y` are compared. If those are identical, mutable objects
285
are compared by address in memory and immutable objects (such as numbers) are compared by
286
contents at the bit level. This function is sometimes called "egal".
287
It always returns a `Bool` value.
288

289
# Examples
290
```jldoctest
291
julia> a = [1, 2]; b = [1, 2];
292

293
julia> a == b
294
true
295

296
julia> a === b
297
false
298

299
julia> a === a
300
true
301
```
302
"""
303
===
304
const ≡ = ===
305

306
"""
307
    !==(x, y)
308
    ≢(x,y)
309

310
Always gives the opposite answer as [`===`](@ref).
311

312
# Examples
313
```jldoctest
314
julia> a = [1, 2]; b = [1, 2];
315

316
julia> a ≢ b
317
true
318

319
julia> a ≢ a
320
false
321
```
322
"""
323
!==(@nospecialize(x), @nospecialize(y)) = !(x === y)
32,998✔
324
const ≢ = !==
325

326
"""
327
    <(x, y)
328

329
Less-than comparison operator. Falls back to [`isless`](@ref).
330
Because of the behavior of floating-point NaN values, this operator implements
331
a partial order.
332

333
# Implementation
334
New types with a canonical partial order should implement this function for
335
two arguments of the new type.
336
Types with a canonical total order should implement [`isless`](@ref) instead.
337

338
# Examples
339
```jldoctest
340
julia> 'a' < 'b'
341
true
342

343
julia> "abc" < "abd"
344
true
345

346
julia> 5 < 3
347
false
348
```
349
"""
350
<(x, y) = isless(x, y)
16,944,041✔
351

352
"""
353
    >(x, y)
354

355
Greater-than comparison operator. Falls back to `y < x`.
356

357
# Implementation
358
Generally, new types should implement [`<`](@ref) instead of this function,
359
and rely on the fallback definition `>(x, y) = y < x`.
360

361
# Examples
362
```jldoctest
363
julia> 'a' > 'b'
364
false
365

366
julia> 7 > 3 > 1
367
true
368

369
julia> "abc" > "abd"
370
false
371

372
julia> 5 > 3
373
true
374
```
375
"""
376
>(x, y) = y < x
19,508,239,440✔
377

378
"""
379
    <=(x, y)
380
    ≤(x,y)
381

382
Less-than-or-equals comparison operator. Falls back to `(x < y) | (x == y)`.
383

384
# Examples
385
```jldoctest
386
julia> 'a' <= 'b'
387
true
388

389
julia> 7 ≤ 7 ≤ 9
390
true
391

392
julia> "abc" ≤ "abc"
393
true
394

395
julia> 5 <= 3
396
false
397
```
398
"""
399
<=(x, y) = (x < y) | (x == y)
16,743,314✔
400
const ≤ = <=
401

402
"""
403
    >=(x, y)
404
    ≥(x,y)
405

406
Greater-than-or-equals comparison operator. Falls back to `y <= x`.
407

408
# Examples
409
```jldoctest
410
julia> 'a' >= 'b'
411
false
412

413
julia> 7 ≥ 7 ≥ 3
414
true
415

416
julia> "abc" ≥ "abc"
417
true
418

419
julia> 5 >= 3
420
true
421
```
422
"""
423
>=(x, y) = (y <= x)
10,231,504,984✔
424
const ≥ = >=
425

426
# this definition allows Number types to implement < instead of isless,
427
# which is more idiomatic:
428
isless(x::Real, y::Real) = x<y
232,433,072✔
429

430
"""
431
    cmp(x,y)
432

433
Return -1, 0, or 1 depending on whether `x` is less than, equal to, or greater than `y`,
434
respectively. Uses the total order implemented by `isless`.
435

436
# Examples
437
```jldoctest
438
julia> cmp(1, 2)
439
-1
440

441
julia> cmp(2, 1)
442
1
443

444
julia> cmp(2+im, 3-im)
445
ERROR: MethodError: no method matching isless(::Complex{Int64}, ::Complex{Int64})
446
[...]
447
```
448
"""
449
cmp(x, y) = isless(x, y) ? -1 : ifelse(isless(y, x), 1, 0)
33✔
450

451
"""
452
    cmp(<, x, y)
453

454
Return -1, 0, or 1 depending on whether `x` is less than, equal to, or greater than `y`,
455
respectively. The first argument specifies a less-than comparison function to use.
456
"""
457
cmp(<, x, y) = (x < y) ? -1 : ifelse(y < x, 1, 0)
5✔
458

459
# cmp returns -1, 0, +1 indicating ordering
460
cmp(x::Integer, y::Integer) = ifelse(isless(x, y), -1, ifelse(isless(y, x), 1, 0))
71,200✔
461

462
"""
463
    max(x, y, ...)
464

465
Return the maximum of the arguments (with respect to [`isless`](@ref)). See also the [`maximum`](@ref) function
466
to take the maximum element from a collection.
467

468
# Examples
469
```jldoctest
470
julia> max(2, 5, 1)
471
5
472
```
473
"""
474
max(x, y) = ifelse(isless(y, x), x, y)
7,448✔
475

476
"""
477
    min(x, y, ...)
478

479
Return the minimum of the arguments (with respect to [`isless`](@ref)). See also the [`minimum`](@ref) function
480
to take the minimum element from a collection.
481

482
# Examples
483
```jldoctest
484
julia> min(2, 5, 1)
485
1
486
```
487
"""
488
min(x,y) = ifelse(isless(y, x), y, x)
7,197✔
489

490
"""
491
    minmax(x, y)
492

493
Return `(min(x,y), max(x,y))`.
494

495
See also [`extrema`](@ref) that returns `(minimum(x), maximum(x))`.
496

497
# Examples
498
```jldoctest
499
julia> minmax('c','b')
500
('b', 'c')
501
```
502
"""
503
minmax(x,y) = isless(y, x) ? (y, x) : (x, y)
16✔
504

505
## definitions providing basic traits of arithmetic operators ##
506

507
"""
508
    identity(x)
509

510
The identity function. Returns its argument.
511

512
See also: [`one`](@ref), [`oneunit`](@ref), and [`LinearAlgebra`](@ref man-linalg)'s `I`.
513

514
# Examples
515
```jldoctest
516
julia> identity("Well, what did you expect?")
517
"Well, what did you expect?"
518
```
519
"""
520
identity(@nospecialize x) = x
60,953,593✔
521

522
+(x::Number) = x
45,610✔
523
*(x::Number) = x
14✔
524
(&)(x::Integer) = x
×
525
(|)(x::Integer) = x
1✔
526
xor(x::Integer) = x
149✔
527

528
const ⊻ = xor
529
const ⊼ = nand
530
const ⊽ = nor
531

532
# foldl for argument lists. expand fully up to a point, then
533
# switch to a loop. this allows small cases like `a+b+c+d` to be managed
534
# efficiently, without a major slowdown for `+(x...)` when `x` is big.
535
# n.b.: keep this method count small, so it can be inferred without hitting the
536
# method count limit in inference
537
afoldl(op, a) = a
402,738,164✔
538
function afoldl(op, a, bs...)
6,546,455✔
539
    l = length(bs)
6,546,373✔
540
    i =  0; y = a;            l == i && return y
19,639,119✔
541
    #@nexprs 31 i -> (y = op(y, bs[i]); l == i && return y)
542
    i =  1; y = op(y, bs[i]); l == i && return y
75,648,638✔
543
    i =  2; y = op(y, bs[i]); l == i && return y
58,209,040✔
544
    i =  3; y = op(y, bs[i]); l == i && return y
375,430✔
545
    i =  4; y = op(y, bs[i]); l == i && return y
143,934✔
546
    i =  5; y = op(y, bs[i]); l == i && return y
29,905✔
547
    i =  6; y = op(y, bs[i]); l == i && return y
1,846✔
548
    i =  7; y = op(y, bs[i]); l == i && return y
1,479✔
549
    i =  8; y = op(y, bs[i]); l == i && return y
1,198✔
550
    i =  9; y = op(y, bs[i]); l == i && return y
449✔
551
    i = 10; y = op(y, bs[i]); l == i && return y
295✔
552
    i = 11; y = op(y, bs[i]); l == i && return y
251✔
553
    i = 12; y = op(y, bs[i]); l == i && return y
208✔
554
    i = 13; y = op(y, bs[i]); l == i && return y
161✔
555
    i = 14; y = op(y, bs[i]); l == i && return y
149✔
556
    i = 15; y = op(y, bs[i]); l == i && return y
142✔
557
    i = 16; y = op(y, bs[i]); l == i && return y
135✔
558
    i = 17; y = op(y, bs[i]); l == i && return y
118✔
559
    i = 18; y = op(y, bs[i]); l == i && return y
114✔
560
    i = 19; y = op(y, bs[i]); l == i && return y
112✔
561
    i = 20; y = op(y, bs[i]); l == i && return y
105✔
562
    i = 21; y = op(y, bs[i]); l == i && return y
98✔
563
    i = 22; y = op(y, bs[i]); l == i && return y
92✔
564
    i = 23; y = op(y, bs[i]); l == i && return y
89✔
565
    i = 24; y = op(y, bs[i]); l == i && return y
86✔
566
    i = 25; y = op(y, bs[i]); l == i && return y
83✔
567
    i = 26; y = op(y, bs[i]); l == i && return y
80✔
568
    i = 27; y = op(y, bs[i]); l == i && return y
77✔
569
    i = 28; y = op(y, bs[i]); l == i && return y
74✔
570
    i = 29; y = op(y, bs[i]); l == i && return y
68✔
571
    i = 30; y = op(y, bs[i]); l == i && return y
65✔
572
    i = 31; y = op(y, bs[i]); l == i && return y
54✔
573
    for i in (i + 1):l
27✔
574
        y = op(y, bs[i])
118✔
575
    end
219✔
576
    return y
17✔
577
end
578
setfield!(typeof(afoldl).name.mt, :max_args, 34, :monotonic)
579

580
for op in (:+, :*, :&, :|, :xor, :min, :max, :kron)
581
    @eval begin
582
        # note: these definitions must not cause a dispatch loop when +(a,b) is
583
        # not defined, and must only try to call 2-argument definitions, so
584
        # that defining +(a,b) is sufficient for full functionality.
585
        ($op)(a, b, c, xs...) = (@inline; afoldl($op, ($op)(($op)(a,b),c), xs...))
1,855,423,809✔
586
        # a further concern is that it's easy for a type like (Int,Int...)
587
        # to match many definitions, so we need to keep the number of
588
        # definitions down to avoid losing type information.
589
    end
590
end
591

592
function kron! end
593

594
const var"'" = adjoint
595

596
"""
597
    \\(x, y)
598

599
Left division operator: multiplication of `y` by the inverse of `x` on the left. Gives
600
floating-point results for integer arguments.
601

602
# Examples
603
```jldoctest
604
julia> 3 \\ 6
605
2.0
606

607
julia> inv(3) * 6
608
2.0
609

610
julia> A = [4 3; 2 1]; x = [5, 6];
611

612
julia> A \\ x
613
2-element Vector{Float64}:
614
  6.5
615
 -7.0
616

617
julia> inv(A) * x
618
2-element Vector{Float64}:
619
  6.5
620
 -7.0
621
```
622
"""
623
\(x,y) = adjoint(adjoint(y)/adjoint(x))
375,303✔
624

625
# Core <<, >>, and >>> take either Int or UInt as second arg. Signed shift
626
# counts can shift in either direction, and are translated here to unsigned
627
# counts. Integer datatypes only need to implement the unsigned version.
628

629
"""
630
    <<(x, n)
631

632
Left bit shift operator, `x << n`. For `n >= 0`, the result is `x` shifted left
633
by `n` bits, filling with `0`s. This is equivalent to `x * 2^n`. For `n < 0`,
634
this is equivalent to `x >> -n`.
635

636
# Examples
637
```jldoctest
638
julia> Int8(3) << 2
639
12
640

641
julia> bitstring(Int8(3))
642
"00000011"
643

644
julia> bitstring(Int8(12))
645
"00001100"
646
```
647
See also [`>>`](@ref), [`>>>`](@ref), [`exp2`](@ref), [`ldexp`](@ref).
648
"""
649
function <<(x::Integer, c::Integer)
49,295✔
650
    @inline
49,295✔
651
    typemin(Int) <= c <= typemax(Int) && return x << (c % Int)
49,295✔
652
    (x >= 0 || c >= 0) && return zero(x) << 0  # for type stability
×
653
    oftype(x, -1)
×
654
end
655
function <<(x::Integer, c::Unsigned)
9✔
656
    @inline
9✔
657
    if c isa UInt
9✔
658
        throw(MethodError(<<, (x, c)))
1✔
659
    end
660
    c <= typemax(UInt) ? x << (c % UInt) : zero(x) << UInt(0)
8✔
661
end
662
<<(x::Integer, c::Int) = c >= 0 ? x << unsigned(c) : x >> unsigned(-c)
941✔
663

664
"""
665
    >>(x, n)
666

667
Right bit shift operator, `x >> n`. For `n >= 0`, the result is `x` shifted
668
right by `n` bits, filling with `0`s if `x >= 0`, `1`s if `x < 0`, preserving
669
the sign of `x`. This is equivalent to `fld(x, 2^n)`. For `n < 0`, this is
670
equivalent to `x << -n`.
671

672
# Examples
673
```jldoctest
674
julia> Int8(13) >> 2
675
3
676

677
julia> bitstring(Int8(13))
678
"00001101"
679

680
julia> bitstring(Int8(3))
681
"00000011"
682

683
julia> Int8(-14) >> 2
684
-4
685

686
julia> bitstring(Int8(-14))
687
"11110010"
688

689
julia> bitstring(Int8(-4))
690
"11111100"
691
```
692
See also [`>>>`](@ref), [`<<`](@ref).
693
"""
694
function >>(x::Integer, c::Integer)
93✔
695
    @inline
93✔
696
    if c isa UInt
93✔
697
        throw(MethodError(>>, (x, c)))
1✔
698
    end
699
    typemin(Int) <= c <= typemax(Int) && return x >> (c % Int)
92✔
700
    (x >= 0 || c < 0) && return zero(x) >> 0
×
701
    oftype(x, -1)
×
702
end
703
>>(x::Integer, c::Int) = c >= 0 ? x >> unsigned(c) : x << unsigned(-c)
705✔
704

705
"""
706
    >>>(x, n)
707

708
Unsigned right bit shift operator, `x >>> n`. For `n >= 0`, the result is `x`
709
shifted right by `n` bits, filling with `0`s. For `n < 0`, this is equivalent
710
to `x << -n`.
711

712
For [`Unsigned`](@ref) integer types, this is equivalent to [`>>`](@ref). For
713
[`Signed`](@ref) integer types, this is equivalent to `signed(unsigned(x) >> n)`.
714

715
# Examples
716
```jldoctest
717
julia> Int8(-14) >>> 2
718
60
719

720
julia> bitstring(Int8(-14))
721
"11110010"
722

723
julia> bitstring(Int8(60))
724
"00111100"
725
```
726

727
[`BigInt`](@ref)s are treated as if having infinite size, so no filling is required and this
728
is equivalent to [`>>`](@ref).
729

730
See also [`>>`](@ref), [`<<`](@ref).
731
"""
732
function >>>(x::Integer, c::Integer)
72✔
733
    @inline
72✔
734
    typemin(Int) <= c <= typemax(Int) ? x >>> (c % Int) : zero(x) >>> 0
72✔
735
end
736
function >>>(x::Integer, c::Unsigned)
6,084✔
737
    @inline
6,084✔
738
    if c isa UInt
6,084✔
739
        throw(MethodError(>>>, (x, c)))
1✔
740
    end
741
    c <= typemax(UInt) ? x >>> (c % UInt) : zero(x) >>> 0
6,083✔
742
end
743
>>>(x::Integer, c::Int) = c >= 0 ? x >>> unsigned(c) : x << unsigned(-c)
26✔
744

745
# operator alias
746

747
"""
748
    rem(x, y)
749
    %(x, y)
750

751
Remainder from Euclidean division, returning a value of the same sign as `x`, and smaller in
752
magnitude than `y`. This value is always exact.
753

754
See also: [`div`](@ref), [`mod`](@ref), [`mod1`](@ref), [`divrem`](@ref).
755

756
# Examples
757
```jldoctest
758
julia> x = 15; y = 4;
759

760
julia> x % y
761
3
762

763
julia> x == div(x, y) * y + rem(x, y)
764
true
765

766
julia> rem.(-5:5, 3)'
767
1×11 adjoint(::Vector{Int64}) with eltype Int64:
768
 -2  -1  0  -2  -1  0  1  2  0  1  2
769
```
770
"""
771
rem
772
const % = rem
773

774
"""
775
    div(x, y)
776
    ÷(x, y)
777

778
The quotient from Euclidean (integer) division. Generally equivalent
779
to a mathematical operation x/y without a fractional part.
780

781
See also: [`cld`](@ref), [`fld`](@ref), [`rem`](@ref), [`divrem`](@ref).
782

783
# Examples
784
```jldoctest
785
julia> 9 ÷ 4
786
2
787

788
julia> -5 ÷ 3
789
-1
790

791
julia> 5.0 ÷ 2
792
2.0
793

794
julia> div.(-5:5, 3)'
795
1×11 adjoint(::Vector{Int64}) with eltype Int64:
796
 -1  -1  -1  0  0  0  0  0  1  1  1
797
```
798
"""
799
div
800
const ÷ = div
801

802
"""
803
    mod1(x, y)
804

805
Modulus after flooring division, returning a value `r` such that `mod(r, y) == mod(x, y)`
806
in the range ``(0, y]`` for positive `y` and in the range ``[y,0)`` for negative `y`.
807

808
With integer arguments and positive `y`, this is equal to `mod(x, 1:y)`, and hence natural
809
for 1-based indexing. By comparison, `mod(x, y) == mod(x, 0:y-1)` is natural for computations with
810
offsets or strides.
811

812
See also [`mod`](@ref), [`fld1`](@ref), [`fldmod1`](@ref).
813

814
# Examples
815
```jldoctest
816
julia> mod1(4, 2)
817
2
818

819
julia> mod1.(-5:5, 3)'
820
1×11 adjoint(::Vector{Int64}) with eltype Int64:
821
 1  2  3  1  2  3  1  2  3  1  2
822

823
julia> mod1.([-0.1, 0, 0.1, 1, 2, 2.9, 3, 3.1]', 3)
824
1×8 Matrix{Float64}:
825
 2.9  3.0  0.1  1.0  2.0  2.9  3.0  0.1
826
```
827
"""
828
mod1(x::T, y::T) where {T<:Real} = (m = mod(x, y); ifelse(m == 0, y, m))
177,910✔
829

830

831
"""
832
    fld1(x, y)
833

834
Flooring division, returning a value consistent with `mod1(x,y)`
835

836
See also [`mod1`](@ref), [`fldmod1`](@ref).
837

838
# Examples
839
```jldoctest
840
julia> x = 15; y = 4;
841

842
julia> fld1(x, y)
843
4
844

845
julia> x == fld(x, y) * y + mod(x, y)
846
true
847

848
julia> x == (fld1(x, y) - 1) * y + mod1(x, y)
849
true
850
```
851
"""
852
fld1(x::T, y::T) where {T<:Real} = (m = mod1(x, y); fld((x - m) + y, y))
3,110✔
853
function fld1(x::T, y::T) where T<:Integer
3,058✔
854
    d = div(x, y)
3,058✔
855
    return d + (!signbit(x ⊻ y) & (d * y != x))
3,054✔
856
end
857

858
"""
859
    fldmod1(x, y)
860

861
Return `(fld1(x,y), mod1(x,y))`.
862

863
See also [`fld1`](@ref), [`mod1`](@ref).
864
"""
865
fldmod1(x, y) = (fld1(x, y), mod1(x, y))
13✔
866

867

868
"""
869
    widen(x)
870

871
If `x` is a type, return a "larger" type, defined so that arithmetic operations
872
`+` and `-` are guaranteed not to overflow nor lose precision for any combination
873
of values that type `x` can hold.
874

875
For fixed-size integer types less than 128 bits, `widen` will return a type with
876
twice the number of bits.
877

878
If `x` is a value, it is converted to `widen(typeof(x))`.
879

880
# Examples
881
```jldoctest
882
julia> widen(Int32)
883
Int64
884

885
julia> widen(1.5f0)
886
1.5
887
```
888
"""
889
widen(x::T) where {T} = convert(widen(T), x)
29,646,382✔
890
widen(x::Type{T}) where {T} = throw(MethodError(widen, (T,)))
1✔
891
widen(x::Type{Union{}}, slurp...) = throw(MethodError(widen, (Union{},)))
×
892

893
# function pipelining
894

895
"""
896
    |>(x, f)
897

898
Infix operator which applies function `f` to the argument `x`.
899
This allows `f(g(x))` to be written `x |> g |> f`.
900
When used with anonymous functions, parentheses are typically required around
901
the definition to get the intended chain.
902

903
# Examples
904
```jldoctest
905
julia> 4 |> inv
906
0.25
907

908
julia> [2, 3, 5] |> sum |> inv
909
0.1
910

911
julia> [0 1; 2 3] .|> (x -> x^2) |> sum
912
14
913
```
914
"""
915
|>(x, f) = f(x)
2,820✔
916

917
_stable_typeof(x) = typeof(x)
208,258,260✔
918
_stable_typeof(::Type{T}) where {T} = @isdefined(T) ? Type{T} : DataType
14✔
919

920
"""
921
    f = Returns(value)
922

923
Create a callable `f` such that `f(args...; kw...) === value` holds.
924

925
# Examples
926

927
```jldoctest
928
julia> f = Returns(42);
929

930
julia> f(1)
931
42
932

933
julia> f("hello", x=32)
934
42
935

936
julia> f.value
937
42
938
```
939

940
!!! compat "Julia 1.7"
941
    `Returns` requires at least Julia 1.7.
942
"""
943
struct Returns{V} <: Function
944
    value::V
945
    Returns{V}(value) where {V} = new{V}(value)
1✔
946
    Returns(value) = new{_stable_typeof(value)}(value)
1,862✔
947
end
948

949
(obj::Returns)(@nospecialize(args...); @nospecialize(kw...)) = obj.value
41,074✔
950

951
# function composition
952

953
"""
954
    f ∘ g
955

956
Compose functions: i.e. `(f ∘ g)(args...; kwargs...)` means `f(g(args...; kwargs...))`. The `∘` symbol can be
957
entered in the Julia REPL (and most editors, appropriately configured) by typing `\\circ<tab>`.
958

959
Function composition also works in prefix form: `∘(f, g)` is the same as `f ∘ g`.
960
The prefix form supports composition of multiple functions: `∘(f, g, h) = f ∘ g ∘ h`
961
and splatting `∘(fs...)` for composing an iterable collection of functions.
962
The last argument to `∘` execute first.
963

964
!!! compat "Julia 1.4"
965
    Multiple function composition requires at least Julia 1.4.
966

967
!!! compat "Julia 1.5"
968
    Composition of one function ∘(f) requires at least Julia 1.5.
969

970
!!! compat "Julia 1.7"
971
    Using keyword arguments requires at least Julia 1.7.
972

973
# Examples
974
```jldoctest
975
julia> map(uppercase∘first, ["apple", "banana", "carrot"])
976
3-element Vector{Char}:
977
 'A': ASCII/Unicode U+0041 (category Lu: Letter, uppercase)
978
 'B': ASCII/Unicode U+0042 (category Lu: Letter, uppercase)
979
 'C': ASCII/Unicode U+0043 (category Lu: Letter, uppercase)
980

981
julia> (==(6)∘length).(["apple", "banana", "carrot"])
982
3-element BitVector:
983
 0
984
 1
985
 1
986

987
julia> fs = [
988
           x -> 2x
989
           x -> x-1
990
           x -> x/2
991
           x -> x+1
992
       ];
993

994
julia> ∘(fs...)(3)
995
2.0
996
```
997
See also [`ComposedFunction`](@ref), [`!f::Function`](@ref).
998
"""
999
function ∘ end
1000

1001
"""
1002
    ComposedFunction{Outer,Inner} <: Function
1003

1004
Represents the composition of two callable objects `outer::Outer` and `inner::Inner`. That is
1005
```julia
1006
ComposedFunction(outer, inner)(args...; kw...) === outer(inner(args...; kw...))
1007
```
1008
The preferred way to construct an instance of `ComposedFunction` is to use the composition operator [`∘`](@ref):
1009
```jldoctest
1010
julia> sin ∘ cos === ComposedFunction(sin, cos)
1011
true
1012

1013
julia> typeof(sin∘cos)
1014
ComposedFunction{typeof(sin), typeof(cos)}
1015
```
1016
The composed pieces are stored in the fields of `ComposedFunction` and can be retrieved as follows:
1017
```jldoctest
1018
julia> composition = sin ∘ cos
1019
sin ∘ cos
1020

1021
julia> composition.outer === sin
1022
true
1023

1024
julia> composition.inner === cos
1025
true
1026
```
1027
!!! compat "Julia 1.6"
1028
    ComposedFunction requires at least Julia 1.6. In earlier versions `∘` returns an anonymous function instead.
1029

1030
See also [`∘`](@ref).
1031
"""
1032
struct ComposedFunction{O,I} <: Function
1033
    outer::O
1034
    inner::I
1035
    ComposedFunction{O, I}(outer, inner) where {O, I} = new{O, I}(outer, inner)
×
1036
    ComposedFunction(outer, inner) = new{Core.Typeof(outer),Core.Typeof(inner)}(outer, inner)
239,496✔
1037
end
1038

1039
(c::ComposedFunction)(x...; kw...) = call_composed(unwrap_composed(c), x, kw)
6,836,114✔
1040
unwrap_composed(c::ComposedFunction) = (unwrap_composed(c.outer)..., unwrap_composed(c.inner)...)
794,256✔
1041
unwrap_composed(c) = (maybeconstructor(c),)
1,154,230✔
1042
call_composed(fs, x, kw) = (@inline; fs[1](call_composed(tail(fs), x, kw)))
3,459,454✔
1043
call_composed(fs::Tuple{Any}, x, kw) = fs[1](x...; kw...)
3,418,040✔
1044

1045
struct Constructor{F} <: Function end
24✔
1046
(::Constructor{F})(args...; kw...) where {F} = (@inline; F(args...; kw...))
48✔
1047
maybeconstructor(::Type{F}) where {F} = Constructor{F}()
24✔
1048
maybeconstructor(f) = f
1,154,206✔
1049

1050
∘(f) = f
2✔
1051
∘(f, g) = ComposedFunction(f, g)
239,496✔
1052
∘(f, g, h...) = ∘(f ∘ g, h...)
8✔
1053

1054
function show(io::IO, c::ComposedFunction)
10✔
1055
    c.outer isa ComposedFunction ? show(io, c.outer) : _showcomposed(io, c.outer)
10✔
1056
    print(io, " ∘ ")
10✔
1057
    _showcomposed(io, c.inner)
10✔
1058
end
1059

1060
#shows !f instead of (!) ∘ f when ! is the outermost function
1061
function show(io::IO, c::ComposedFunction{typeof(!)})
6✔
1062
    print(io, '!')
6✔
1063
    _showcomposed(io, c.inner)
6✔
1064
end
1065

1066
_showcomposed(io::IO, x) = show(io, x)
×
1067
#display operators like + and - inside parens
1068
_showcomposed(io::IO, f::Function) = isoperator(Symbol(f)) ? (print(io, '('); show(io, f); print(io, ')')) : show(io, f)
43✔
1069
#nesting for chained composition
1070
_showcomposed(io::IO, f::ComposedFunction) = (print(io, '('); show(io, f); print(io, ')'))
6✔
1071
#no nesting when ! is the outer function in a composition chain
1072
_showcomposed(io::IO, f::ComposedFunction{typeof(!)}) = show(io, f)
1✔
1073

1074
"""
1075
    !f::Function
1076

1077
Predicate function negation: when the argument of `!` is a function, it returns a composed function which computes the boolean negation of `f`.
1078

1079
See also [`∘`](@ref).
1080

1081
# Examples
1082
```jldoctest
1083
julia> str = "∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"
1084
"∀ ε > 0, ∃ δ > 0: |x-y| < δ ⇒ |f(x)-f(y)| < ε"
1085

1086
julia> filter(isletter, str)
1087
"εδxyδfxfyε"
1088

1089
julia> filter(!isletter, str)
1090
"∀  > 0, ∃  > 0: |-| <  ⇒ |()-()| < "
1091
```
1092

1093
!!! compat "Julia 1.9"
1094
    Starting with Julia 1.9, `!f` returns a [`ComposedFunction`](@ref) instead of an anonymous function.
1095
"""
1096
!(f::Function) = (!) ∘ f
29,989✔
1097
!(f::ComposedFunction{typeof(!)}) = f.inner #allows !!f === f
8✔
1098

1099
"""
1100
    Fix1(f, x)
1101

1102
A type representing a partially-applied version of the two-argument function
1103
`f`, with the first argument fixed to the value "x". In other words,
1104
`Fix1(f, x)` behaves similarly to `y->f(x, y)`.
1105

1106
See also [`Fix2`](@ref Base.Fix2).
1107
"""
1108
struct Fix1{F,T} <: Function
1109
    f::F
1110
    x::T
1111

1112
    Fix1(f::F, x) where {F} = new{F,_stable_typeof(x)}(f, x)
209,067,913✔
1113
    Fix1(f::Type{F}, x) where {F} = new{Type{F},_stable_typeof(x)}(f, x)
156,715✔
1114
end
1115

1116
(f::Fix1)(y) = f.f(f.x, y)
424,385,609✔
1117

1118
"""
1119
    Fix2(f, x)
1120

1121
A type representing a partially-applied version of the two-argument function
1122
`f`, with the second argument fixed to the value "x". In other words,
1123
`Fix2(f, x)` behaves similarly to `y->f(y, x)`.
1124
"""
1125
struct Fix2{F,T} <: Function
1126
    f::F
1127
    x::T
1128

1129
    Fix2(f::F, x) where {F} = new{F,_stable_typeof(x)}(f, x)
1,237,499✔
1130
    Fix2(f::Type{F}, x) where {F} = new{Type{F},_stable_typeof(x)}(f, x)
×
1131
end
1132

1133
(f::Fix2)(y) = f.f(y, f.x)
5,712,460✔
1134

1135
"""
1136
    isequal(x)
1137

1138
Create a function that compares its argument to `x` using [`isequal`](@ref), i.e.
1139
a function equivalent to `y -> isequal(y, x)`.
1140

1141
The returned function is of type `Base.Fix2{typeof(isequal)}`, which can be
1142
used to implement specialized methods.
1143
"""
1144
isequal(x) = Fix2(isequal, x)
512,157✔
1145

1146
"""
1147
    ==(x)
1148

1149
Create a function that compares its argument to `x` using [`==`](@ref), i.e.
1150
a function equivalent to `y -> y == x`.
1151

1152
The returned function is of type `Base.Fix2{typeof(==)}`, which can be
1153
used to implement specialized methods.
1154
"""
1155
==(x) = Fix2(==, x)
332✔
1156

1157
"""
1158
    !=(x)
1159

1160
Create a function that compares its argument to `x` using [`!=`](@ref), i.e.
1161
a function equivalent to `y -> y != x`.
1162
The returned function is of type `Base.Fix2{typeof(!=)}`, which can be
1163
used to implement specialized methods.
1164

1165
!!! compat "Julia 1.2"
1166
    This functionality requires at least Julia 1.2.
1167
"""
1168
!=(x) = Fix2(!=, x)
699,259✔
1169

1170
"""
1171
    >=(x)
1172

1173
Create a function that compares its argument to `x` using [`>=`](@ref), i.e.
1174
a function equivalent to `y -> y >= x`.
1175
The returned function is of type `Base.Fix2{typeof(>=)}`, which can be
1176
used to implement specialized methods.
1177

1178
!!! compat "Julia 1.2"
1179
    This functionality requires at least Julia 1.2.
1180
"""
1181
>=(x) = Fix2(>=, x)
457✔
1182

1183
"""
1184
    <=(x)
1185

1186
Create a function that compares its argument to `x` using [`<=`](@ref), i.e.
1187
a function equivalent to `y -> y <= x`.
1188
The returned function is of type `Base.Fix2{typeof(<=)}`, which can be
1189
used to implement specialized methods.
1190

1191
!!! compat "Julia 1.2"
1192
    This functionality requires at least Julia 1.2.
1193
"""
1194
<=(x) = Fix2(<=, x)
47✔
1195

1196
"""
1197
    >(x)
1198

1199
Create a function that compares its argument to `x` using [`>`](@ref), i.e.
1200
a function equivalent to `y -> y > x`.
1201
The returned function is of type `Base.Fix2{typeof(>)}`, which can be
1202
used to implement specialized methods.
1203

1204
!!! compat "Julia 1.2"
1205
    This functionality requires at least Julia 1.2.
1206
"""
1207
>(x) = Fix2(>, x)
387✔
1208

1209
"""
1210
    <(x)
1211

1212
Create a function that compares its argument to `x` using [`<`](@ref), i.e.
1213
a function equivalent to `y -> y < x`.
1214
The returned function is of type `Base.Fix2{typeof(<)}`, which can be
1215
used to implement specialized methods.
1216

1217
!!! compat "Julia 1.2"
1218
    This functionality requires at least Julia 1.2.
1219
"""
1220
<(x) = Fix2(<, x)
194✔
1221

1222
"""
1223
    splat(f)
1224

1225
Equivalent to
1226
```julia
1227
    my_splat(f) = args->f(args...)
1228
```
1229
i.e. given a function returns a new function that takes one argument and splats
1230
it into the original function. This is useful as an adaptor to pass a
1231
multi-argument function in a context that expects a single argument, but passes
1232
a tuple as that single argument.
1233

1234
# Example usage:
1235
```jldoctest
1236
julia> map(splat(+), zip(1:3,4:6))
1237
3-element Vector{Int64}:
1238
 5
1239
 7
1240
 9
1241

1242
julia> my_add = splat(+)
1243
splat(+)
1244

1245
julia> my_add((1,2,3))
1246
6
1247
```
1248
"""
1249
splat(f) = Splat(f)
272✔
1250

1251
"""
1252
    Base.Splat{F} <: Function
1253

1254
Represents a splatted function. That is
1255
```julia
1256
Base.Splat(f)(args) === f(args...)
1257
```
1258
The preferred way to construct an instance of `Base.Splat` is to use the [`splat`](@ref) function.
1259

1260
!!! compat "Julia 1.9"
1261
    Splat requires at least Julia 1.9. In earlier versions `splat` returns an anonymous function instead.
1262

1263
See also [`splat`](@ref).
1264
"""
1265
struct Splat{F} <: Function
1266
    f::F
1267
    Splat(f) = new{Core.Typeof(f)}(f)
272✔
1268
end
1269
(s::Splat)(args) = s.f(args...)
592,275✔
1270
print(io::IO, s::Splat) = print(io, "splat(", s.f, ')')
×
1271
show(io::IO, s::Splat) = print(io, s)
×
1272

1273
## in and related operators
1274

1275
"""
1276
    in(collection)
1277
    ∈(collection)
1278

1279
Create a function that checks whether its argument is [`in`](@ref) `collection`, i.e.
1280
a function equivalent to `y -> y in collection`. See also [`insorted`](@ref) for use
1281
with sorted collections.
1282

1283
The returned function is of type `Base.Fix2{typeof(in)}`, which can be
1284
used to implement specialized methods.
1285
"""
1286
in(x) = Fix2(in, x)
24,035✔
1287

1288
function in(x, itr)
2,898,188✔
1289
    anymissing = false
2,366,799✔
1290
    for y in itr
126,687,436✔
1291
        v = (y == x)
716,727,179✔
1292
        if ismissing(v)
14,271,579✔
1293
            anymissing = true
3✔
1294
        elseif v
716,675,163✔
1295
            return true
43,883,440✔
1296
        end
1297
    end
761,696,418✔
1298
    return anymissing ? missing : false
81,549,086✔
1299
end
1300

1301
const ∈ = in
1302
∉(x, itr) = !∈(x, itr)
951,775✔
1303
∉(itr) = Fix2(∉, itr)
×
1304

1305
"""
1306
    ∋(collection, item) -> Bool
1307

1308
Like [`in`](@ref), but with arguments in reverse order.
1309
Avoid adding methods to this function; define `in` instead.
1310
"""
1311
∋(itr, x) = in(x, itr)
21✔
1312

1313
"""
1314
    ∋(item)
1315

1316
Create a function that checks whether its argument contains the given `item`, i.e.
1317
a function equivalent to `y -> item in y`.
1318

1319
!!! compat "Julia 1.6"
1320
    This method requires Julia 1.6 or later.
1321
"""
1322
∋(x) = Fix2(∋, x)
3✔
1323

1324
∌(itr, x) = !∋(itr, x)
2✔
1325
∌(x) = Fix2(∌, x)
1✔
1326

1327
"""
1328
    in(item, collection) -> Bool
1329
    ∈(item, collection) -> Bool
1330

1331
Determine whether an item is in the given collection, in the sense that it is
1332
[`==`](@ref) to one of the values generated by iterating over the collection.
1333
Return a `Bool` value, except if `item` is [`missing`](@ref) or `collection`
1334
contains `missing` but not `item`, in which case `missing` is returned
1335
([three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic),
1336
matching the behavior of [`any`](@ref) and [`==`](@ref)).
1337

1338
Some collections follow a slightly different definition. For example,
1339
[`Set`](@ref)s check whether the item [`isequal`](@ref) to one of the elements;
1340
[`Dict`](@ref)s look for `key=>value` pairs, and the `key` is compared using
1341
[`isequal`](@ref).
1342

1343
To test for the presence of a key in a dictionary, use [`haskey`](@ref)
1344
or `k in keys(dict)`. For the collections mentioned above,
1345
the result is always a `Bool`.
1346

1347
When broadcasting with `in.(items, collection)` or `items .∈ collection`, both
1348
`item` and `collection` are broadcasted over, which is often not what is intended.
1349
For example, if both arguments are vectors (and the dimensions match), the result is
1350
a vector indicating whether each value in collection `items` is `in` the value at the
1351
corresponding position in `collection`. To get a vector indicating whether each value
1352
in `items` is in `collection`, wrap `collection` in a tuple or a `Ref` like this:
1353
`in.(items, Ref(collection))` or `items .∈ Ref(collection)`.
1354

1355
See also: [`∉`](@ref).
1356

1357
# Examples
1358
```jldoctest
1359
julia> a = 1:3:20
1360
1:3:19
1361

1362
julia> 4 in a
1363
true
1364

1365
julia> 5 in a
1366
false
1367

1368
julia> missing in [1, 2]
1369
missing
1370

1371
julia> 1 in [2, missing]
1372
missing
1373

1374
julia> 1 in [1, missing]
1375
true
1376

1377
julia> missing in Set([1, 2])
1378
false
1379

1380
julia> (1=>missing) in Dict(1=>10, 2=>20)
1381
missing
1382

1383
julia> [1, 2] .∈ [2, 3]
1384
2-element BitVector:
1385
 0
1386
 0
1387

1388
julia> [1, 2] .∈ ([2, 3],)
1389
2-element BitVector:
1390
 0
1391
 1
1392
```
1393

1394
See also: [`insorted`](@ref), [`contains`](@ref), [`occursin`](@ref), [`issubset`](@ref).
1395
"""
1396
in
1397

1398
"""
1399
    ∉(item, collection) -> Bool
1400
    ∌(collection, item) -> Bool
1401

1402
Negation of `∈` and `∋`, i.e. checks that `item` is not in `collection`.
1403

1404
When broadcasting with `items .∉ collection`, both `item` and `collection` are
1405
broadcasted over, which is often not what is intended. For example, if both arguments
1406
are vectors (and the dimensions match), the result is a vector indicating whether
1407
each value in collection `items` is not in the value at the corresponding position
1408
in `collection`. To get a vector indicating whether each value in `items` is not in
1409
`collection`, wrap `collection` in a tuple or a `Ref` like this:
1410
`items .∉ Ref(collection)`.
1411

1412
# Examples
1413
```jldoctest
1414
julia> 1 ∉ 2:4
1415
true
1416

1417
julia> 1 ∉ 1:3
1418
false
1419

1420
julia> [1, 2] .∉ [2, 3]
1421
2-element BitVector:
1422
 1
1423
 1
1424

1425
julia> [1, 2] .∉ ([2, 3],)
1426
2-element BitVector:
1427
 1
1428
 0
1429
```
1430
"""
1431
∉, ∌
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc