# Anonymous function style

**URL:** <https://forum.elixirforum.com/t/anonymous-function-style/32399>\
**Category:** Questions\
**Tags:** code-style, anonymous-functions\
**Created:** [June 17, 2020, 5:34am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399 "2020-06-17T05:34:23Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![oliveiragahenrique](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/oliveiragahenrique/32/30594_2.png) [@oliveiragahenrique](https://forum.elixirforum.com/u/oliveiragahenrique)\
**Post date:** [June 17, 2020, 5:34am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/1 "2020-06-17T05:34:23Z")

</div>

I’m current learning Elixir by the book Elixir in Action 2nd Edition. On chapter 3 at Stream functions exercises i’ve solved them a bit different from the original repository.

Problem: Get the longest line length of a file

Book resoluiton:

```elixir
  def longest_line_length!(path) do
    path
    |> Stream.map(&String.replace(&1, "\n", ""))
    |> Stream.map(&String.length/1)
    |> Enum.max()
  end

```

My resolution:

```elixir
  def longest_line_length!(path) do
    File.stream!(path)
    |> Stream.map(&String.replace(&1, "\n", ""))
    |> Stream.map(&String.length(&1))
    |> Enum.max()
  end

```

Is there some fundamental difference between `Stream.map(&String.length/1)` and `Stream.map(&String.length(&1))` ?

Or it is just a style preference?

Personally I think it’s more clear to use `&1` even if it’s not necessary. Maybe it is just because I’m still getting used with the language. Any thoughts?

---

<div class="post-metadata">

**Author:** ![srowley](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/srowley/32/28720_2.png) [@srowley](https://forum.elixirforum.com/u/srowley)\
**Post date:** [June 17, 2020, 5:43am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/2 "2020-06-17T05:43:05Z")

</div>

I had the same reaction and preference when I first started learning Elixir. There may be some real difference between them that I am not aware of; I just think of it as a style difference.

Over time as I got accustomed to referring to a function as `Module.function/arity`, the former syntax (i.e. `&String.length/1`) became more natural and the latter started to feel redundant/unnecessary.

---

<div class="post-metadata">

**Author:** ![i-n-g-m-a-r](https://forum.elixirforum.com/letter_avatar_proxy/v4/letter/i/c67d28/32.png) [@i-n-g-m-a-r](https://forum.elixirforum.com/u/i-n-g-m-a-r)\
**Post date:** [June 17, 2020, 5:49am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/3 "2020-06-17T05:49:33Z")

</div>

2 calls vs 1

---

<div class="post-metadata">

**Author:** ![oliveiragahenrique](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/oliveiragahenrique/32/30594_2.png) [@oliveiragahenrique](https://forum.elixirforum.com/u/oliveiragahenrique)\
**Post date:** [June 17, 2020, 7:18am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/4 "2020-06-17T07:18:22Z")

</div>

Hmmm… can you explain a bit more? I’m learning yet.

I did not get how the `&String.length(&1)` is called twice, why is this so?

---

<div class="post-metadata">

**Author:** ![NobbZ](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/nobbz/32/27235_2.png) [@NobbZ](https://forum.elixirforum.com/u/NobbZ)\
**Post date:** [June 17, 2020, 7:22am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/5 "2020-06-17T07:22:42Z")

</div>

It’s not that that function is called twice, but it causes two function calls.

Using `&f(&1)` needs to first call the anonymous function which calls then the inner function call. Using `&f/1` creates a more direct “reference” to the target function and therefore removes one level of indirection.

---

<div class="post-metadata">

**Author:** ![Marcus](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/marcus/32/24640_2.png) [@Marcus](https://forum.elixirforum.com/u/Marcus)\
**Post date:** [June 17, 2020, 8:22am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/6 "2020-06-17T08:22:01Z")

</div>

But something like this will be optimised by the compiler.  
For example:

```elixir
  def foo(map) do
    map
    |> Enum.map(&String.upcase/1)
    |> Enum.map(&String.upcase(&1))
  end

```

becomes

```erlang
foo(_map@1) ->
    'Elixir.Enum':map(
      'Elixir.Enum':map(_map@1, fun 'Elixir.String':upcase/1),
      fun 'Elixir.String':upcase/1).

```

But `&String.upcase/1` should be the preferred form.

---

<div class="post-metadata">

**Author:** ![shanesveller](https://forum.elixirforum.com/user_avatar/forum.elixirforum.com/shanesveller/32/7962_2.png) [@shanesveller](https://forum.elixirforum.com/u/shanesveller)\
**Post date:** [June 17, 2020, 11:54am UTC](https://forum.elixirforum.com/t/anonymous-function-style/32399/7 "2020-06-17T11:54:59Z")

</div>

Besides the points above, if you’re not reordering or transforming arguments before calling the existing function, the `Mod.fun/arity` style is more succinct to read and write, especially for functions with arity \> 1. `fun(&1, &2, &3)` is repetitive in a way that isn’t productive.
