# How to use \`Anonymous Function\` in \`Function Piping\`?

**URL:** <https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392>\
**Category:** General Usage\
**Tags:** question, function\
**Created:** [December 7, 2020, 10:08am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392 "2020-12-07T10:08:04Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)\
**Post date:** [December 7, 2020, 10:08am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/1 "2020-12-07T10:08:04Z")

</div>

Currently, I’m learning the [Function Piping](https://docs.julialang.org/en/v1/manual/functions/#Function-composition-and-piping). And I try to imitate the following example:

```julia
julia> 1:10 |> sum |> sqrt

```

with `function piping` to realize my purpose:

- Step 1: Square each element in the list,
- Step 2: Sum them up,
- Step 3: Sqrt(var…)

But I got into trouble even in _step 1_. My initial code snippet is:

```julia
1:5 |> (x->x^2)

```

the above does not work. Can anyone help me _to square each element in the list_ using **Function piping** with **anonymous function**.

* * *

* * *

**Background**  
I have some experience in `Mathematica`, and _step 1_ can be expressed as:

```julia
Map[#^2 &, Range[1, 5]] 

```

where, `#^2 &` is a pure function, which is called an `anonymous function` in Julia.

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 7, 2020, 10:17am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/2 "2020-12-07T10:17:47Z")

</div>

```julia
help?> |>
search: |>

  |>(x, f)

  Applies a function to the preceding argument. This allows for easy function chaining.

  Examples
  ≡≡≡≡≡≡≡≡≡≡

  julia> [1:5;] |> x->x.^2 |> sum |> inv
  0.01818181818181818

```

So:

```julia
1:5 |> x -> x .^ 2

```

---

<div class="post-metadata">

**Author:** ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)\
**Post date:** [December 7, 2020, 10:25am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/3 "2020-12-07T10:25:11Z")

</div>

By the way, could you help to explain the difference between `[1:5]` and `[1:5;]`? And why there should a `;` in the end of `[1:5;]`? (Just any reference is OK.)

I guesstimate that if there is `only one element` in the array, we should suffix some punctuation (here is the semicolon) to indicate whether this array is an **row vector** , **column vector**.

Can someone affirm my guess?

---

<div class="post-metadata">

**Author:** ![oheil](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oheil/32/220745_2.png) [@oheil](https://discourse.julialang.org/u/oheil)\
**Post date:** [December 7, 2020, 10:35am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/4 "2020-12-07T10:35:37Z")

</div>

[https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation](https://docs.julialang.org/en/v1/manual/arrays/#man-array-concatenation)

> If the arguments inside the square brackets are separated by semicolons ( `;` ) or newlines instead of commas, then their contents are _vertically concatenated_ together instead of the arguments being used as elements themselves.

This means on the other side, that `[1:5]` is an array with a `UnitRange` as the only element:

```julia
julia> [1:5]
1-element Array{UnitRange{Int64},1}:
 1:5

julia> length([1:5])
1

```

---

<div class="post-metadata">

**Author:** ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)\
**Post date:** [December 7, 2020, 10:39am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/5 "2020-12-07T10:39:49Z")

</div>

Thanks very much. You illuminate my puzzle.

---

<div class="post-metadata">

**Author:** ![archermarx](https://avatars.discourse-cdn.com/v4/letter/a/8edcca/32.png) [@archermarx](https://discourse.julialang.org/u/archermarx)\
**Post date:** [December 7, 2020, 5:31pm UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/6 "2020-12-07T17:31:43Z")

</div>

I think the main issue with your code segment is that you didn’t use a dot to broadcast the operation over the range and perform it elementwise

```julia
1:5 |> x -> x^2 # Will error, squaring a range not defined

1:5 .|> x -> x^2 # OK, passes each element of range into function

1:5 |> x -> x.^2 # also ok

```

---

<div class="post-metadata">

**Author:** ![KyleJT](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/kylejt/32/8929_2.png) [@KyleJT](https://discourse.julialang.org/u/KyleJT)\
**Post date:** [December 8, 2020, 2:25am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/8 "2020-12-08T02:25:53Z")

</div>

I get it. Thank you.

---

<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:** [December 8, 2020, 2:35am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/9 "2020-12-08T02:35:24Z")

</div>

> [@KyleJT](#):
>
> **Background**  
> I have some experience in `Mathematica` , and _step 1_ can be expressed as:
> 
> ```julia
> Map[#^2 &, Range[1, 5]] 
> 
> ```
> 
> where, `#^2 &` is a pure function, which is called an `anonymous function` in Julia.

By the way, the equivalent of this in Julia is

```julia
map(x -> x^2, 1:5)

```

---

<div class="post-metadata">

**Author:** ![DNF](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/dnf/32/10191_2.png) [@DNF](https://discourse.julialang.org/u/DNF)\
**Post date:** [December 8, 2020, 7:47am UTC](https://discourse.julialang.org/t/how-to-use-anonymous-function-in-function-piping/51392/10 "2020-12-08T07:47:24Z")

</div>

> [@KyleJT](#):
>
> By the way, could you help to explain the difference between `[1:5]` and `[1:5;]` ? And why there should a `;` in the end of `[1:5;]` ?

It’s weird that the documentation suggest collecting a range like this. You should generally never collect ranges, so this

> [@oheil](#):
>
> `1:5 |> x -> x .^ 2`

is better.

But if your _purpose_ is to

> [@KyleJT](#):
>
> - Step 1: Square each element in the list,
> - Step 2: Sum them up,
> - Step 3: Sqrt(var…)

then you shouldn’t use piping at all, since you will needlessly allocate intermediate arrays. Instead do this:

```julia-auto
sqrt(sum(abs2, 1:5))

```

(You can use `abs2` or `x->x^2` depending on whatever your specific needs are.)
