# How To Broadcast "in" to Compare Two Arrays?

**URL:** <https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280>\
**Category:** General Usage\
**Tags:** question, broadcast\
**Created:** [May 18, 2022, 8:09pm UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280 "2022-05-18T20:09:55Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![TheCedarPrince](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/thecedarprince/32/17323_2.png) [@TheCedarPrince](https://discourse.julialang.org/u/TheCedarPrince)\
**Post date:** [May 18, 2022, 8:09pm UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/1 "2022-05-18T20:09:55Z")

</div>

Hi all,

I was doing some googling today and stumbled across this interesting syntax for an “in” broadcast where I want to check to see if any element from the first array is present in the second array:

```julia
in([1, 2]).([1, 2, 3])

```

I haven’t really seen this type of broadcasting before.  
It returns a bit vector which makes sense but I am having trouble understanding the semantics of how this is working.  
Could anyone explain to me in simple terms how this is working?

Thanks!

~ tcp 🌳

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 18, 2022, 8:14pm UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/2 "2022-05-18T20:14:40Z")

</div>

~~`in(x)` is equivalent to `y -> in(x, y)`, i.e. `in(x)(y) == x in y`. That means that `in(x).(v)` will be like `[x in y for y in v]`~~

* * *

Edit, I accidentally got the above backwards as was pointed out by @rafael.guerra. The correct version of the above is:

`in(x)` is equivalent to `y -> in(y, x)`, i.e. `in(x)(y) == y in x`. That means that `in(x).(v)` will be like `[y in x for y in v]`

---

<div class="post-metadata">

**Author:** ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)\
**Post date:** [May 18, 2022, 8:27pm UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/3 "2022-05-18T20:27:34Z")

</div>

Sorry master, shouldn’t it be instead:  
`in(x)` is equivalent to `y -> y in x`, i.e. `in(x)(y) == y in x`  
That means that: `in(x).(y)` will be like: `y .∈ (x,)`

In the OP example, the following two are equivalent:

```julia
( in([1, 2]).([1, 2, 3]) ) == ( [1, 2, 3] .∈ ([1, 2],) ) # true

```

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 18, 2022, 8:38pm UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/4 "2022-05-18T20:38:36Z")

</div>

Yes, sorry I wrote the above backwards, I wasn’t at my computer to double check.

It’s good that it works this way because `in(x)` really **should** generate a predicate function that checks if `y` is in `x`!

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 19, 2022, 2:19am UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/5 "2022-05-19T02:19:00Z")

</div>

> [@rafael.guerra](#):
>
> `( [1, 2, 3] .∈ ([1, 2],) ) `

I see how this works, but this initially confused me since I would naively expect it to correspond to:  
` [1 ∈ ([1,2],), 2 ∈ ([1,2],), 3 ∈ ([1,2],)] == [0, 0, 0]`  
but the broadcast re-interprets the length-1 tuple `([1,2],)` into just `[1,2]`.

---

<div class="post-metadata">

**Author:** ![Mason](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mason/32/2423_2.png) [@Mason](https://discourse.julialang.org/u/Mason)\
**Post date:** [May 19, 2022, 2:34am UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/6 "2022-05-19T02:34:45Z")

</div>

Broadcast doesn’t reinterpret the tuple, it iterates over all of it’s elements (which is one). E.g.

```julia
julia> println.(());

julia> println.((1,));
1

julia> println.((1,2));
1
2

```

Wrapping something in a `Tuple` or a `Ref` is how we make something act like a ‘scalar’ to broadcast.

So for instance, if we want to add `[1, 2]` to each element of `[[1, 2], [3, 4], [5, 6]]`, then one thing we could write is

```julia
julia> [[1, 2], [3, 4], [5, 6]] .+ ([1, 2],)
3-element Vector{Vector{Int64}}:
 [2, 4]
 [4, 6]
 [6, 8]

```

---

<div class="post-metadata">

**Author:** ![jd-foster](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jd-foster/32/35824_2.png) [@jd-foster](https://discourse.julialang.org/u/jd-foster)\
**Post date:** [May 19, 2022, 2:42am UTC](https://discourse.julialang.org/t/how-to-broadcast-in-to-compare-two-arrays/81280/7 "2022-05-19T02:42:19Z")

</div>

I see, that helps me understand now. Thanks!
