15 Jul 2013, 03:42
Dave_gnome_head_isolated_pragsmall

Dave Thomas (338 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
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

  

09 Sep 2013, 01:01
Felipe_avatar_pragsmall

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
14 Sep 2013, 18:27
Ac_simpsonized_pragsmall

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

20 Sep 2013, 19:32
Generic-user-small

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
04 Oct 2013, 20:02
Generic-user-small

Ganesan Janarthanam (Jana) (1 post)

Elixir 0.10.2

iex> Enum.map [1,2,3,4], IO.inspect(&1)
1
2 
3 
4 
[1,2,3,4] 
28 Oct 2013, 12:52
Generic-user-small

Johan Kristiansson (1 post)

iex> Enum.map [1, 2, 3, 4], &IO.inspect/1
1
2
3
4
[1, 2, 3, 4]
19 Jan 2014, 18:33
Generic-user-small

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))

02 Jul 2014, 18:16
Generic-user-small

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

  You must be logged in to comment