Skip to content

Instantly share code, notes, and snippets.

@DanCouper
Created January 26, 2018 08:52
Show Gist options
  • Save DanCouper/4fc7dcd34329507fdb847ca7cbec11be to your computer and use it in GitHub Desktop.
Save DanCouper/4fc7dcd34329507fdb847ca7cbec11be to your computer and use it in GitHub Desktop.
defmodule MapExtras do
def take_strict!(map, keys) do
existing_keys = Map.keys(map)
case Enum.all?(keys, fn(k) -> Enum.member?(existing_keys, k) end) do
true -> Map.take(map, keys)
false -> raise "One or more of the keys passed does not exist in the map"
end
end
def take_strict(map, keys) do
existing_keys = Map.keys(map)
case Enum.all?(keys, fn(k) -> Enum.member?(existing_keys, k) end) do
true -> {:ok, Map.take(map, keys)}
false -> {:error, "One or more of the keys passed does not exist in the map"}
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment