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

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
Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
skosch
To my knowledge, put_in, Map.update etc. all have the one limitation of not automatically creating intermediate keys when needed (for exa...
New
jay1
Why is it that the mnesia database isn’t the most preferred database for use in Elixir/Phoenix?
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
New
marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New

Other popular topics Top

vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
aadeshere1
I have a another noob question about loop. Since elixir is immutable, while loop is not directly possible. total = 10 while total != 0 ...
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
AstonJ
Seen any cool LiveView demos, sample apps or examples? Please post them here! :003:
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
TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 42576 114
New

We're in Beta

About us Mission Statement