Elixir HashDict (Protocol.UndefinedError) protocol Enumerable not implemented for {1, 1, 1}

426 views Asked by At

Please, why tableroA run without problem and tableroB get error "...protocol Enumerable not implemented for {1, 1, 1}" in the next code:

def tableroA do
### generamos la rejilla
    s=Enum.to_list(1..9)
    rejilla=for cada <- s,  fila <-[1,2,3], col <- [1,2,3], do: {cada, fila, col}
    convalor=Enum.map(rejilla, &({&1,2}))
    Enum.into(convalor, HashDict.new)                                   
end
def tableroB do
    ### generamos la rejilla
    s=Enum.to_list(1..9)
    for cada <- s,  fila <-[1,2,3], col <- [1,2,3], do: {cada, fila, col}
    |>Enum.map(&({&1,2}))
    |>Enum.into(HashDict.new)                                   
end
1

There are 1 answers

0
sasajuric On

Due to precedence of |> operator your code is interpreted as:

for cada <- s,  fila <-[1,2,3], col <- [1,2,3], do: (
  {cada, fila, col}
  |>Enum.map(&({&1,2}))
  |>Enum.into(HashDict.new)
)

To resolve it, you can either put for comprehension in parentheses:

(for cada <- s,  fila <-[1,2,3], col <- [1,2,3], do: {cada, fila, col})
|> ...

Or use explicit do/end blocks:

for cada <- s,  fila <-[1,2,3], col <- [1,2,3] do {cada, fila, col} end
|> ...