# \<\< operator for arrays

**URL:** <https://discourse.julialang.org/t/operator-for-arrays/17089>\
**Category:** Internals & Design\
**Created:** [November 2, 2018, 3:21pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089 "2018-11-02T15:21:03Z")\
**Posts on this page:** 10\
**Page:** 2

<div class="post-metadata">

**Author:** ![y4lu](https://avatars.discourse-cdn.com/v4/letter/y/47e85d/32.png) [@y4lu](https://discourse.julialang.org/u/y4lu)\
**Post date:** [November 4, 2018, 11:16pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/21 "2018-11-04T23:16:51Z")

</div>

I guess the trouble is where you start trying to reference which gate to open with strings, but `open("front")` opens a file instead

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [December 19, 2020, 8:57am UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/22 "2020-12-19T08:57:54Z")

</div>

Figured I’d add on to this thread because I had a similar thought. Mostly posting for posterity, as I assume likelihood of doing something like this is low.

Comparing the readability of these ways of calculating a result and adding it to a vector (assuming that it can’t be neatly done as a loop or generator):

```julia
a = []
push!(a, lengthyfunction1(lotsofargs))
push!(a, lengthyfunction2(lotsofargs))

a = []
b1 = lengthyfunction1(lotsofargs)
push!(a, b1)
b2 = lengthyfunction2(lotsofargs)
push!(a, b2)

a = Vector{Any}(undef, 2)
a[1] = lengthyfunction1(lotsofargs)
a[2] = lengthyfunction2(lotsofargs)

a = []
pusha! = Base.Fix1(push!, a)
lengthyfunction1(lotsofargs) |> pusha!
lengthyfunction2(lotsofargs) |> pusha!

a = []
←(x, y) = push!(x, y)
a ← lengthyfunction1(lotsofargs)
a ← lengthyfunction2(lotsofargs)

```

(or ↤ as noted upthread; just couldn’t find it in the latex lookup, so it might not be accessible enough)

To me the last option is definitely the cleanest. And it would be nice for a very common operation to have a clean operator. I hear the critique that it may be unclear that it is mutating though.

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 19, 2020, 2:13pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/23 "2020-12-19T14:13:41Z")

</div>

I prefer to define

```julia
module std
    export <<, endl, cout
    <<(io::IO, x) = (print(io, x); io)
    const endl = '\n'
    const cout = stdout
end

```

so that I can do:

```julia
julia> using .std; import .std: <<

julia> cout << "Hello world!" << endl;
Hello world!

```

which is so much cleaner and more natural than `println`.

---

<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:** [December 19, 2020, 2:46pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/24 "2020-12-19T14:46:10Z")

</div>

[In case anyone misreads it, @stevengj does not do this. It’s a joke about C++’s notoriously confusing abuse of bitshift operators for input and output.]

---

<div class="post-metadata">

**Author:** ![Oscar\_Smith](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oscar_smith/32/25343_2.png) [@Oscar\_Smith](https://discourse.julialang.org/u/Oscar_Smith)\
**Post date:** [December 19, 2020, 3:16pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/25 "2020-12-19T15:16:47Z")

</div>

Honestly, “enough power to make your code look and run like C++, and enough common sense to only choose the second” would be a pretty good moto for Julia.

---

<div class="post-metadata">

**Author:** ![klacru](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/klacru/32/27890_2.png) [@klacru](https://discourse.julialang.org/u/klacru)\
**Post date:** [December 19, 2020, 3:43pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/26 "2020-12-19T15:43:41Z")

</div>

> [@BioTurboNick](#):
>
> ↤

`\mapsfrom` is valid in julia-REPL

---

<div class="post-metadata">

**Author:** ![BioTurboNick](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/bioturbonick/32/6380_2.png) [@BioTurboNick](https://discourse.julialang.org/u/BioTurboNick)\
**Post date:** [December 19, 2020, 4:02pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/27 "2020-12-19T16:02:20Z")

</div>

touché 😆

---

<div class="post-metadata">

**Author:** ![dlfivefifty](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dlfivefifty/32/1959_2.png) [@dlfivefifty](https://discourse.julialang.org/u/dlfivefifty)\
**Post date:** [December 20, 2020, 7:49am UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/28 "2020-12-20T07:49:56Z")

</div>

Though @stevengj’s example is fine from the standpoint of not calling different actions the same thing, as his `<<` is in a different name space.

---

<div class="post-metadata">

**Author:** ![giordano](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/giordano/32/2166_2.png) [@giordano](https://discourse.julialang.org/u/giordano)\
**Post date:** [December 21, 2020, 10:32am UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/29 "2020-12-21T10:32:37Z")

</div>

> [@stevengj](#):
>
> ```julia
> <<(io::IO, x) = (print(x); io)
> 
> ```

I assume you wanted to do

```julia
    <<(io::IO, x) = (print(io, x); io)

```

so that

```julia
const cerr = stderr
cerr << "Hello world!" << endl;

```

works as expected

---

<div class="post-metadata">

**Author:** ![stevengj](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/stevengj/32/71_2.png) [@stevengj](https://discourse.julialang.org/u/stevengj)\
**Post date:** [December 21, 2020, 1:15pm UTC](https://discourse.julialang.org/t/operator-for-arrays/17089/30 "2020-12-21T13:15:47Z")

</div>

> [@giordano](#):
>
> I assume you wanted to do

Whoops, fixed. Forgetting to use the `io` argument in custom `show`-like methods seems to be a pretty common mistake, unfortunately.

[Previous page](https://discourse.julialang.org/t/operator-for-arrays/17089.md?page=1)
