humdinger
humdinger

Reputation: 531

Check if list of lists in guard clause for Elixir

What is the best way to check if an argument is a list of lists in a guard clause, or a list of key-value pairs?

The solution I came up with just grabs the head and does a check, but I feel like there must be a better way.

def stuff(items) when is_list(hd(items)) do
    something
end

Upvotes: 2

Views: 1923

Answers (1)

Dogbert
Dogbert

Reputation: 222118

but I feel like there must be a better way

Yes, there is. Use pattern matching like this:

def stuff([head | _]) when is_list(head) do
  something
end

Upvotes: 6

Related Questions