# Confused by unique! example in Base Collections docs

**URL:** <https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096>\
**Category:** New to Julia\
**Created:** [October 1, 2022, 9:16pm UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096 "2022-10-01T21:16:43Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stinkymatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stinkymatt/32/42557_2.png) [@stinkymatt](https://discourse.julialang.org/u/stinkymatt)\
**Post date:** [October 1, 2022, 9:16pm UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096/1 "2022-10-01T21:16:43Z")

</div>

I’m confused by one of the examples for [Base.unique!](https://docs.julialang.org/en/v1/base/collections/#Base.unique!):

```julia
julia> unique!(iseven, [2, 3, 5, 7, 9])
2-element Vector{Int64}:
 2
 3

```

The way I read this code makes me think that the elements [2, 3] are both even. To sanity check, I modified this to print to the REPL to try to understand what was going on better and that output makes sense to me:

```julia
julia> foreach(n -> println(iseven(n)), [2, 3, 5, 7, 9])
true
false
false
false
false

```

How does the example make sense? I double checked that it is the same when executed in my REPL.

---

<div class="post-metadata">

**Author:** ![stinkymatt](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stinkymatt/32/42557_2.png) [@stinkymatt](https://discourse.julialang.org/u/stinkymatt)\
**Post date:** [October 1, 2022, 9:20pm UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096/2 "2022-10-01T21:20:42Z")

</div>

Disregard, I get it now. The elements [2,3] are the first to produce unique values, and none of the rest of the list does.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 1, 2022, 10:00pm UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096/3 "2022-10-01T22:00:54Z")

</div>

But you might be on to something. What if this function was called `mapunique` (and `mapunique!`), perhaps it would be clearer.

---

<div class="post-metadata">

**Author:** ![digital\_carver](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/digital_carver/32/33818_2.png) [@digital\_carver](https://discourse.julialang.org/u/digital_carver)\
**Post date:** [October 1, 2022, 10:22pm UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096/4 "2022-10-01T22:22:21Z")

</div>

So many other functions in Base use a similar pattern, where a function as a first argument serves to transform the data before the core functionality of the function is applied.

It’s really nice when used with the `do` syntax, but can be confusing at first, in cases such as this one. Overall I think it’s a nice use of multiple dispatch though, and easy to make sense of and remember once you’ve grasped the general idea. Giving only this one a `map...` prefix would be inconsistent and ultimately _more_ confusing.

---

<div class="post-metadata">

**Author:** ![Dan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dan/32/42581_2.png) [@Dan](https://discourse.julialang.org/u/Dan)\
**Post date:** [October 1, 2022, 11:04pm UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096/5 "2022-10-01T23:04:54Z")

</div>

Only this one? What about `mapreduce`, `mapfoldl`, `mapslices`? But it was just a suggestion. IMHO `uniqe(f,V)` to `unique(V)` is similar to `mapfoldl` to `foldl`

---

<div class="post-metadata">

**Author:** ![aplavin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/aplavin/32/222056_2.png) [@aplavin](https://discourse.julialang.org/u/aplavin)\
**Post date:** [October 2, 2022, 8:02am UTC](https://discourse.julialang.org/t/confused-by-unique-example-in-base-collections-docs/88096/6 "2022-10-02T08:02:36Z")

</div>

> [@digital\_carver](#):
>
> So many other functions in Base use a similar pattern, where a function as a first argument serves to transform the data before the core functionality of the function is applied.

That’s true, but there are differences in how exactly the inner function values are used.  
Many functions just apply it first, equivalent to `func(f, A) == func(map(f, A))`: eg `maximum` and `findmax`. Others do different things that cannot be expressed with a separate `map`: eg `argmax` and `unique`. I can easily understand how this could lead to confusion.
