Sha256: c8272a7790718a16107659025736b98ff72e2fde018f58d564edc9bbbc3e445c
Contents?: true
Size: 772 Bytes
Versions: 327
Compression:
Stored size: 772 Bytes
Contents
defmodule BracketPush do @brackets %{ "{" => "}", "(" => ")", "[" => "]" } @doc """ Check that all the brackets and braces in the string are matched correctly, and nested correctly """ @spec check_brackets(String.t) :: boolean def check_brackets(str) do str |> String.replace(~r/[^\{\}\[\]\(\)]/, "") |> String.codepoints |> check([]) end defp check([], []), do: true defp check([], _), do: false defp check([h|t], acc) do cond do Map.has_key?(@brackets, h) -> check(t, [Map.get(@brackets, h) | acc]) Enum.empty?(acc) and !Map.has_key?(@brackets, h) -> false h != hd(acc) -> false h == hd(acc) -> check(t, tl(acc)) true -> true end end end
Version data entries
327 entries across 327 versions & 1 rubygems