small medium large xlarge

Dave_gnome_head_isolated_pragsmall
15 Jul 2013, 03:42
Dave Thomas (366 posts)
  • Use the &1,… notation to rewrite the following.

    • Enum.map [1,2,3,4], fn x -> x + 2 end
    • Enum.each [1,2,3,4], fn x -> IO.puts x end

A Possible Solution</summary>

iex(1)> Enum.map [1,2,3,4], &1 + 2
[3, 4, 5, 6]

iex(2)> Enum.each [1,2,3,4], IO.puts(&1)
1
2
3
4
:ok

  

</details>

Felipe_avatar_pragsmall
09 Sep 2013, 01:01
Felipe Coury (2 posts)

The second one doesn’t work for me:

iex(32)>  Enum.each [1,2,3,4], IO.puts(&1)
** (FunctionClauseError) no function clause matching in IO.puts/2
    /private/tmp/elixir-JhMW/elixir-0.10.2/lib/elixir/lib/io.ex:120: IO.puts(#PID<0.36.0>, 1)
    /private/tmp/elixir-JhMW/elixir-0.10.2/lib/elixir/lib/enum.ex:351: Enum."-each/2-lists^foreach/1-0-"/2
    /private/tmp/elixir-JhMW/elixir-0.10.2/lib/elixir/lib/enum.ex:351: Enum.each/2
    erl_eval.erl:569: :erl_eval.do_apply/6
    src/elixir.erl:138: :elixir.eval_forms/3

It seems that IO.puts doesn’t work with Intergers:

iex(32)> IO.puts 1
** (FunctionClauseError) no function clause matching in IO.puts/2
    /private/tmp/elixir-JhMW/elixir-0.10.2/lib/elixir/lib/io.ex:120: IO.puts(#PID<0.36.0>, 1)
    erl_eval.erl:569: :erl_eval.do_apply/6
    src/elixir.erl:138: :elixir.eval_forms/3
Ac_simpsonized_pragsmall
14 Sep 2013, 18:27
Antonio Carlos da Graça Mota Durão de Souza (5 posts)

Possible fix:

Pass a string, instead of a Integer, to IO.puts.

Enum.each [1,2,3,4], fn x -> IO.puts "#{x}" end

My environment: Erlang R16B01 (erts-5.10.2) [source-bdf5300] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll:false] Interactive Elixir (0.10.2-dev)

AC

Generic-user-small
20 Sep 2013, 19:32
F Paul (1 post)

the possible solution suggested, even with adjusting for the error with puts and integers doesn’t work:

iex> Enum.each 1..4, IO.puts("#{&1}")
** (ArgumentError) argument error
:erl_eval.expr/3

this works though:

iex> Enum.each [1,2,3,4], &IO.puts("#{&1}")
1
2
3
4
:ok
Generic-user-small
28 Oct 2013, 12:52
Johan Kristiansson (1 post)
iex> Enum.map [1, 2, 3, 4], &IO.inspect/1
1
2
3
4
[1, 2, 3, 4]
Generic-user-small
19 Jan 2014, 18:33
Ramzi AKREMI (1 post)

Elixir 0.12.2

Enum.map [1,2,3,4], &(&1 + 2)

Enum.each [1,2,3,4], &(IO.puts(&1))

Generic-user-small
02 Jul 2014, 18:16
Federico Tomassetti (5 posts)

In the book you refer to IO.inspect, while here you refer to IO.puts, I guess you should change the suggested solution to match the exercise as shown in the book

Generic-user-small
12 Oct 2014, 10:06
Dzianis Dashkevich (1 post)

Elixir 0.15.1

Enum.map [1, 2, 3, 4], &(&1 + 2)

Enum.each [1, 2, 3, 4], &IO.inspect/1
Generic-user-small
19 Dec 2015, 18:37
Nathan Hornby (5 posts)

Elixir v1.1

I was quite proud of my combination solution:

[1, 2, 3, 4]
 |> Enum.map(&(&1 + 2))
 |> Enum.each(&IO.inspect/1)

Getting the hang of those pipes! Although obviously this is a twist on the question, as this isn’t using the fresh list on the each.

20206604202_77853bea43_z_pragsmall
09 Jan 2016, 09:49
Paul Sladek (1 post)

v1.2.0

From a file:

first_result = Enum.map [1, 2, 3, 4], &(IO.inspect &1 + 2)

IO.puts first_result

second_result = Enum.each [1,2,3,4], &(IO.inspect &1)

IO.puts second_result

From iex:

Enum.map [1, 2, 3, 4], &(&1 + 2)

Enum.each [1,2,3,4], &(IO.inspect &1) #Dzianis Dashkevich version also works

Ericheadshotsmall_pragsmall
23 Jan 2016, 21:57
Eric Hutzelman (1 post)

Those solutions in the original post don’t appear to be valid without the surrounding function context &().

Generic-user-small
31 Jan 2016, 11:50
Ivo Balbaert (3 posts)

Indeed, in ELixir 1.2 at least they give a CompileError:

IO.puts Enum.map [1,2,3,4], &1 + 2 # CompileError: unhandled &1 outside of a capture

Please correct the text.

You must be logged in to comment