tushar

tushar

Can length function be used for function argument pattern matching

The length(List) function can be used in Guards if we want to have different methods based on the length of a list , but can we use the length function for argument pattern matching of a function ?

I guess no we cant but still would love to have an expert opinion from here :slight_smile:

Marked As Solved

dimitarvp

dimitarvp

I still don’t get what kind of polymorphism you are after. If you explain your intended use-case with a more complete code sample then I’m sure we can give you a proper advice.

Outside of that I am with @wolf4earth – don’t mix guards with pattern matching, they serve different purposes. You can use both at the same time though and that’s perfectly valid if you want stricter checks.

Also Liked

wolf4earth

wolf4earth

To be honest I fail to see how this is more compact and sleek.

With patterns one matches on the structure of the data. It’s explicit and describes exactly how the data should look like. Guards exist to enforce further constraints which cannot be easily expressed in a structural pattern, such as a value being an integer.

That said, your particular examples can be expressed using structural patterns (and I would argue more clearly):

def my_function([x1, x2] = list) do
  # ...
end

def my_function([x1, x2, x3] = list) do
  # ...
end
wolf4earth

wolf4earth

It mixes concerns.

On the one hand you have structural matching using patterns and on the other hand you have logical matching using conditions (guards). Both are useful but serve subtly different purposes.

Mixing them is not necessarily a bad thing but from where I’m standing it’s good to know that a pattern is a pattern and a guard is a guard. I can look at a function header and know that there is no logical component to the match (such as an or) as long as there isn’t a guard clause.

Where Next?

Popular in Questions Top

jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

Other popular topics Top

JeremM34
Hello, how can I check the Phoenix version ? Thanks !
New
minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New