# What's the practical use of \*(1), &(2) , |(3), xor(4)?

**URL:** <https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489>\
**Category:** New to Julia\
**Created:** [January 20, 2018, 5:09am UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489 "2018-01-20T05:09:25Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 20, 2018, 5:09am UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/1 "2018-01-20T05:09:25Z")

</div>

Base.operators.jl contains the following definitions

```julia
*(x::Number) = x
(&)(x::Integer) = x
(|)(x::Integer) = x
xor(x::Integer) = x

```

The practical use of those functions is beyond my imagination. What would be examples of the usefulness of those functions?

---

<div class="post-metadata">

**Author:** ![Tamas\_Papp](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/tamas_papp/32/25949_2.png) [@Tamas\_Papp](https://discourse.julialang.org/u/Tamas_Papp)\
**Post date:** [January 20, 2018, 6:55am UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/2 "2018-01-20T06:55:49Z")

</div>

Look at the code right below that definition (`afoldl`).

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 21, 2018, 12:31am UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/3 "2018-01-21T00:31:14Z")

</div>

I see

```julia
afoldl(op,a) = a
afoldl(op,a,b) = op(a,b)
afoldl(op,a,b,c...) = afoldl(op, op(a,b), c...)

```

First statement

```julia
afoldl( &, 2 ) = 2
(&)(x::Integer) = x # this definition is not needed.

```

Second statement

```julia
afoldl( op, a, b ) = op( a, b )
afoldl( &, 2, 6 ) = (&)( 2, 6 ) # same as 2 & 6
(&)(x::Integer) = x # this definition is still not needed.

```

Therefore I’m still wondering: what’s the practical use of `(&)(x::Integer) = x`

* * *

Update: I didn’t look far enough.

```julia
 ($op)(a, b, c, xs...) = afoldl($op, ($op)(($op)(a,b),c), xs...)

# which may reduce to 
afoldl( ($op)(z) )

```

However

```julia
 ($op)(a, b, c, xs...) = afoldl($op, ($op)(a,b), c, xs...)

# would reduce no further than 
afoldl( ($op)(x, y) )

# which becomes the binary function call
$op(x,y)

```

Thus avoiding the need for a unary function.  
But as I note further below, the current implementation is what it is. No point changing it.

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 21, 2018, 2:07am UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/4 "2018-01-21T02:07:02Z")

</div>

What else would these operations mean? It’s standard in mathematics that a product of one item is equal to that item. The only reason the product of zero items isn’t defined in Julia is that without any arguments one doesn’t know what type of `1` to return.

---

<div class="post-metadata">

**Author:** ![yurivish](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/yurivish/32/307_2.png) [@yurivish](https://discourse.julialang.org/u/yurivish)\
**Post date:** [January 21, 2018, 2:52am UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/5 "2018-01-21T02:52:08Z")

</div>

> [@jandehaan](#):
>
> (&)(x::Integer)

Does this definition have any implications for the idea of using `&foo` to mean something like `Ref(foo)`? Numbers don’t need to be scalarized since they’re already single-element iterators, but it would still mean having the same operator, with the same number of arguments, meaning two different things based on its argument type.

---

<div class="post-metadata">

**Author:** ![jandehaan](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/jandehaan/32/6805_2.png) [@jandehaan](https://discourse.julialang.org/u/jandehaan)\
**Post date:** [January 21, 2018, 3:00pm UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/6 "2018-01-21T15:00:35Z")

</div>

I personally would favor an implementation that throws an error when a binary operator is being used as a unary operator. I most likely made a mistake if I wrote an expression like `xor(x)`. Implicitly converting a binary operator to a NOP operator seems somewhat arbitrary.

However, I don’t wish to belabor this point. It is what it is. I was just curious if the unary use of a binary operator has practical meaning. For example, I consider (-) to be a binary operator, but using it as a unary operator also makes sense because in that case it moves a number from one side of the number line to the other side. Example: `(-)(-2)`

---

<div class="post-metadata">

**Author:** ![StefanKarpinski](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stefankarpinski/32/24_2.png) [@StefanKarpinski](https://discourse.julialang.org/u/StefanKarpinski)\
**Post date:** [January 21, 2018, 3:24pm UTC](https://discourse.julialang.org/t/whats-the-practical-use-of-1-2-3-xor-4/8489/7 "2018-01-21T15:24:32Z")

</div>

That’s a good point although the syntax with and without parens are different, so technically no, but it would just be confusing since `&x` would mean something different than `&(x)`. Maybe open an issue?
