oliveiragahenrique

oliveiragahenrique

Anonymous function style

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:

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

My resolution:

  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?

Marked As Solved

NobbZ

NobbZ

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.

Also Liked

Marcus

Marcus

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

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

becomes

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.

shanesveller

shanesveller

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.

srowley

srowley

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.

Where Next?

Popular in Questions Top

Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
Kagamiiiii
Student & New to elixir. Nice language. I want to convert a english character, e.g. “a”, which is stored in a variable, to it’s asci...
New
polypush135
As many of you may have realized by now (sorry for all the posts here) I’ve been working on a db problem where I’m trying to aggregate a ...
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
vac
Hi, I'm quite new in Elixir and I'm trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and ...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
Fl4m3Ph03n1x
Background Let’s assume I have a typical GenServer that receives messages as requests, does some operation in a DB and returns responses....
New
fayddelight
I tried installing elixir 1.11.2 erlang 23.3.4 via asdf in my zsh shell. Enabled the versions locally and globally. When I list them ...
New

Other popular topics Top

peerreynders
Manning 2016 Halloween weekend sale via Deal of the Day Friday, October 28 - Half off all MEAPs - code WM102816LT Saturday, October 29 ...
326 29600 154
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 record...
New
quazar
How to set Jason to encode all fields in ecto schema, I don’t care about security and implementing only is taking long list of attributes...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod -- where is this set? Thanks.
New
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
chensan
I have a User schema with a :from_id field set to type :string: defmodule TweetBot.Repo.Migrations.CreateUsers do use Ecto.Migration ...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New

We're in Beta

About us Mission Statement