Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save MichaelDimmitt/00e13d4aeb88840dc07e4b1328e6f34b to your computer and use it in GitHub Desktop.
Save MichaelDimmitt/00e13d4aeb88840dc07e4b1328e6f34b to your computer and use it in GitHub Desktop.
elixir, traversing maps and lists. Also look at iex history.

Elixir

Shrink a map into a head | tail by using another maps keys:

(note: you could skip a step here by just having a list of keys.)

Starting place: https://devdecks.io/2022-remove-multiple-key-value-pairs-from-map/

map = %{k1: :v1, k2: :v2, k3: :v3}  
exampleMap = %{k1: :anyName, k2: :valueDoesNotMatter}

removeMap = Map.keys(exampleMap)  
newMap1 = Map.drop(map, removeMap)

removeMap2 = Map.keys(newMap1)  
newMap2 = Map.drop(map, removeMap2)

IO.inspect([newMap1, newMap2])

Look at your current IEx process's iex_history queue:
(also in this post ... traversing a twice nested Keyword list)

first iteration: uses Enum.at to get by index and work on my machine.

(Process.info self) |>Enum.at(5) |>elem(1) |> Map.new |> Map.fetch(:iex_history) |> elem(1)|>
    IEx.History.each(&IO.inspect/1)

second iteration: uses Keyword.fetch to make the solution more compatable with other peoples computers.

(Process.info self) |>Enum.at(5) |>elem(1) |> Keyword.fetch(:iex_history) |> elem(1) |>
    IEx.History.each(&IO.inspect/1)

final iteration, uses get_in and avoids how the previous solutions added complications to the data structure wrapping parts tuples along the process.

self |> Process.info |> get_in([:dictionary,:iex_history]) |> IEx.History.each(&IO.inspect/1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment