Skip to content

Commit ad5d244

Browse files
committed
Moved docs out of helpDB, way more examples
Lots of our documentation for some relatively simple iterator methods had no examples. I added a bunch, moved docstrings out of HelpDB, and tried to illustrate some common use cases.
1 parent 2638c64 commit ad5d244

File tree

10 files changed

+563
-182
lines changed

10 files changed

+563
-182
lines changed

base/abstractarray.jl

+10
Original file line numberDiff line numberDiff line change
@@ -1338,6 +1338,16 @@ Call function `f` on each element of iterable `c`.
13381338
For multiple iterable arguments, `f` is called elementwise.
13391339
`foreach` should be used instead of `map` when the results of `f` are not
13401340
needed, for example in `foreach(println, array)`.
1341+
1342+
```jldoctest
1343+
julia> a
1344+
1:3:7
1345+
1346+
julia> foreach(x->println(x^2),a)
1347+
1
1348+
16
1349+
49
1350+
```
13411351
"""
13421352
foreach(f) = (f(); nothing)
13431353
foreach(f, itr) = (for x in itr; f(x); end; nothing)

base/array.jl

+94-1
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,17 @@ function findnz{T}(A::AbstractMatrix{T})
824824
return (I, J, NZs)
825825
end
826826

827+
"""
828+
findmax(itr) -> (x, index)
829+
830+
Returns the maximum element and its index.
831+
The collection must not be empty.
832+
833+
```jldoctest
834+
julia> findmax([8,0.1,-9,pi])
835+
(8.0,1)
836+
```
837+
"""
827838
function findmax(a)
828839
if isempty(a)
829840
throw(ArgumentError("collection must be non-empty"))
@@ -842,6 +853,17 @@ function findmax(a)
842853
return (m, mi)
843854
end
844855

856+
"""
857+
findmin(itr) -> (x, index)
858+
859+
Returns the minimum element and its index.
860+
The collection must not be empty.
861+
862+
```jldoctest
863+
julia> findmax([8,0.1,-9,pi])
864+
(-9.0,3)
865+
```
866+
"""
845867
function findmin(a)
846868
if isempty(a)
847869
throw(ArgumentError("collection must be non-empty"))
@@ -860,16 +882,87 @@ function findmin(a)
860882
return (m, mi)
861883
end
862884

885+
"""
886+
indmax(itr) -> Integer
887+
888+
Returns the index of the maximum element in a collection.
889+
```jldoctest
890+
julia> indmax([8,0.1,-9,pi])
891+
1
892+
```
893+
"""
863894
indmax(a) = findmax(a)[2]
895+
896+
"""
897+
indmin(itr) -> Integer
898+
899+
Returns the index of the minimum element in a collection.
900+
```jldoctest
901+
julia> indmin([8,0.1,-9,pi])
902+
3
903+
```
904+
"""
864905
indmin(a) = findmin(a)[2]
865906

866907
# similar to Matlab's ismember
867-
# returns a vector containing the highest index in b for each value in a that is a member of b
908+
"""
909+
indexin(a, b)
910+
911+
Returns a vector containing the highest index in `b` for
912+
each value in `a` that is a member of `b` . The output
913+
vector contains 0 wherever `a` is not a member of `b`.
914+
915+
```jldoctest
916+
julia> a = ['a', 'b', 'c', 'b', 'd', 'a'];
917+
918+
julia> b = ['a','b','c']
919+
920+
julia> indexin(a,b)
921+
6-element Array{Int64,1}:
922+
1
923+
2
924+
3
925+
2
926+
0
927+
1
928+
929+
julia> indexin(b,a)
930+
3-element Array{Int64,1}:
931+
6
932+
4
933+
3
934+
```
935+
"""
868936
function indexin(a::AbstractArray, b::AbstractArray)
869937
bdict = Dict(zip(b, 1:length(b)))
870938
[get(bdict, i, 0) for i in a]
871939
end
872940

941+
"""
942+
findin(a, b)
943+
944+
Returns the indices of elements in collection `a` that appear in collection `b`.
945+
946+
```jldoctest
947+
julia> a = collect(1:3:15)
948+
5-element Array{Int64,1}:
949+
1
950+
4
951+
7
952+
10
953+
13
954+
955+
julia> b = collect(2:4:10)
956+
3-element Array{Int64,1}:
957+
2
958+
6
959+
10
960+
961+
julia> findin(a,b) # 10 is the only common element
962+
1-element Array{Int64,1}:
963+
4
964+
```
965+
"""
873966
function findin(a, b)
874967
ind = Array{Int,1}(0)
875968
bset = Set(b)

0 commit comments

Comments
 (0)