Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Created April 14, 2016 19:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/fbe35c88496e3dbd53d892999b241a5f to your computer and use it in GitHub Desktop.
Save pmarreck/fbe35c88496e3dbd53d892999b241a5f to your computer and use it in GitHub Desktop.
defmodule EnumTools do
def find_deep_value(map, value) when is_map(map) do
map |> Map.values |> Enum.any?(
fn
val when is_map(val) -> find_deep_value(val, value)
val when val == value -> true
_ -> false
end)
end
end
# run this inline suite with "elixir #{__ENV__.file} test"
if System.argv |> List.first == "test" do
ExUnit.start
defmodule FindDeepTest do
use ExUnit.Case, async: true
alias EnumTools, as: ET
test "find a deep value" do
assert ET.find_deep_value(%{a: %{b: %{c: "d"}, e: "f"}, g: "h"}, "d")
end
# negative case
test "could not find a deep value" do
refute ET.find_deep_value(%{a: %{b: %{c: "d"}}}, "f")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment