Hello,
In Haskell we can write an argument of a function as pair@(x,y). That means that pair is the pair with the two elements x and y. Is there something similar in Julia? For example if I have a list of pairs, I would like to do
A single argument function would be: (p,) -> p[1]+p[2]. If we then replace p by (x,y) that means x, y = p, i.e. it iterates over the input argument and assigns to x and y.
It is also possible to extract elements from a struct with a similar syntax. For example:
You seem happy with the solution, but I was wondering if there is some miscommunication going on. Specifically, do you really want or need as patterns? Your example doesn’t actually use pair in the function, so it seems like an example of a case where the as pattern isn’t needed and isn’t used. As opposed to some other non-Julia/non-Haskell snippet like:
map(pair@(x,y) -> x+pair[2], my_list_of_pairs)
where you would be using the binding to pair established by the as pattern. I don’t think there is anything like an as pattern in Julia or provided in any package I know of. However MLStyle.jl is definitely worth checking out for fancy pattern matching.