Skip to content

Commit

Permalink
Move docs inline from helpdb/Base.jl
Browse files Browse the repository at this point in the history
  • Loading branch information
xorJane committed Dec 22, 2016
1 parent 5989eaf commit 69c7554
Show file tree
Hide file tree
Showing 16 changed files with 575 additions and 655 deletions.
30 changes: 30 additions & 0 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,41 @@ _length(A) = length(A)
endof(a::AbstractArray) = length(a)
first(a::AbstractArray) = a[first(eachindex(a))]

"""
first(coll)
Get the first element of an iterable collection. Returns the start point of a
`Range` even if it is empty.
```jldoctest
julia> first(2:2:10)
2
julia> first([1; 2; 3; 4])
1
```
"""
function first(itr)
state = start(itr)
done(itr, state) && throw(ArgumentError("collection must be non-empty"))
next(itr, state)[1]
end

"""
last(coll)
Get the last element of an ordered collection, if it can be computed in O(1) time. This is
accomplished by calling [`endof`](@ref) to get the last index. Returns the end
point of a `Range` even if it is empty.
```jldoctest
julia> last(1:2:10)
9
julia> last([1; 2; 3; 4])
4
```
"""
last(a) = a[end]

"""
Expand Down
74 changes: 74 additions & 0 deletions base/array.jl
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,13 @@ function getindex(::Type{Any}, vals::ANY...)
end
getindex(::Type{Any}) = Array{Any,1}(0)

"""
fill!(A, x)
Fill array `A` with the value `x`. If `x` is an object reference, all elements will refer to
the same object. `fill!(A, Foo())` will return `A` filled with the result of evaluating
`Foo()` once.
"""
function fill!(a::Union{Array{UInt8}, Array{Int8}}, x::Integer)
ccall(:memset, Ptr{Void}, (Ptr{Void}, Cint, Csize_t), a, x, length(a))
return a
Expand Down Expand Up @@ -205,6 +212,73 @@ dims)` will return an array filled with the result of evaluating `Foo()` once.
fill(v, dims::Dims) = fill!(Array{typeof(v)}(dims), v)
fill(v, dims::Integer...) = fill!(Array{typeof(v)}(dims...), v)

"""
zeros(type, dims)
Create an array of all zeros of specified type.
The type defaults to `Float64` if not specified.
```jldoctest
julia> zeros(Int8, 2, 3)
2×3 Array{Int8,2}:
0 0 0
0 0 0
```
"""
zeros

"""
zeros(A)
Create an array of all zeros with the same element type and shape as `A`.
```jldoctest
julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4
julia> zeros(A)
2×2 Array{Int64,2}:
0 0
0 0
```
"""
zeros

"""
ones(type, dims)
Create an array of all ones of specified type. The type defaults to `Float64` if not specified.
```jldoctest
julia> ones(Complex128, 2, 3)
2×3 Array{Complex{Float64},2}:
1.0+0.0im 1.0+0.0im 1.0+0.0im
1.0+0.0im 1.0+0.0im 1.0+0.0im
```
"""
ones

"""
ones(A)
Create an array of all ones with the same element type and shape as `A`.
```jldoctest
julia> A = [1 2; 3 4]
2×2 Array{Int64,2}:
1 2
3 4
julia> ones(A)
2×2 Array{Int64,2}:
1 1
1 1
```
"""
ones

for (fname, felt) in ((:zeros,:zero), (:ones,:one))
@eval begin
($fname)(T::Type, dims...) = fill!(Array{T}(dims...), ($felt)(T))
Expand Down
17 changes: 17 additions & 0 deletions base/bitarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1301,6 +1301,23 @@ function (|)(B::BitArray, x::Bool)
end
(|)(x::Bool, B::BitArray) = B | x

"""
xor(x, y)
⊻(x, y)
Bitwise exclusive or of `x` and `y`. The infix operation
`a ⊻ b` is a synonym for `xor(a,b)`, and
`⊻` can be typed by tab-completing `\\xor`
or `\\veebar` in the Julia REPL.
```jldoctest
julia> [true; true; false] ⊻ [true; false; false]
3-element Array{Bool,1}:
false
true
false
```
"""
function xor(B::BitArray, x::Bool)
x ? ~B : copy(B)
end
Expand Down
17 changes: 17 additions & 0 deletions base/bool.jl
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,23 @@ typemax(::Type{Bool}) = true

## boolean operations ##

"""
!(x)
Boolean not.
```jldoctest
julia> !true
false
julia> !false
true
julia> ![true false true]
1×3 Array{Bool,2}:
false true false
```
"""
function !(x::Bool)
## We need a better heuristic to detect this automatically
@_pure_meta
Expand Down
Loading

0 comments on commit 69c7554

Please sign in to comment.