@@ -1492,6 +1492,11 @@ cat(n::Integer, x::Integer...) = reshape([x...], (ntuple(x->1, n-1)..., length(x
1492
1492
1493
1493
# # find ##
1494
1494
1495
+ _pairs (A:: Union{AbstractArray, AbstractDict, AbstractString, Tuple, NamedTuple} ) = pairs (A)
1496
+ _pairs (iter) = _pairs (IteratorSize (iter), iter)
1497
+ _pairs (:: Union{HasLength, HasShape} , iter) = zip (1 : length (iter), iter)
1498
+ _pairs (:: Union{SizeUnknown, IsInfinite} , iter) = zip (Iterators. countfrom (1 ), iter)
1499
+
1495
1500
"""
1496
1501
findnext(A, i::Integer)
1497
1502
@@ -1545,12 +1550,14 @@ end
1545
1550
"""
1546
1551
findfirst(A)
1547
1552
1548
- Return the index of the first `true` value in `A`.
1553
+ Return the index or key of the first `true` value in `A`.
1549
1554
Return `nothing` if no such value is found.
1550
1555
To search for other kinds of values, pass a predicate as the first argument.
1551
1556
1552
- Indices are of the same type as those returned by [`keys(A)`](@ref)
1553
- and [`pairs(A)`](@ref).
1557
+ Indices or keys are of the same type as those returned by [`keys(A)`](@ref)
1558
+ and [`pairs(A)`](@ref) for `AbstractArray`, `AbstractDict`, `AbstractString`
1559
+ `Tuple` and `NamedTuple` objects, and are linear indices starting at `1`
1560
+ for other iterables.
1554
1561
1555
1562
# Examples
1556
1563
```jldoctest
@@ -1576,7 +1583,22 @@ julia> findfirst(A)
1576
1583
CartesianIndex(2, 1)
1577
1584
```
1578
1585
"""
1579
- findfirst (A) = isempty (A) ? nothing : findnext (A, first (keys (A)))
1586
+ function findfirst (A)
1587
+ warned = false
1588
+ for (i, a) in _pairs (A)
1589
+ if ! warned && ! (a isa Bool)
1590
+ depwarn (" In the future `findfirst` will only work on boolean collections. Use `findfirst(x->x!=0, A)` instead." , :findfirst )
1591
+ warned = true
1592
+ end
1593
+ if a != 0
1594
+ return i
1595
+ end
1596
+ end
1597
+ return nothing
1598
+ end
1599
+
1600
+ # Needed for bootstrap, and allows defining only an optimized findnext method
1601
+ findfirst (A:: Union{AbstractArray, AbstractString} ) = findnext (A, first (keys (A)))
1580
1602
1581
1603
"""
1582
1604
findnext(predicate::Function, A, i)
@@ -1626,11 +1648,13 @@ end
1626
1648
"""
1627
1649
findfirst(predicate::Function, A)
1628
1650
1629
- Return the index of the first element of `A` for which `predicate` returns `true`.
1651
+ Return the index or key of the first element of `A` for which `predicate` returns `true`.
1630
1652
Return `nothing` if there is no such element.
1631
1653
1632
- Indices are of the same type as those returned by [`keys(A)`](@ref)
1633
- and [`pairs(A)`](@ref).
1654
+ Indices or keys are of the same type as those returned by [`keys(A)`](@ref)
1655
+ and [`pairs(A)`](@ref) for `AbstractArray`, `AbstractDict`, `AbstractString`
1656
+ `Tuple` and `NamedTuple` objects, and are linear indices starting at `1`
1657
+ for other iterables.
1634
1658
1635
1659
# Examples
1636
1660
```jldoctest
@@ -1659,7 +1683,16 @@ julia> findfirst(iseven, A)
1659
1683
CartesianIndex(2, 1)
1660
1684
```
1661
1685
"""
1662
- findfirst (testf:: Function , A) = isempty (A) ? nothing : findnext (testf, A, first (keys (A)))
1686
+ function findfirst (testf:: Function , A)
1687
+ for (i, a) in _pairs (A)
1688
+ testf (a) && return i
1689
+ end
1690
+ return nothing
1691
+ end
1692
+
1693
+ # Needed for bootstrap, and allows defining only an optimized findnext method
1694
+ findfirst (testf:: Function , A:: Union{AbstractArray, AbstractString} ) =
1695
+ findnext (testf, A, first (keys (A)))
1663
1696
1664
1697
"""
1665
1698
findprev(A, i)
@@ -1711,11 +1744,13 @@ end
1711
1744
"""
1712
1745
findlast(A)
1713
1746
1714
- Return the index of the last `true` value in `A`.
1747
+ Return the index or key of the last `true` value in `A`.
1715
1748
Return `nothing` if there is no `true` value in `A`.
1716
1749
1717
- Indices are of the same type as those returned by [`keys(A)`](@ref)
1718
- and [`pairs(A)`](@ref).
1750
+ Indices or keys are of the same type as those returned by [`keys(A)`](@ref)
1751
+ and [`pairs(A)`](@ref) for `AbstractArray`, `AbstractDict`, `AbstractString`
1752
+ `Tuple` and `NamedTuple` objects, and are linear indices starting at `1`
1753
+ for other iterables.
1719
1754
1720
1755
# Examples
1721
1756
```jldoctest
@@ -1743,7 +1778,22 @@ julia> findlast(A)
1743
1778
CartesianIndex(2, 1)
1744
1779
```
1745
1780
"""
1746
- findlast (A) = isempty (A) ? nothing : findprev (A, last (keys (A)))
1781
+ function findlast (A)
1782
+ warned = false
1783
+ for (i, a) in Iterators. reverse (_pairs (A))
1784
+ if ! warned && ! (a isa Bool)
1785
+ depwarn (" In the future `findlast` will only work on boolean collections. Use `findlast(x->x!=0, A)` instead." , :findlast )
1786
+ warned = true
1787
+ end
1788
+ if a != 0
1789
+ return i
1790
+ end
1791
+ end
1792
+ return nothing
1793
+ end
1794
+
1795
+ # Needed for bootstrap, and allows defining only an optimized findprev method
1796
+ findlast (A:: Union{AbstractArray, AbstractString} ) = findprev (A, last (keys (A)))
1747
1797
1748
1798
"""
1749
1799
findprev(predicate::Function, A, i)
@@ -1790,11 +1840,13 @@ end
1790
1840
"""
1791
1841
findlast(predicate::Function, A)
1792
1842
1793
- Return the index of the last element of `A` for which `predicate` returns `true`.
1843
+ Return the index or key of the last element of `A` for which `predicate` returns `true`.
1794
1844
Return `nothing` if there is no such element.
1795
1845
1796
- Indices are of the same type as those returned by [`keys(A)`](@ref)
1797
- and [`pairs(A)`](@ref).
1846
+ Indices or keys are of the same type as those returned by [`keys(A)`](@ref)
1847
+ and [`pairs(A)`](@ref) for `AbstractArray`, `AbstractDict`, `AbstractString`
1848
+ `Tuple` and `NamedTuple` objects, and are linear indices starting at `1`
1849
+ for other iterables.
1798
1850
1799
1851
# Examples
1800
1852
```jldoctest
@@ -1820,16 +1872,27 @@ julia> findlast(isodd, A)
1820
1872
CartesianIndex(2, 1)
1821
1873
```
1822
1874
"""
1823
- findlast (testf:: Function , A) = isempty (A) ? nothing : findprev (testf, A, last (keys (A)))
1875
+ function findlast (testf:: Function , A)
1876
+ for (i, a) in Iterators. reverse (_pairs (A))
1877
+ testf (a) && return i
1878
+ end
1879
+ return nothing
1880
+ end
1881
+
1882
+ # Needed for bootstrap, and allows defining only an optimized findprev method
1883
+ findlast (testf:: Function , A:: Union{AbstractArray, AbstractString} ) =
1884
+ findprev (testf, A, last (keys (A)))
1824
1885
1825
1886
"""
1826
1887
findall(f::Function, A)
1827
1888
1828
1889
Return a vector `I` of the indices or keys of `A` where `f(A[I])` returns `true`.
1829
1890
If there are no such elements of `A`, return an empty array.
1830
1891
1831
- Indices are of the same type as those returned by [`keys(A)`](@ref)
1832
- and [`pairs(A)`](@ref).
1892
+ Indices or keys are of the same type as those returned by [`keys(A)`](@ref)
1893
+ and [`pairs(A)`](@ref) for `AbstractArray`, `AbstractDict`, `AbstractString`
1894
+ `Tuple` and `NamedTuple` objects, and are linear indices starting at `1`
1895
+ for other iterables.
1833
1896
1834
1897
# Examples
1835
1898
```jldoctest
@@ -1875,9 +1938,6 @@ julia> findall(x -> x >= 0, d)
1875
1938
"""
1876
1939
findall (testf:: Function , A) = collect (first (p) for p in _pairs (A) if testf (last (p)))
1877
1940
1878
- _pairs (A:: Union{AbstractArray, AbstractDict, AbstractString, Tuple, NamedTuple} ) = pairs (A)
1879
- _pairs (iter) = zip (OneTo (typemax (Int)), iter) # safe for objects that don't implement length
1880
-
1881
1941
"""
1882
1942
findall(A)
1883
1943
0 commit comments