diff --git a/base/complex.jl b/base/complex.jl index cd30afbd86e0b..4969eea3d24b2 100644 --- a/base/complex.jl +++ b/base/complex.jl @@ -97,6 +97,7 @@ Return the type that represents the real part of a value of type `T`. e.g: for `T == Complex{R}`, returns `R`. Equivalent to `typeof(real(zero(T)))`. +# Examples ```jldoctest julia> real(Complex{Int}) Int64 diff --git a/base/dict.jl b/base/dict.jl index ea793e0c35479..1cf50f84d9fd0 100644 --- a/base/dict.jl +++ b/base/dict.jl @@ -68,6 +68,7 @@ Keys are compared with [`isequal`](@ref) and hashed with [`hash`](@ref). Given a single iterable argument, constructs a [`Dict`](@ref) whose key-value pairs are taken from 2-tuples `(key,value)` generated by the argument. +# Examples ```jldoctest julia> Dict([("A", 1), ("B", 2)]) Dict{String,Int64} with 2 entries: @@ -256,6 +257,7 @@ end Remove all elements from a `collection`. +# Examples ```jldoctest julia> A = Dict("a" => 1, "b" => 2) Dict{String,Int64} with 2 entries: @@ -534,8 +536,9 @@ end """ haskey(collection, key) -> Bool -Determine whether a collection has a mapping for a given key. +Determine whether a collection has a mapping for a given `key`. +# Examples ```jldoctest julia> D = Dict('a'=>2, 'b'=>3) Dict{Char,Int64} with 2 entries: @@ -557,6 +560,7 @@ in(key, v::KeySet{<:Any, <:Dict}) = (ht_keyindex(v.dict, key) >= 0) Return the key matching argument `key` if one exists in `collection`, otherwise return `default`. +# Examples ```jldoctest julia> D = Dict('a'=>2, 'b'=>3) Dict{Char,Int64} with 2 entries: diff --git a/base/event.jl b/base/event.jl index 0a3122573807b..c8fda346c5b87 100644 --- a/base/event.jl +++ b/base/event.jl @@ -104,6 +104,7 @@ If a second argument `val` is provided, it will be passed to the task (via the r [`yieldto`](@ref)) when it runs again. If `error` is `true`, the value is raised as an exception in the woken task. +# Examples ```jldoctest julia> a5() = sum(i for i in 1:1000); diff --git a/base/float.jl b/base/float.jl index 990c2700a1278..43c76a6173bd5 100644 --- a/base/float.jl +++ b/base/float.jl @@ -274,6 +274,7 @@ float(x) = AbstractFloat(x) Return an appropriate type to represent a value of type `T` as a floating point value. Equivalent to `typeof(float(zero(T)))`. +# Examples ```jldoctest julia> float(Complex{Int}) Complex{Float64} @@ -531,6 +532,7 @@ isnan(x::Real) = false Test whether a number is finite. +# Examples ```jldoctest julia> isfinite(5) true @@ -765,6 +767,7 @@ default). This is defined as the gap between 1 and the next largest value repres `typeof(one(T))`, and is equivalent to `eps(one(T))`. (Since `eps(T)` is a bound on the *relative error* of `T`, it is a "dimensionless" quantity like [`one`](@ref).) +# Examples ```jldoctest julia> eps() 2.220446049250313e-16 @@ -802,6 +805,7 @@ is the nearest floating point number to ``y``, then |y-x| \\leq \\operatorname{eps}(x)/2. ``` +# Examples ```jldoctest julia> eps(1.0) 2.220446049250313e-16 diff --git a/base/gmp.jl b/base/gmp.jl index cd53081e6024b..be9b729cd8f4c 100644 --- a/base/gmp.jl +++ b/base/gmp.jl @@ -70,6 +70,7 @@ results are promoted to a [`BigInt`](@ref). Instances can be constructed from strings via [`parse`](@ref), or using the `big` string literal. +# Examples ```jldoctest julia> parse(BigInt, "42") 42 diff --git a/base/indices.jl b/base/indices.jl index 15953df62bdbe..f4d0cec8fb5e0 100644 --- a/base/indices.jl +++ b/base/indices.jl @@ -80,6 +80,7 @@ end Check two array shapes for compatibility, allowing trailing singleton dimensions, and return whichever shape has more dimensions. +# Examples ```jldoctest julia> a = fill(1, (3,4,1,1,1)); diff --git a/base/int.jl b/base/int.jl index bb35129a82148..94da5080e6ac8 100644 --- a/base/int.jl +++ b/base/int.jl @@ -63,6 +63,7 @@ inv(x::Integer) = float(one(x)) / float(x) Return `true` if `x` is odd (that is, not divisible by 2), and `false` otherwise. +# Examples ```jldoctest julia> isodd(9) true @@ -78,6 +79,7 @@ isodd(n::Integer) = rem(n, 2) != 0 Return `true` is `x` is even (that is, divisible by 2), and `false` otherwise. +# Examples ```jldoctest julia> iseven(9) false @@ -116,6 +118,7 @@ when `abs` is applied to the minimum representable value of a signed integer. That is, when `x == typemin(typeof(x))`, `abs(x) == x < 0`, not `-x` as might be expected. +# Examples ```jldoctest julia> abs(-3) 3 @@ -346,6 +349,7 @@ bswap(x::Union{Int16, UInt16, Int32, UInt32, Int64, UInt64, Int128, UInt128}) = Number of ones in the binary representation of `x`. +# Examples ```jldoctest julia> count_ones(7) 3 @@ -358,6 +362,7 @@ count_ones(x::BitInteger) = Int(ctpop_int(x)) Number of zeros leading the binary representation of `x`. +# Examples ```jldoctest julia> leading_zeros(Int32(1)) 31 @@ -370,6 +375,7 @@ leading_zeros(x::BitInteger) = Int(ctlz_int(x)) Number of zeros trailing the binary representation of `x`. +# Examples ```jldoctest julia> trailing_zeros(2) 1 @@ -382,6 +388,7 @@ trailing_zeros(x::BitInteger) = Int(cttz_int(x)) Number of zeros in the binary representation of `x`. +# Examples ```jldoctest julia> count_zeros(Int32(2 ^ 16 - 1)) 16 @@ -394,6 +401,7 @@ count_zeros(x::Integer) = count_ones(~x) Number of ones leading the binary representation of `x`. +# Examples ```jldoctest julia> leading_ones(UInt32(2 ^ 32 - 2)) 31 @@ -406,6 +414,7 @@ leading_ones(x::Integer) = leading_zeros(~x) Number of ones trailing the binary representation of `x`. +# Examples ```jldoctest julia> trailing_ones(3) 2 @@ -476,6 +485,7 @@ if nameof(@__MODULE__) === :Base If `T` can represent any integer (e.g. `T == BigInt`), then this operation corresponds to a conversion to `T`. + # Examples ```jldoctest julia> 129 % Int8 -127 @@ -616,6 +626,15 @@ function typemin end typemax(T) The highest value representable by the given (real) numeric `DataType`. + +# Examples +```jldoctest +julia> typemax(Int8) +127 + +julia> typemax(UInt32) +0xffffffff +``` """ function typemax end diff --git a/base/iterators.jl b/base/iterators.jl index db5873ead9d2e..1ce2d65264d01 100644 --- a/base/iterators.jl +++ b/base/iterators.jl @@ -473,7 +473,7 @@ rest(itr) = itr Returns the first element and an iterator over the remaining elements. -# Example +# Examples ```jldoctest julia> (a, rest) = Iterators.peel("abc"); @@ -992,7 +992,7 @@ iteration can be resumed from the same spot by continuing to iterate over the same iterator object (in contrast, an immutable iterator would restart from the beginning). -# Example: +# Examples ```jldoctest julia> a = Iterators.Stateful("abcdef"); diff --git a/base/math.jl b/base/math.jl index 315229eb1437b..551d370b215d3 100644 --- a/base/math.jl +++ b/base/math.jl @@ -55,6 +55,7 @@ end Return `x` if `lo <= x <= hi`. If `x > hi`, return `hi`. If `x < lo`, return `lo`. Arguments are promoted to a common type. +# Examples ```jldoctest julia> clamp.([pi, 1.0, big(10.)], 2., 9.) 3-element Array{BigFloat,1}: @@ -110,6 +111,7 @@ that is, the coefficients are given in ascending order by power of `z`. This ma to efficient inline code that uses either Horner's method or, for complex `z`, a more efficient Goertzel-like algorithm. +# Examples ```jldoctest julia> @evalpoly(3, 1, 0, 1) 10 @@ -151,6 +153,7 @@ end Convert `x` from radians to degrees. +# Examples ```jldoctest julia> rad2deg(pi) 180.0 @@ -163,6 +166,7 @@ rad2deg(z::AbstractFloat) = z * (180 / oftype(z, pi)) Convert `x` from degrees to radians. +# Examples ```jldoctest julia> deg2rad(90) 1.5707963267948966 @@ -182,12 +186,27 @@ log(b::T, x::T) where {T<:Number} = log(x)/log(b) Compute the base `b` logarithm of `x`. Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments. -```jldoctest +# Examples +```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*" julia> log(4,8) 1.5 julia> log(4,2) 0.5 + +julia> log(-2, 3) +ERROR: DomainError with log: +-2.0 will only return a complex result if called with a complex argument. Try -2.0(Complex(x)). +Stacktrace: + [1] throw_complex_domainerror(::Float64, ::Symbol) at ./math.jl:31 +[...] + +julia> log(2, -3) +ERROR: DomainError with log: +-3.0 will only return a complex result if called with a complex argument. Try -3.0(Complex(x)). +Stacktrace: + [1] throw_complex_domainerror(::Float64, ::Symbol) at ./math.jl:31 +[...] ``` !!! note @@ -267,9 +286,13 @@ Return the cube root of `x`, i.e. ``x^{1/3}``. Negative values are accepted The prefix operator `∛` is equivalent to `cbrt`. +# Examples ```jldoctest julia> cbrt(big(27)) 3.0 + +julia> cbrt(big(-27)) +-3.0 ``` """ cbrt(x::AbstractFloat) = x < 0 ? -(-x)^(1//3) : x^(1//3) @@ -389,12 +412,19 @@ Compute the logarithm of `x` to base 2. Throws [`DomainError`](@ref) for negativ [`Real`](@ref) arguments. # Examples -```jldoctest +```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*" julia> log2(4) 2.0 julia> log2(10) 3.321928094887362 + +julia> log2(-2) +ERROR: DomainError with -2.0: +NaN result for non-NaN input. +Stacktrace: + [1] nan_dom_err at ./math.jl:325 [inlined] +[...] ``` """ log2(x) @@ -406,12 +436,19 @@ Compute the logarithm of `x` to base 10. Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments. # Examples -```jldoctest +```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*" julia> log10(100) 2.0 julia> log10(2) 0.3010299956639812 + +julia> log10(-2) +ERROR: DomainError with -2.0: +NaN result for non-NaN input. +Stacktrace: + [1] nan_dom_err at ./math.jl:325 [inlined] +[...] ``` """ log10(x) @@ -423,12 +460,19 @@ Accurate natural logarithm of `1+x`. Throws [`DomainError`](@ref) for [`Real`](@ arguments less than -1. # Examples -```jldoctest +```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*" julia> log1p(-0.5) -0.6931471805599453 julia> log1p(0) 0.0 + +julia> log1p(-2) +ERROR: DomainError with log1p: +-2.0 will only return a complex result if called with a complex argument. Try -2.0(Complex(x)). +Stacktrace: + [1] throw_complex_domainerror(::Float64, ::Symbol) at ./math.jl:31 +[...] ``` """ log1p(x) @@ -450,6 +494,22 @@ end Return ``\\sqrt{x}``. Throws [`DomainError`](@ref) for negative [`Real`](@ref) arguments. Use complex negative arguments instead. The prefix operator `√` is equivalent to `sqrt`. + +# Examples +```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*" +julia> sqrt(big(81)) +9.0 + +julia> sqrt(big(-81)) +ERROR: DomainError with -8.1e+01: +NaN result for non-NaN input. +Stacktrace: + [1] sqrt(::BigFloat) at ./mpfr.jl:501 +[...] + +julia> sqrt(big(complex(-81))) +0.0 + 9.0im +``` """ sqrt(x::Real) = sqrt(float(x)) @@ -459,7 +519,7 @@ sqrt(x::Real) = sqrt(float(x)) Compute the hypotenuse ``\\sqrt{x^2+y^2}`` avoiding overflow and underflow. # Examples -```jldoctest +```jldoctest; filter = r"Stacktrace:(\\n \\[[0-9]+\\].*)*" julia> a = 10^10; julia> hypot(a, a) @@ -692,13 +752,16 @@ rem(x::Float16, y::Float16, r::RoundingMode{:Nearest}) = Float16(rem(Float32(x), """ modf(x) -Return a tuple (fpart,ipart) of the fractional and integral parts of a number. Both parts +Return a tuple `(fpart, ipart)` of the fractional and integral parts of a number. Both parts have the same sign as the argument. # Examples ```jldoctest julia> modf(3.5) (0.5, 3.0) + +julia> modf(-3.5) +(-0.5, -3.0) ``` """ modf(x) = rem(x,one(x)), trunc(x) diff --git a/base/mathconstants.jl b/base/mathconstants.jl index 80a960dbe2786..a3d1be99becbb 100644 --- a/base/mathconstants.jl +++ b/base/mathconstants.jl @@ -23,6 +23,7 @@ Base.@irrational catalan 0.91596559417721901505 catalan The constant pi. +# Examples ```jldoctest julia> pi π = 3.1415926535897... @@ -36,6 +37,7 @@ julia> pi The constant ℯ. +# Examples ```jldoctest julia> ℯ ℯ = 2.7182818284590... @@ -49,6 +51,7 @@ julia> ℯ Euler's constant. +# Examples ```jldoctest julia> Base.MathConstants.eulergamma γ = 0.5772156649015... @@ -62,6 +65,7 @@ julia> Base.MathConstants.eulergamma The golden ratio. +# Examples ```jldoctest julia> Base.MathConstants.golden φ = 1.6180339887498... @@ -74,6 +78,7 @@ julia> Base.MathConstants.golden Catalan's constant. +# Examples ```jldoctest julia> Base.MathConstants.catalan catalan = 0.9159655941772... diff --git a/base/number.jl b/base/number.jl index b1b20d4102b4c..89cec5169e870 100644 --- a/base/number.jl +++ b/base/number.jl @@ -11,6 +11,7 @@ convert(::Type{T}, x::Number) where {T<:Number} = T(x) Test whether `x` is numerically equal to some integer. +# Examples ```jldoctest julia> isinteger(4.0) true @@ -24,9 +25,16 @@ isinteger(x::Integer) = true Return `true` if `x == zero(x)`; if `x` is an array, this checks whether all of the elements of `x` are zero. +# Examples ```jldoctest julia> iszero(0.0) true + +julia> iszero([1, 9, 0]) +false + +julia> iszero([false, 0, 0]) +true ``` """ iszero(x) = x == zero(x) # fallback method @@ -37,9 +45,16 @@ iszero(x) = x == zero(x) # fallback method Return `true` if `x == one(x)`; if `x` is an array, this checks whether `x` is an identity matrix. +# Examples ```jldoctest julia> isone(1.0) true + +julia> isone([1 0; 0 2]) +false + +julia> isone([1 0; 0 true]) +true ``` """ isone(x) = x == one(x) # fallback method @@ -78,6 +93,7 @@ copy(x::Number) = x # some code treats numbers as collection-like The quotient and remainder from Euclidean division. Equivalent to `(div(x,y), rem(x,y))` or `(x÷y, x%y)`. +# Examples ```jldoctest julia> divrem(3,7) (0, 3) @@ -132,6 +148,7 @@ abs(x::Real) = ifelse(signbit(x), -x, x) Squared absolute value of `x`. +# Examples ```jldoctest julia> abs2(-3) 9 @@ -144,6 +161,7 @@ abs2(x::Real) = x*x Return `x` with its sign flipped if `y` is negative. For example `abs(x) = flipsign(x,x)`. +# Examples ```jldoctest julia> flipsign(5, 3) 5 @@ -207,6 +225,7 @@ inv(x::Number) = one(x)/x Multiply `x` and `y`, giving the result as a larger type. +# Examples ```jldoctest julia> widemul(Float32(3.), 4.) 1.2e+01 @@ -225,6 +244,7 @@ map(f, x::Number, ys::Number...) = f(x, ys...) Get the additive identity element for the type of `x` (`x` can also specify the type itself). +# Examples ```jldoctest julia> zero(1) 0 @@ -260,6 +280,8 @@ should return an identity value of the same precision If you want a quantity that is of the same type as `x`, or of type `T`, even if `x` is dimensionful, use [`oneunit`](@ref) instead. + +# Examples ```jldoctest julia> one(3.7) 1.0 @@ -285,6 +307,7 @@ Returns `T(one(x))`, where `T` is either the type of the argument or dimensionful quantities: `one` is dimensionless (a multiplicative identity) while `oneunit` is dimensionful (of the same type as `x`, or of type `T`). +# Examples ```jldoctest julia> oneunit(3.7) 1.0 @@ -306,6 +329,7 @@ integer (promoted to at least 64 bits). Note that this may overflow if `n` is no but you can use `factorial(big(n))` to compute the result exactly in arbitrary precision. If `n` is not an `Integer`, `factorial(n)` is equivalent to [`gamma(n+1)`](@ref). +# Examples ```jldoctest julia> factorial(6) 720 @@ -330,6 +354,7 @@ factorial(x::Number) = gamma(x + 1) # fallback for x not Integer Compute the type that represents the numeric type `T` with arbitrary precision. Equivalent to `typeof(big(zero(T)))`. +# Examples ```jldoctest julia> big(Rational) Rational{BigInt} diff --git a/base/operators.jl b/base/operators.jl index baa9126af9635..4b6b0e96b9d10 100644 --- a/base/operators.jl +++ b/base/operators.jl @@ -8,6 +8,7 @@ Subtype operator: returns `true` if and only if all values of type `T1` are also of type `T2`. +# Examples ```jldoctest julia> Float64 <: AbstractFloat true @@ -33,6 +34,7 @@ const (>:)(@nospecialize(a), @nospecialize(b)) = (b <: a) Return the supertype of DataType `T`. +# Examples ```jldoctest julia> supertype(Int32) Signed @@ -242,6 +244,7 @@ New numeric types with a canonical partial order should implement this function two arguments of the new type. Types with a canonical total order should implement [`isless`](@ref) instead. (x < y) | (x == y) + # Examples ```jldoctest julia> 'a' < 'b' diff --git a/base/parse.jl b/base/parse.jl index b4af7fa87b630..e046a35c86714 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -14,6 +14,7 @@ of the form `"R±Iim"` as a `Complex(R,I)` of the requested type; `"i"` or `"j"` used instead of `"im"`, and `"R"` or `"Iim"` are also permitted. If the string does not contain a valid number, an error is raised. +# Examples ```jldoctest julia> parse(Int, "1234") 1234 diff --git a/base/path.jl b/base/path.jl index bfef1dfed5ef9..6a4a2dae3e99f 100644 --- a/base/path.jl +++ b/base/path.jl @@ -155,7 +155,7 @@ See also: [`basename`](@ref) Get the file name part of a path. # Examples - ```jldoctest +```jldoctest julia> basename("/home/myuser/example.jl") "example.jl" ``` diff --git a/base/range.jl b/base/range.jl index cf45e158940e4..dbf0f407e61cf 100644 --- a/base/range.jl +++ b/base/range.jl @@ -359,6 +359,8 @@ isempty(r::LinRange) = length(r) == 0 step(r) Get the step size of an `AbstractRange` object. + +# Examples ```jldoctest julia> step(1:10) 1 diff --git a/base/rational.jl b/base/rational.jl index d130ccab24a5e..6a05b18b7eb3b 100644 --- a/base/rational.jl +++ b/base/rational.jl @@ -29,6 +29,7 @@ end Divide two integers or rational numbers, giving a [`Rational`](@ref) result. +# Examples ```jldoctest julia> 3 // 5 3//5 @@ -112,6 +113,7 @@ widen(::Type{Rational{T}}) where {T} = Rational{widen(T)} Approximate floating point number `x` as a [`Rational`](@ref) number with components of the given integer type. The result will differ from `x` by no more than `tol`. +# Examples ```jldoctest julia> rationalize(5.6) 28//5 @@ -190,6 +192,7 @@ rationalize(x::AbstractFloat; kvs...) = rationalize(Int, x; kvs...) Numerator of the rational representation of `x`. +# Examples ```jldoctest julia> numerator(2//3) 2 @@ -206,6 +209,7 @@ numerator(x::Rational) = x.num Denominator of the rational representation of `x`. +# Examples ```jldoctest julia> denominator(2//3) 3 diff --git a/base/reduce.jl b/base/reduce.jl index e30e11b85e18e..989e34c5d95b0 100644 --- a/base/reduce.jl +++ b/base/reduce.jl @@ -80,6 +80,7 @@ end Like [`reduce`](@ref), but with guaranteed left associativity. `v0` will be used exactly once. +# Examples ```jldoctest julia> foldl(=>, 0, 1:4) (((0=>1)=>2)=>3) => 4 @@ -93,6 +94,7 @@ foldl(op, v0, itr) = mapfoldl(identity, op, v0, itr) Like `foldl(op, v0, itr)`, but using the first element of `itr` as `v0`. In general, this cannot be used with empty collections (see [`reduce(op, itr)`](@ref)). +# Examples ```jldoctest julia> foldl(=>, 1:4) ((1=>2)=>3) => 4 @@ -148,6 +150,7 @@ end Like [`reduce`](@ref), but with guaranteed right associativity. `v0` will be used exactly once. +# Examples ```jldoctest julia> foldr(=>, 0, 1:4) 1 => (2=>(3=>(4=>0))) @@ -161,6 +164,7 @@ foldr(op, v0, itr) = mapfoldr(identity, op, v0, itr) Like `foldr(op, v0, itr)`, but using the last element of `itr` as `v0`. In general, this cannot be used with empty collections (see [`reduce(op, itr)`](@ref)). +# Examples ```jldoctest julia> foldr(=>, 1:4) 1 => (2=>(3=>4)) @@ -222,6 +226,7 @@ collections. It is unspecified whether `v0` is used for non-empty collections. map(f, itr))`, but will in general execute faster since no intermediate collection needs to be created. See documentation for [`reduce`](@ref) and [`map`](@ref). +# Examples ```jldoctest julia> mapreduce(x->x^2, +, [1:3;]) # == 1 + 4 + 9 14 @@ -390,6 +395,7 @@ Like `reduce(op, v0, itr)`. This cannot be used with empty collections, except f special cases (e.g. when `op` is one of `+`, `*`, `max`, `min`, `&`, `|`) when Julia can determine the neutral element of `op`. +# Examples ```jldoctest julia> reduce(*, [2; 3; 4]) 24 @@ -411,6 +417,7 @@ The return type is `Int` for signed integers of less than system word size, and `UInt` for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted. +# Examples ```jldoctest julia> sum(abs2, [2; 3; 4]) 29 @@ -442,6 +449,7 @@ The return type is `Int` for signed integers of less than system word size, and `UInt` for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted. +# Examples ```jldoctest julia> sum(1:20) 210 @@ -460,6 +468,7 @@ The return type is `Int` for signed integers of less than system word size, and `UInt` for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted. +# Examples ```jldoctest julia> prod(abs2, [2; 3; 4]) 576 @@ -476,6 +485,7 @@ The return type is `Int` for signed integers of less than system word size, and `UInt` for unsigned integers of less than system word size. For all other arguments, a common return type is found to which all arguments are promoted. +# Examples ```jldoctest julia> prod(1:20) 2432902008176640000 @@ -507,6 +517,7 @@ minimum(f::Callable, a) = mapreduce(f, min, a) Returns the largest element in a collection. +# Examples ```jldoctest julia> maximum(-20.5:10) 9.5 @@ -522,6 +533,7 @@ maximum(a) = mapreduce(identity, max, a) Returns the smallest element in a collection. +# Examples ```jldoctest julia> minimum(-20.5:10) -20.5 @@ -542,6 +554,7 @@ extrema(x::Real) = (x, x) Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple. +# Examples ```jldoctest julia> extrema(2:10) (2, 10) @@ -577,6 +590,7 @@ If the input contains [`missing`](@ref) values, return `missing` if all non-miss values are `false` (or equivalently, if the input contains no `true` value), following [three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic). +# Examples ```jldoctest julia> a = [true,false,false,true] 4-element Array{Bool,1}: @@ -611,6 +625,7 @@ If the input contains [`missing`](@ref) values, return `missing` if all non-miss values are `true` (or equivalently, if the input contains no `false` value), following [three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic). +# Examples ```jldoctest julia> a = [true,false,false,true] 4-element Array{Bool,1}: @@ -647,6 +662,7 @@ If the input contains [`missing`](@ref) values, return `missing` if all non-miss values are `false` (or equivalently, if the input contains no `true` value), following [three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic). +# Examples ```jldoctest julia> any(i->(4<=i<=6), [3,5,7]) true @@ -694,6 +710,7 @@ If the input contains [`missing`](@ref) values, return `missing` if all non-miss values are `true` (or equivalently, if the input contains no `false` value), following [three-valued logic](https://en.wikipedia.org/wiki/Three-valued_logic). +# Examples ```jldoctest julia> all(i->(4<=i<=6), [4,5,6]) true @@ -759,6 +776,7 @@ Some collections follow a slightly different definition. For example, use [`haskey`](@ref) or `k in keys(dict)`. For these collections, the result is always a `Bool` and never `missing`. +# Examples ```jldoctest julia> a = 1:3:20 1:3:19 @@ -810,6 +828,7 @@ Count the number of elements in `itr` for which predicate `p` returns `true`. If `p` is omitted, counts the number of `true` elements in `itr` (which should be a collection of boolean values). +# Examples ```jldoctest julia> count(i->(4<=i<=6), [2,3,4,5,6]) 3 diff --git a/base/reducedim.jl b/base/reducedim.jl index 19acea13f8292..9c7bd8221e0bc 100644 --- a/base/reducedim.jl +++ b/base/reducedim.jl @@ -431,6 +431,7 @@ Compute the maximum value of an array over the given dimensions. See also the [`max(a,b)`](@ref) function to take the maximum of two or more arguments, which can be applied elementwise to arrays via `max.(a,b)`. +# Examples ```jldoctest julia> A = [1 2; 3 4] 2×2 Array{Int64,2}: diff --git a/base/regex.jl b/base/regex.jl index 2a3b3c92b8b15..b36442fd91a7d 100644 --- a/base/regex.jl +++ b/base/regex.jl @@ -81,13 +81,12 @@ listed after the ending quote, to change its behaviour: `\\s`, `\\W`, `\\w`, etc. match based on Unicode character properties. With this option, these sequences only match ASCII characters. - -For example, this regex has the first three flags enabled: - +# Examples ```jldoctest julia> match(r"a+.*b+.*?d\$"ism, "Goodbye,\\nOh, angry,\\nBad world\\n") RegexMatch("angry,\\nBad world") ``` +This regex has the first three flags enabled. """ macro r_str(pattern, flags...) Regex(pattern, flags...) end diff --git a/base/reshapedarray.jl b/base/reshapedarray.jl index ffc59bf9aa483..ab13135a74bb5 100644 --- a/base/reshapedarray.jl +++ b/base/reshapedarray.jl @@ -50,6 +50,7 @@ in which case its length is computed such that its product with all the specified dimensions is equal to the length of the original array `A`. The total number of elements must not change. +# Examples ```jldoctest julia> A = Vector(1:16) 16-element Array{Int64,1}: diff --git a/base/some.jl b/base/some.jl index 0245ddc3ac683..fe4ced04e0bb4 100644 --- a/base/some.jl +++ b/base/some.jl @@ -48,7 +48,6 @@ if any. Otherwise throw an error. Arguments of type [`Some`](@ref) are unwrapped. # Examples - ```jldoctest julia> something(nothing, 1) 1 diff --git a/base/special/exp.jl b/base/special/exp.jl index 87a10f6eabc33..493e5167f1e66 100644 --- a/base/special/exp.jl +++ b/base/special/exp.jl @@ -64,6 +64,7 @@ MIN_EXP(::Type{Float32}) = -103.97207708f0 # log 2^-150 Compute the natural base exponential of `x`, in other words ``e^x``. +# Examples ```jldoctest julia> exp(1.0) 2.718281828459045 diff --git a/base/subarray.jl b/base/subarray.jl index 206abb46e1561..3d72c7ff5b92c 100644 --- a/base/subarray.jl +++ b/base/subarray.jl @@ -105,6 +105,7 @@ given indices instead of making a copy. Calling [`getindex`](@ref) or [`setindex!`](@ref) on the returned `SubArray` computes the indices to the parent array on the fly without checking bounds. +# Examples ```jldoctest julia> A = [1 2; 3 4] 2×2 Array{Int64,2}: diff --git a/base/task.jl b/base/task.jl index d93cdbd2c84c4..b299246de7315 100644 --- a/base/task.jl +++ b/base/task.jl @@ -59,6 +59,7 @@ end Wrap an expression in a [`Task`](@ref) without executing it, and return the [`Task`](@ref). This only creates a task, and does not run it. +# Examples ```jldoctest julia> a1() = sum(i for i in 1:1000); @@ -91,6 +92,7 @@ current_task() = ccall(:jl_get_current_task, Ref{Task}, ()) Determine whether a task has exited. +# Examples ```jldoctest julia> a2() = sum(i for i in 1:1000); @@ -114,6 +116,7 @@ istaskdone(t::Task) = ((t.state == :done) | istaskfailed(t)) Determine whether a task has started executing. +# Examples ```jldoctest julia> a3() = sum(i for i in 1:1000); diff --git a/base/tuple.jl b/base/tuple.jl index c6a50da418657..0839e0a11b43c 100644 --- a/base/tuple.jl +++ b/base/tuple.jl @@ -6,6 +6,7 @@ A compact way of representing the type for a tuple of length `N` where all elements are of type `T`. +# Examples ```jldoctest julia> isa((1, 2, 3, 4, 5, 6), NTuple{6, Int}) true @@ -120,6 +121,7 @@ end Create a tuple of length `n`, computing each element as `f(i)`, where `i` is the index of the element. +# Examples ```jldoctest julia> ntuple(i -> 2*i, 4) (2, 4, 6, 8) diff --git a/base/views.jl b/base/views.jl index 7673445cee2e9..268000cf7765d 100644 --- a/base/views.jl +++ b/base/views.jl @@ -77,6 +77,7 @@ reference expression (e.g. `@view A[1,2:end]`), and should *not* be used as the an assignment (e.g. `@view(A[1,2:end]) = ...`). See also [`@views`](@ref) to switch an entire block of code to use views for slicing. +# Examples ```jldoctest julia> A = [1 2; 3 4] 2×2 Array{Int64,2}: diff --git a/doc/src/manual/types.md b/doc/src/manual/types.md index 3527f19f41109..514c5a063d5e3 100644 --- a/doc/src/manual/types.md +++ b/doc/src/manual/types.md @@ -1183,7 +1183,7 @@ Any If you apply [`supertype`](@ref) to other type objects (or non-type objects), a [`MethodError`](@ref) is raised: -```jldoctest +```jldoctest; filter = r"Closest candidates.*"s julia> supertype(Union{Float64,Int64}) ERROR: MethodError: no method matching supertype(::Type{Union{Float64, Int64}}) Closest candidates are: