ETS Tables and Processes - Introducing Elixir Book

Im tryng to run this example(11-7) and getting this error :

[error] Process #PID<0.101.0> raised an exception
** (ArgumentError) argument error
    (stdlib) :ets.new(:planemos, [:name_table, {:keypos, 2}])
    drop.ex:23: Drop.setup/0
    drop.ex:5: Drop.drop/0

This is the code:

defmodule Drop do
    require Planemo

    def drop do
        setup
        handle_drops
    end
    
    def handle_drops do
        receive do
            {from, planemo, distance} ->
                send(from, {planemo, distance, fall_velocity(planemo, distance)})
                handle_drops
        end
    end

    def fall_velocity(planemo, distance) when distance >= 0 do
        p = hd(:ets.lookup(:planemos, planemo))
        :math.sqrt(2 * Planemo.planemo(p, :gravity) * distance)
    end

    def setup do
        :ets.new(:planemos, [:name_table, {:keypos, Planemo.planemo(:name) + 1}])
        info = [
            {:mercury, 3.7, 4878, 57.9},
            {:venus, 8.9, 12104, 108.2},
            {:earth, 9.8, 12756, 149.6},
            {:moon, 1.6, 3475, 149.6},
            {:mars, 3.7, 6787, 227.9},
            {:ceres, 0.27, 950, 413.7},
            {:jupiter, 23.1, 142796, 778.3},
            {:saturn, 9.0, 120660, 1427.0},
            {:uranus, 8.7, 51118, 2871.0},
            {:neptune, 11.0, 30200, 4497.1},
            {:pluto, 0.6, 2300, 5913.0},
            {:haumea, 0.44, 1150, 6484.0},
            {:makemake, 0.5, 1500, 6850.0},
            {:eris, 0.8, 2400, 10210.0}]
            insert_into_table(info)
    end

    def insert_into_table([]) do #stop recursion
    :undefined
    end 

    def insert_into_table([{name, gravity, diameter, distance} | tail]) do
        :ets.insert(:planemos, Planemo.new(name: name, gravity: gravity, diameter: diameter, distance_from_sun: distance))
        insert_into_table(tail)
    end
end

I remember that you get the same error when you want to setup twice a table , but i checked that i no have another table.

2 Likes

Took me a while to see this… it has to be a :named_table, to be read as a table you gave a name, instead of a :name_table which you could read as a table of names, but this doesn’t exist in ETS…

4 Likes