Collections and Data Structures
Iteration
Sequential iteration is implemented by the iterate function. The general for loop:
1. for i in iter # or "for i = iter"
2. # body
3. end
is translated into:
1. next = iterate(iter)
2. while next !== nothing
3. (i, state) = next
4. # body
5. next = iterate(iter, state)
6. end
The state object may be anything, and should be chosen appropriately for each iterable type. See the manual section on the iteration interface for more details about defining a custom iterable type.
1. iterate(iter [, state]) -> Union{Nothing, Tuple{Any, Any}}
Advance the iterator to obtain the next element. If no elements remain, nothing should be returned. Otherwise, a 2-tuple of the next element and the new iteration state should be returned.
1. IteratorSize(itertype::Type) -> IteratorSize
Given the type of an iterator, return one of the following values:
SizeUnknown()if the length (number of elements) cannot be determined in advance.HasLength()if there is a fixed, finite length.HasShape{N}()if there is a known length plus a notion of multidimensional shape (as for an array). In this caseNshould give the number of dimensions, and theaxesfunction is valid for the iterator.IsInfinite()if the iterator yields values forever.
The default value (for iterators that do not define this function) is HasLength(). This means that most iterators are assumed to implement length.
This trait is generally used to select between algorithms that pre-allocate space for their result, and algorithms that resize their result incrementally.
1. julia> Base.IteratorSize(1:5)
2. Base.HasShape{1}()
4. julia> Base.IteratorSize((2,3))
5. Base.HasLength()
1. IteratorEltype(itertype::Type) -> IteratorEltype
Given the type of an iterator, return one of the following values:
EltypeUnknown()if the type of elements yielded by the iterator is not known in advance.HasEltype()if the element type is known, andeltypewould return a meaningful value.
HasEltype() is the default, since iterators are assumed to implement eltype.
This trait is generally used to select between algorithms that pre-allocate a specific type of result, and algorithms that pick a result type based on the types of yielded values.
1. julia> Base.IteratorEltype(1:5)
2. Base.HasEltype()
Fully implemented by:
AbstractRangeUnitRangeTupleNumberAbstractArrayBitSetIdDictDictWeakKeyDictEachLineAbstractStringSetPairNamedTuple
Constructors and Types
1. AbstractRange{T}
Supertype for ranges with elements of type T. UnitRange and other types are subtypes of this.
1. OrdinalRange{T, S} <: AbstractRange{T}
Supertype for ordinal ranges with elements of type T with spacing(s) of type S. The steps should be always-exact multiples of oneunit, and T should be a “discrete” type, which cannot have values smaller than oneunit. For example, Integer or Date types would qualify, whereas Float64 would not (since this type can represent values smaller than oneunit(Float64). UnitRange, StepRange, and other types are subtypes of this.
1. AbstractUnitRange{T} <: OrdinalRange{T, T}
Supertype for ranges with a step size of oneunit(T) with elements of type T. UnitRange and other types are subtypes of this.
1. StepRange{T, S} <: OrdinalRange{T, S}
Ranges with elements of type T with spacing of type S. The step between each element is constant, and the range is defined in terms of a start and stop of type T and a step of type S. Neither T nor S should be floating point types. The syntax a:b:c with b > 1 and a, b, and c all integers creates a StepRange.
Examples
1. julia> collect(StepRange(1, Int8(2), 10))
2. 5-element Array{Int64,1}:
3. 1
4. 3
5. 5
6. 7
7. 9
9. julia> typeof(StepRange(1, Int8(2), 10))
10. StepRange{Int64,Int8}
12. julia> typeof(1:3:6)
13. StepRange{Int64,Int64}
1. UnitRange{T<:Real}
A range parameterized by a start and stop of type T, filled with elements spaced by 1 from start until stop is exceeded. The syntax a:b with a and b both Integers creates a UnitRange.
Examples
1. julia> collect(UnitRange(2.3, 5.2))
2. 3-element Array{Float64,1}:
3. 2.3
4. 3.3
5. 4.3
7. julia> typeof(1:10)
8. UnitRange{Int64}
1. LinRange{T}
A range with len linearly spaced elements between its start and stop. The size of the spacing is controlled by len, which must be an Int.
Examples
1. julia> LinRange(1.5, 5.5, 9)
2. 9-element LinRange{Float64}:
3. 1.5,2.0,2.5,3.0,3.5,4.0,4.5,5.0,5.5
General Collections
1. isempty(collection) -> Bool
Determine whether a collection is empty (has no elements).
Examples
1. julia> isempty([])
2. true
4. julia> isempty([1 2 3])
5. false
1. isempty(condition)
Return true if no tasks are waiting on the condition, false otherwise.
1. empty!(collection) -> collection
Remove all elements from a collection.
Examples
1. julia> A = Dict("a" => 1, "b" => 2)
2. Dict{String,Int64} with 2 entries:
3. "b" => 2
4. "a" => 1
6. julia> empty!(A);
8. julia> A
9. Dict{String,Int64} with 0 entries
1. length(collection) -> Integer
Return the number of elements in the collection.
Use lastindex to get the last valid index of an indexable collection.
Examples
1. julia> length(1:5)
2. 5
4. julia> length([1, 2, 3, 4])
5. 4
7. julia> length([1 2; 3 4])
8. 4
Fully implemented by:
AbstractRangeUnitRangeTupleNumberAbstractArrayBitSetIdDictDictWeakKeyDictAbstractStringSetNamedTuple
Iterable Collections
1. in(item, collection) -> Bool
2. ∈(item, collection) -> Bool
3. ∋(collection, item) -> Bool
Determine whether an item is in the given collection, in the sense that it is == to one of the values generated by iterating over the collection. Returns a Bool value, except if item is missing or collection contains missing but not item, in which case missing is returned (three-valued logic, matching the behavior of any and ==).
Some collections follow a slightly different definition. For example, Sets check whether the item isequal to one of the elements. Dicts look for key=>value pairs, and the key is compared using isequal. To test for the presence of a key in a dictionary, use haskey or k in keys(dict). For these collections, the result is always a Bool and never missing.
Examples
1. julia> a = 1:3:20
2. 1:3:19
4. julia> 4 in a
5. true
7. julia> 5 in a
8. false
10. julia> missing in [1, 2]
11. missing
13. julia> 1 in [2, missing]
14. missing
16. julia> 1 in [1, missing]
17. true
19. julia> missing in Set([1, 2])
20. false
1. ∉(item, collection) -> Bool
2. ∌(collection, item) -> Bool
Negation of ∈ and ∋, i.e. checks that item is not in collection.
Examples
1. julia> 1 ∉ 2:4
2. true
4. julia> 1 ∉ 1:3
5. false
1. eltype(type)
Determine the type of the elements generated by iterating a collection of the given type. For dictionary types, this will be a Pair{KeyType,ValType}. The definition eltype(x) = eltype(typeof(x)) is provided for convenience so that instances can be passed instead of types. However the form that accepts a type argument should be defined for new types.
Examples
1. julia> eltype(fill(1f0, (2,2)))
2. Float32
4. julia> eltype(fill(0x1, (2,2)))
5. UInt8
1. indexin(a, b)
Return an array containing the first index in b for each value in a that is a member of b. The output array contains nothing wherever a is not a member of b.
Examples
1. julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];
3. julia> b = ['a', 'b', 'c'];
5. julia> indexin(a, b)
6. 6-element Array{Union{Nothing, Int64},1}:
7. 1
8. 2
9. 3
10. 2
11. nothing
12. 1
14. julia> indexin(b, a)
15. 3-element Array{Union{Nothing, Int64},1}:
16. 1
17. 2
18. 3
1. unique(itr)
Return an array containing only the unique elements of collection itr, as determined by isequal, in the order that the first of each set of equivalent elements originally appears. The element type of the input is preserved.
Examples
1. julia> unique([1, 2, 6, 2])
2. 3-element Array{Int64,1}:
3. 1
4. 2
5. 6
7. julia> unique(Real[1, 1.0, 2])
8. 2-element Array{Real,1}:
9. 1
10. 2
1. unique(f, itr)
Returns an array containing one value from itr for each unique value produced by f applied to elements of itr.
Examples
1. julia> unique(x -> x^2, [1, -1, 3, -3, 4])
2. 3-element Array{Int64,1}:
3. 1
4. 3
5. 4
1. unique(A::AbstractArray; dims::Int)
Return unique regions of A along dimension dims.
Examples
1. julia> A = map(isodd, reshape(Vector(1:8), (2,2,2)))
2. 2×2×2 Array{Bool,3}:
3. [:, :, 1] =
4. 1 1
5. 0 0
7. [:, :, 2] =
8. 1 1
9. 0 0
11. julia> unique(A)
12. 2-element Array{Bool,1}:
13. 1
14. 0
16. julia> unique(A, dims=2)
17. 2×1×2 Array{Bool,3}:
18. [:, :, 1] =
19. 1
20. 0
22. [:, :, 2] =
23. 1
24. 0
26. julia> unique(A, dims=3)
27. 2×2×1 Array{Bool,3}:
28. [:, :, 1] =
29. 1 1
30. 0 0
1. unique!(f, A::AbstractVector)
Selects one value from A for each unique value produced by f applied to elements of A , then return the modified A.
This method is available as of Julia 1.1.
Examples
1. julia> unique!(x -> x^2, [1, -1, 3, -3, 4])
2. 3-element Array{Int64,1}:
3. 1
4. 3
5. 4
7. julia> unique!(n -> n%3, [5, 1, 8, 9, 3, 4, 10, 7, 2, 6])
8. 3-element Array{Int64,1}:
9. 5
10. 1
11. 9
13. julia> unique!(iseven, [2, 3, 5, 7, 9])
14. 2-element Array{Int64,1}:
15. 2
16. 3
1. unique!(A::AbstractVector)
Remove duplicate items as determined by isequal, then return the modified A. unique! will return the elements of A in the order that they occur. If you do not care about the order of the returned data, then calling (sort!(A); unique!(A)) will be much more efficient as long as the elements of A can be sorted.
Examples
1. julia> unique!([1, 1, 1])
2. 1-element Array{Int64,1}:
3. 1
5. julia> A = [7, 3, 2, 3, 7, 5];
7. julia> unique!(A)
8. 4-element Array{Int64,1}:
9. 7
10. 3
11. 2
12. 5
14. julia> B = [7, 6, 42, 6, 7, 42];
16. julia> sort!(B); # unique! is able to process sorted data much more efficiently.
18. julia> unique!(B)
19. 3-element Array{Int64,1}:
20. 6
21. 7
22. 42
1. allunique(itr) -> Bool
Return true if all values from itr are distinct when compared with isequal.
Examples
1. julia> a = [1; 2; 3]
2. 3-element Array{Int64,1}:
3. 1
4. 2
5. 3
7. julia> allunique([a, a])
8. false
1. reduce(op, itr; [init])
Reduce the given collection itr with the given binary operator op. If provided, the initial value init must be a neutral element for op that will be returned for empty collections. It is unspecified whether init is used for non-empty collections.
For empty collections, providing init will be necessary, except for some special cases (e.g. when op is one of +, *, max, min, &, |) when Julia can determine the neutral element of op.
Reductions for certain commonly-used operators may have special implementations, and should be used instead: maximum(itr), minimum(itr), sum(itr), prod(itr), any(itr), all(itr).
The associativity of the reduction is implementation dependent. This means that you can’t use non-associative operations like - because it is undefined whether reduce(-,[1,2,3]) should be evaluated as (1-2)-3 or 1-(2-3). Use foldl or foldr instead for guaranteed left or right associativity.
Some operations accumulate error. Parallelism will be easier if the reduction can be executed in groups. Future versions of Julia might change the algorithm. Note that the elements are not reordered if you use an ordered collection.
Examples
1. julia> reduce(*, [2; 3; 4])
2. 24
4. julia> reduce(*, [2; 3; 4]; init=-1)
5. -24
1. foldl(op, itr; [init])
Like reduce, but with guaranteed left associativity. If provided, the keyword argument init will be used exactly once. In general, it will be necessary to provide init to work with empty collections.
Examples
1. julia> foldl(=>, 1:4)
2. ((1 => 2) => 3) => 4
4. julia> foldl(=>, 1:4; init=0)
5. (((0 => 1) => 2) => 3) => 4
1. foldr(op, itr; [init])
Like reduce, but with guaranteed right associativity. If provided, the keyword argument init will be used exactly once. In general, it will be necessary to provide init to work with empty collections.
Examples
1. julia> foldr(=>, 1:4)
2. 1 => (2 => (3 => 4))
4. julia> foldr(=>, 1:4; init=0)
5. 1 => (2 => (3 => (4 => 0)))
1. maximum(f, itr)
Returns the largest result of calling function f on each element of itr.
Examples
1. julia> maximum(length, ["Julion", "Julia", "Jule"])
2. 6
1. maximum(itr)
Returns the largest element in a collection.
Examples
1. julia> maximum(-20.5:10)
2. 9.5
4. julia> maximum([1,2,3])
5. 3
1. maximum(A::AbstractArray; dims)
Compute the maximum value of an array over the given dimensions. See also the max(a,b) function to take the maximum of two or more arguments, which can be applied elementwise to arrays via max.(a,b).
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> maximum(A, dims=1)
7. 1×2 Array{Int64,2}:
8. 3 4
10. julia> maximum(A, dims=2)
11. 2×1 Array{Int64,2}:
12. 2
13. 4
1. maximum!(r, A)
Compute the maximum value of A over the singleton dimensions of r, and write results to r.
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> maximum!([1; 1], A)
7. 2-element Array{Int64,1}:
8. 2
9. 4
11. julia> maximum!([1 1], A)
12. 1×2 Array{Int64,2}:
13. 3 4
1. minimum(f, itr)
Returns the smallest result of calling function f on each element of itr.
Examples
1. julia> minimum(length, ["Julion", "Julia", "Jule"])
2. 4
1. minimum(itr)
Returns the smallest element in a collection.
Examples
1. julia> minimum(-20.5:10)
2. -20.5
4. julia> minimum([1,2,3])
5. 1
1. minimum(A::AbstractArray; dims)
Compute the minimum value of an array over the given dimensions. See also the min(a,b) function to take the minimum of two or more arguments, which can be applied elementwise to arrays via min.(a,b).
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> minimum(A, dims=1)
7. 1×2 Array{Int64,2}:
8. 1 2
10. julia> minimum(A, dims=2)
11. 2×1 Array{Int64,2}:
12. 1
13. 3
1. minimum!(r, A)
Compute the minimum value of A over the singleton dimensions of r, and write results to r.
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> minimum!([1; 1], A)
7. 2-element Array{Int64,1}:
8. 1
9. 3
11. julia> minimum!([1 1], A)
12. 1×2 Array{Int64,2}:
13. 1 2
1. extrema(itr) -> Tuple
Compute both the minimum and maximum element in a single pass, and return them as a 2-tuple.
Examples
1. julia> extrema(2:10)
2. (2, 10)
4. julia> extrema([9,pi,4.5])
5. (3.141592653589793, 9.0)
1. extrema(f, itr) -> Tuple
Compute both the minimum and maximum of f applied to each element in itr and return them as a 2-tuple. Only one pass is made over itr.
This method requires Julia 1.2 or later.
Examples
1. julia> extrema(sin, 0:π)
2. (0.0, 0.9092974268256817)
1. extrema(A::AbstractArray; dims) -> Array{Tuple}
Compute the minimum and maximum elements of an array over the given dimensions.
Examples
1. julia> A = reshape(Vector(1:2:16), (2,2,2))
2. 2×2×2 Array{Int64,3}:
3. [:, :, 1] =
4. 1 5
5. 3 7
7. [:, :, 2] =
8. 9 13
9. 11 15
11. julia> extrema(A, dims = (1,2))
12. 1×1×2 Array{Tuple{Int64,Int64},3}:
13. [:, :, 1] =
14. (1, 7)
16. [:, :, 2] =
17. (9, 15)
1. extrema(f, A::AbstractArray; dims) -> Array{Tuple}
Compute the minimum and maximum of f applied to each element in the given dimensions of A.
This method requires Julia 1.2 or later.
1. argmax(itr) -> Integer
Return the index of the maximum element in a collection. If there are multiple maximal elements, then the first one will be returned.
The collection must not be empty.
Examples
1. julia> argmax([8,0.1,-9,pi])
2. 1
4. julia> argmax([1,7,7,6])
5. 2
7. julia> argmax([1,7,7,NaN])
8. 4
1. argmax(A; dims) -> indices
For an array input, return the indices of the maximum elements over the given dimensions. NaN is treated as greater than all other values.
Examples
1. julia> A = [1.0 2; 3 4]
2. 2×2 Array{Float64,2}:
3. 1.0 2.0
4. 3.0 4.0
6. julia> argmax(A, dims=1)
7. 1×2 Array{CartesianIndex{2},2}:
8. CartesianIndex(2, 1) CartesianIndex(2, 2)
10. julia> argmax(A, dims=2)
11. 2×1 Array{CartesianIndex{2},2}:
12. CartesianIndex(1, 2)
13. CartesianIndex(2, 2)
1. argmin(itr) -> Integer
Return the index of the minimum element in a collection. If there are multiple minimal elements, then the first one will be returned.
The collection must not be empty.
Examples
1. julia> argmin([8,0.1,-9,pi])
2. 3
4. julia> argmin([7,1,1,6])
5. 2
7. julia> argmin([7,1,1,NaN])
8. 4
1. argmin(A; dims) -> indices
For an array input, return the indices of the minimum elements over the given dimensions. NaN is treated as less than all other values.
Examples
1. julia> A = [1.0 2; 3 4]
2. 2×2 Array{Float64,2}:
3. 1.0 2.0
4. 3.0 4.0
6. julia> argmin(A, dims=1)
7. 1×2 Array{CartesianIndex{2},2}:
8. CartesianIndex(1, 1) CartesianIndex(1, 2)
10. julia> argmin(A, dims=2)
11. 2×1 Array{CartesianIndex{2},2}:
12. CartesianIndex(1, 1)
13. CartesianIndex(2, 1)
1. findmax(itr) -> (x, index)
Return the maximum element of the collection itr and its index. If there are multiple maximal elements, then the first one will be returned. If any data element is NaN, this element is returned. The result is in line with max.
The collection must not be empty.
Examples
1. julia> findmax([8,0.1,-9,pi])
2. (8.0, 1)
4. julia> findmax([1,7,7,6])
5. (7, 2)
7. julia> findmax([1,7,7,NaN])
8. (NaN, 4)
1. findmax(A; dims) -> (maxval, index)
For an array input, returns the value and index of the maximum over the given dimensions. NaN is treated as greater than all other values.
Examples
1. julia> A = [1.0 2; 3 4]
2. 2×2 Array{Float64,2}:
3. 1.0 2.0
4. 3.0 4.0
6. julia> findmax(A, dims=1)
7. ([3.0 4.0], CartesianIndex{2}[CartesianIndex(2, 1) CartesianIndex(2, 2)])
9. julia> findmax(A, dims=2)
10. ([2.0; 4.0], CartesianIndex{2}[CartesianIndex(1, 2); CartesianIndex(2, 2)])
1. findmin(itr) -> (x, index)
Return the minimum element of the collection itr and its index. If there are multiple minimal elements, then the first one will be returned. If any data element is NaN, this element is returned. The result is in line with min.
The collection must not be empty.
Examples
1. julia> findmin([8,0.1,-9,pi])
2. (-9.0, 3)
4. julia> findmin([7,1,1,6])
5. (1, 2)
7. julia> findmin([7,1,1,NaN])
8. (NaN, 4)
1. findmin(A; dims) -> (minval, index)
For an array input, returns the value and index of the minimum over the given dimensions. NaN is treated as less than all other values.
Examples
1. julia> A = [1.0 2; 3 4]
2. 2×2 Array{Float64,2}:
3. 1.0 2.0
4. 3.0 4.0
6. julia> findmin(A, dims=1)
7. ([1.0 2.0], CartesianIndex{2}[CartesianIndex(1, 1) CartesianIndex(1, 2)])
9. julia> findmin(A, dims=2)
10. ([1.0; 3.0], CartesianIndex{2}[CartesianIndex(1, 1); CartesianIndex(2, 1)])
1. findmax!(rval, rind, A) -> (maxval, index)
Find the maximum of A and the corresponding linear index along singleton dimensions of rval and rind, and store the results in rval and rind. NaN is treated as greater than all other values.
1. findmin!(rval, rind, A) -> (minval, index)
Find the minimum of A and the corresponding linear index along singleton dimensions of rval and rind, and store the results in rval and rind. NaN is treated as less than all other values.
1. sum(f, itr)
Sum the results of calling function f on each element of itr.
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
1. julia> sum(abs2, [2; 3; 4])
2. 29
Note the important difference between sum(A) and reduce(+, A) for arrays with small integer eltype:
1. julia> sum(Int8[100, 28])
2. 128
4. julia> reduce(+, Int8[100, 28])
5. -128
In the former case, the integers are widened to system word size and therefore the result is 128. In the latter case, no such widening happens and integer overflow results in -128.
1. sum(itr)
Returns the sum of all elements in a collection.
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
1. julia> sum(1:20)
2. 210
1. sum(A::AbstractArray; dims)
Sum elements of an array over the given dimensions.
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> sum(A, dims=1)
7. 1×2 Array{Int64,2}:
8. 4 6
10. julia> sum(A, dims=2)
11. 2×1 Array{Int64,2}:
12. 3
13. 7
1. sum!(r, A)
Sum elements of A over the singleton dimensions of r, and write results to r.
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> sum!([1; 1], A)
7. 2-element Array{Int64,1}:
8. 3
9. 7
11. julia> sum!([1 1], A)
12. 1×2 Array{Int64,2}:
13. 4 6
1. prod(f, itr)
Returns the product of f applied to each element of itr.
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
1. julia> prod(abs2, [2; 3; 4])
2. 576
1. prod(itr)
Returns the product of all elements of a collection.
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
1. julia> prod(1:20)
2. 2432902008176640000
1. prod(A::AbstractArray; dims)
Multiply elements of an array over the given dimensions.
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> prod(A, dims=1)
7. 1×2 Array{Int64,2}:
8. 3 8
10. julia> prod(A, dims=2)
11. 2×1 Array{Int64,2}:
12. 2
13. 12
1. prod!(r, A)
Multiply elements of A over the singleton dimensions of r, and write results to r.
Examples
1. julia> A = [1 2; 3 4]
2. 2×2 Array{Int64,2}:
3. 1 2
4. 3 4
6. julia> prod!([1; 1], A)
7. 2-element Array{Int64,1}:
8. 2
9. 12
11. julia> prod!([1 1], A)
12. 1×2 Array{Int64,2}:
13. 3 8
1. any(itr) -> Bool
Test whether any elements of a boolean collection are true, returning true as soon as the first true value in itr is encountered (short-circuiting).
If the input contains missing values, return missing if all non-missing values are false (or equivalently, if the input contains no true value), following three-valued logic.
Examples
1. julia> a = [true,false,false,true]
2. 4-element Array{Bool,1}:
3. 1
4. 0
5. 0
6. 1
8. julia> any(a)
9. true
11. julia> any((println(i); v) for (i, v) in enumerate(a))
12. 1
13. true
15. julia> any([missing, true])
16. true
18. julia> any([false, missing])
19. missing
1. any(p, itr) -> Bool
Determine whether predicate p returns true for any elements of itr, returning true as soon as the first item in itr for which p returns true is encountered (short-circuiting).
If the input contains missing values, return missing if all non-missing values are false (or equivalently, if the input contains no true value), following three-valued logic.
Examples
1. julia> any(i->(4<=i<=6), [3,5,7])
2. true
4. julia> any(i -> (println(i); i > 3), 1:10)
5. 1
6. 2
7. 3
8. 4
9. true
11. julia> any(i -> i > 0, [1, missing])
12. true
14. julia> any(i -> i > 0, [-1, missing])
15. missing
17. julia> any(i -> i > 0, [-1, 0])
18. false
1. any!(r, A)
Test whether any values in A along the singleton dimensions of r are true, and write results to r.
Examples
1. julia> A = [true false; true false]
2. 2×2 Array{Bool,2}:
3. 1 0
4. 1 0
6. julia> any!([1; 1], A)
7. 2-element Array{Int64,1}:
8. 1
9. 1
11. julia> any!([1 1], A)
12. 1×2 Array{Int64,2}:
13. 1 0
1. all(itr) -> Bool
Test whether all elements of a boolean collection are true, returning false as soon as the first false value in itr is encountered (short-circuiting).
If the input contains missing values, return missing if all non-missing values are true (or equivalently, if the input contains no false value), following three-valued logic.
Examples
1. julia> a = [true,false,false,true]
2. 4-element Array{Bool,1}:
3. 1
4. 0
5. 0
6. 1
8. julia> all(a)
9. false
11. julia> all((println(i); v) for (i, v) in enumerate(a))
12. 1
13. 2
14. false
16. julia> all([missing, false])
17. false
19. julia> all([true, missing])
20. missing
1. all(p, itr) -> Bool
Determine whether predicate p returns true for all elements of itr, returning false as soon as the first item in itr for which p returns false is encountered (short-circuiting).
If the input contains missing values, return missing if all non-missing values are true (or equivalently, if the input contains no false value), following three-valued logic.
Examples
1. julia> all(i->(4<=i<=6), [4,5,6])
2. true
4. julia> all(i -> (println(i); i < 3), 1:10)
5. 1
6. 2
7. 3
8. false
10. julia> all(i -> i > 0, [1, missing])
11. missing
13. julia> all(i -> i > 0, [-1, missing])
14. false
16. julia> all(i -> i > 0, [1, 2])
17. true
1. all!(r, A)
Test whether all values in A along the singleton dimensions of r are true, and write results to r.
Examples
1. julia> A = [true false; true false]
2. 2×2 Array{Bool,2}:
3. 1 0
4. 1 0
6. julia> all!([1; 1], A)
7. 2-element Array{Int64,1}:
8. 0
9. 0
11. julia> all!([1 1], A)
12. 1×2 Array{Int64,2}:
13. 1 0
1. count(p, itr) -> Integer
2. count(itr) -> Integer
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
1. julia> count(i->(4<=i<=6), [2,3,4,5,6])
2. 3
4. julia> count([true, false, true, true])
5. 3
1. count(
2. pattern::Union{AbstractString,Regex},
3. string::AbstractString;
4. overlap::Bool = false,
5. )
Return the number of matches for pattern in string. This is equivalent to calling length(findall(pattern, string)) but more efficient.
If overlap=true, the matching sequences are allowed to overlap indices in the original string, otherwise they must be from disjoint character ranges.
1. any(p, itr) -> Bool
Determine whether predicate p returns true for any elements of itr, returning true as soon as the first item in itr for which p returns true is encountered (short-circuiting).
If the input contains missing values, return missing if all non-missing values are false (or equivalently, if the input contains no true value), following three-valued logic.
Examples
1. julia> any(i->(4<=i<=6), [3,5,7])
2. true
4. julia> any(i -> (println(i); i > 3), 1:10)
5. 1
6. 2
7. 3
8. 4
9. true
11. julia> any(i -> i > 0, [1, missing])
12. true
14. julia> any(i -> i > 0, [-1, missing])
15. missing
17. julia> any(i -> i > 0, [-1, 0])
18. false
1. all(p, itr) -> Bool
Determine whether predicate p returns true for all elements of itr, returning false as soon as the first item in itr for which p returns false is encountered (short-circuiting).
If the input contains missing values, return missing if all non-missing values are true (or equivalently, if the input contains no false value), following three-valued logic.
Examples
1. julia> all(i->(4<=i<=6), [4,5,6])
2. true
4. julia> all(i -> (println(i); i < 3), 1:10)
5. 1
6. 2
7. 3
8. false
10. julia> all(i -> i > 0, [1, missing])
11. missing
13. julia> all(i -> i > 0, [-1, missing])
14. false
16. julia> all(i -> i > 0, [1, 2])
17. true
1. foreach(f, c...) -> Nothing
Call function f on each element of iterable c. For multiple iterable arguments, f is called elementwise. foreach should be used instead of map when the results of f are not needed, for example in foreach(println, array).
Examples
1. julia> a = 1:3:7;
3. julia> foreach(x -> println(x^2), a)
4. 1
5. 16
6. 49
1. map(f, c...) -> collection
Transform collection c by applying f to each element. For multiple collection arguments, apply f elementwise.
See also: mapslices
Examples
1. julia> map(x -> x * 2, [1, 2, 3])
2. 3-element Array{Int64,1}:
3. 2
4. 4
5. 6
7. julia> map(+, [1, 2, 3], [10, 20, 30])
8. 3-element Array{Int64,1}:
9. 11
10. 22
11. 33
1. map!(function, destination, collection...)
Like map, but stores the result in destination rather than a new collection. destination must be at least as large as the first collection.
Examples
1. julia> a = zeros(3);
3. julia> map!(x -> x * 2, a, [1, 2, 3]);
5. julia> a
6. 3-element Array{Float64,1}:
7. 2.0
8. 4.0
9. 6.0
1. map!(f, values(dict::AbstractDict))
Modifies dict by transforming each value from val to f(val). Note that the type of dict cannot be changed: if f(val) is not an instance of the value type of dict then it will be converted to the value type if possible and otherwise raise an error.
map!(f, values(dict::AbstractDict)) requires Julia 1.2 or later.
Examples
1. julia> d = Dict(:a => 1, :b => 2)
2. Dict{Symbol,Int64} with 2 entries:
3. :a => 1
4. :b => 2
6. julia> map!(v -> v-1, values(d))
7. Base.ValueIterator for a Dict{Symbol,Int64} with 2 entries. Values:
8. 0
9. 1
1. mapreduce(f, op, itrs...; [init])
Apply function f to each element(s) in itrs, and then reduce the result using the binary function op. If provided, init must be a neutral element for op that will be returned for empty collections. It is unspecified whether init is used for non-empty collections. In general, it will be necessary to provide init to work with empty collections.
mapreduce is functionally equivalent to calling reduce(op, map(f, itr); init=init), but will in general execute faster since no intermediate collection needs to be created. See documentation for reduce and map.
mapreduce with multiple iterators requires Julia 1.2 or later.
Examples
1. julia> mapreduce(x->x^2, +, [1:3;]) # == 1 + 4 + 9
2. 14
The associativity of the reduction is implementation-dependent. Additionally, some implementations may reuse the return value of f for elements that appear multiple times in itr. Use mapfoldl or mapfoldr instead for guaranteed left or right associativity and invocation of f for every value.
1. mapfoldl(f, op, itr; [init])
Like mapreduce, but with guaranteed left associativity, as in foldl. If provided, the keyword argument init will be used exactly once. In general, it will be necessary to provide init to work with empty collections.
1. mapfoldr(f, op, itr; [init])
Like mapreduce, but with guaranteed right associativity, as in foldr. If provided, the keyword argument init will be used exactly once. In general, it will be necessary to provide init to work with empty collections.
1. first(coll)
Get the first element of an iterable collection. Return the start point of an AbstractRange even if it is empty.
Examples
1. julia> first(2:2:10)
2. 2
4. julia> first([1; 2; 3; 4])
5. 1
1. first(s::AbstractString, n::Integer)
Get a string consisting of the first n characters of s.
1. julia> first("∀ϵ≠0: ϵ²>0", 0)
2. ""
4. julia> first("∀ϵ≠0: ϵ²>0", 1)
5. "∀"
7. julia> first("∀ϵ≠0: ϵ²>0", 3)
8. "∀ϵ≠"
1. last(coll)
Get the last eleme
(本文超出站点单文档体积上限,已截断)
