1
struct EnumerableUnique{T,TKEY,S,Q<:Function} <: Enumerable
2 23
    source::S
3
    f::Q
4
end
5

6
function unique(source::Enumerable, f::Function, f_expr::Expr)
7 23
    T = eltype(source)
8
    S = typeof(source)
9 23
    TKEY = Base._return_type(f, Tuple{T,})
10 23
    return EnumerableUnique{T,TKEY,S,typeof(f)}(source, f)
11
end
12

13 0
Base.eltype(::Type{EnumerableUnique{T,TKEY,S,Q}}) where {T,TKEY,S,Q} = T
14

15
function Base.iterate(iter::EnumerableUnique{T,TKEY,S,Q}) where {T,TKEY,S,Q}
16 5
    ret = iterate(iter.source)
17
    
18 8
    ret===nothing && return nothing
19

20 5
    observed_keys = Set{TKEY}()
21

22 0
    first_element = ret[1]
23 0
    source_state = ret[2]
24

25 5
    key_first_element = iter.f(first_element)
26

27 5
    push!(observed_keys, key_first_element)
28

29 8
    return first_element, (observed_keys=observed_keys, source_state=source_state)
30
end
31

32
function Base.iterate(iter::EnumerableUnique{T,TKEY,S,Q}, state) where {T,TKEY,S,Q}
33 23
    ret = iterate(iter.source, state.source_state)
34

35 23
    ret===nothing && return nothing
36

37 23
    while true
38
        current_element = ret[1]
39 20
        key_current_element=iter.f(current_element)
40 23
        if key_current_element in state.observed_keys
41 20
            ret = iterate(iter.source, ret[2])
42 23
            ret===nothing && return nothing
43
        else
44 20
            push!(state.observed_keys, key_current_element)
45

46 23
            return current_element, (observed_keys=state.observed_keys, source_state=ret[2])
47
        end
48
    end
49
end

Read our documentation on viewing source code .

Loading