Created
April 14, 2016 19:31
-
-
Save pmarreck/fbe35c88496e3dbd53d892999b241a5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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