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

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
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
New
saif
Hello everyone, Long time lurker first time poster here. I’ve recently begun working on Elixir full-time again! :raised_hands: It’s been...
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

We're in Beta

About us Mission Statement