Skip to content

Commit

Permalink
document unique(i -> a[i], eachindex(a)) trick (#45291)
Browse files Browse the repository at this point in the history
Co-authored-by: Milan Bouchet-Valat <nalimilan@club.fr>
  • Loading branch information
stevengj and nalimilan authored May 16, 2022
1 parent 7e37de4 commit 6962c91
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,28 @@ julia> unique(x -> x^2, [1, -1, 3, -3, 4])
3
4
```
This functionality can also be used to extract the *indices* of the first
occurrences of unique elements in an array:
```jldoctest
julia> a = [3.1, 4.2, 5.3, 3.1, 3.1, 3.1, 4.2, 1.7];
julia> i = unique(i -> a[i], eachindex(a))
4-element Vector{Int64}:
1
2
3
8
julia> a[i]
4-element Vector{Float64}:
3.1
4.2
5.3
1.7
julia> a[i] == unique(a)
true
```
"""
function unique(f, C; seen::Union{Nothing,Set}=nothing)
out = Vector{eltype(C)}()
Expand Down

0 comments on commit 6962c91

Please sign in to comment.