Skip to content

Instantly share code, notes, and snippets.

@blackode
Last active March 27, 2017 15:25
Show Gist options
  • Save blackode/a1505bb9f5c1c54b024998ac385cb7a8 to your computer and use it in GitHub Desktop.
Save blackode/a1505bb9f5c1c54b024998ac385cb7a8 to your computer and use it in GitHub Desktop.
list of keys in ets tables
defmodule EtsKeys do
def keys(table_name) do
first_key = :ets.first(table_name)
keys(table_name, first_key, [first_key])
end
def keys(_table_name, '$end_of_table', ['$end_of_table'|acc]) do
acc
end
def keys(table_name, current_key, acc) do
next_key = :ets.next(table_name, current_key)
keys(table_name, next_key, [next_key|acc])
end
end
defmodule EtsKeys do
defp get_ets_keys_lazy(table_name) when is_atom(table_name) do
eot = :"$end_of_table"
Stream.resource(
fn -> [] end,
fn acc ->
case acc do
[] ->
case :ets.first(table_name) do
^eot -> {:halt, acc}
first_key -> {[first_key], first_key}
end
acc ->
case :ets.next(table_name, acc) do
^eot -> {:halt, acc}
next_key -> {[next_key], next_key}
end
end
end,
fn _acc -> :ok end
)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment