# Add unique!(f, itr) #28415

**URL:** <https://discourse.julialang.org/t/add-unique-f-itr-28415/17913>\
**Category:** Internals & Design\
**Created:** [November 23, 2018, 3:45pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913 "2018-11-23T15:45:31Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [November 23, 2018, 3:45pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/1 "2018-11-23T15:45:31Z")

</div>

Should unique!(f, itr) support Real,AbstractString and Symbol just as unique!(itr) do?  
Also do we have to take care of the order of occurence when implementing it?

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [November 23, 2018, 4:34pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/2 "2018-11-23T16:34:16Z")

</div>

Hi,

`unique!` needs to make sure that it only works on types that can actually be updated in place (in this case: AbstractVector). e.g. `unique!(1:3)` won’t work, because you can’t update a range in place.

There is a fast-path implementation available for `unique!(itr::AbstractVector{...})` if the _element types_ of the `AbstractVector` can be sorted, which is the case for `Real`, `AbstractString` and `Symbol`, but not automatically for other element types.  
The same fast-path implementation doesn’t work for `unique!(f, itr)`, as you would only now if its sorted _after_ applying `f`, which might be quite costly and you wouldn’t want to execute it twice. (with a type unstable `f` you wouldn’t even know if all elements can be sorted…)

I had an implementation that mixed the issorted-fastpath checking with calling `f` while performing the `unqiue` operation and therefore circumventing the cost of calling f twice, but the added complexity wasn’t worth the improvement.

I’ve made a PR a while ago which improves situation and makes the various function consistent in their implementation (e.g. automatic widening):  
[https://github.com/JuliaLang/julia/pull/29038](https://github.com/JuliaLang/julia/pull/29038)

---

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [November 24, 2018, 5:30pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/3 "2018-11-24T17:30:15Z")

</div>

I have created a PR RFC : Added naive unique!(f, itr) function #30141 which fixes #28415.  
Please review it and comment for further amendments in it.

Earlier, I accidentally pushed it directly to origin/master branch for which I don’t have permission.  
Please tell me if it would cause any further problems?

---

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [November 25, 2018, 3:52pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/4 "2018-11-25T15:52:40Z")

</div>

I think the PR has been amended to the best of my knowledge and thanks to @oxinabox for helping me out.I am not getting why travis-ci build is failing repeatedly, any advice would be helpful.

---

<div class="post-metadata">

**Author:** ![laborg](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/laborg/32/5474_2.png) [@laborg](https://discourse.julialang.org/u/laborg)\
**Post date:** [November 25, 2018, 5:16pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/5 "2018-11-25T17:16:02Z")

</div>

Have you benchmarked it against my solution in the linked PR? Especially if you regard all kinds of element set your PR might not be optimal.

---

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [November 26, 2018, 5:19am UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/6 "2018-11-26T05:19:00Z")

</div>

I will benchmark it against your solution.Thanks for guiding me.

---

<div class="post-metadata">

**Author:** ![raghav9-97](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/raghav9-97/32/6286_2.png) [@raghav9-97](https://discourse.julialang.org/u/raghav9-97)\
**Post date:** [December 5, 2018, 3:56pm UTC](https://discourse.julialang.org/t/add-unique-f-itr-28415/17913/7 "2018-12-05T15:56:47Z")

</div>

When I run @btime unique!(x-\>2x, 1:10\_000); it says setindex! not defined for UnitRange{Int64}. I couldn’t understand what the error message says?  
Same problem occurs with the @benchmark macro.
