I would like to know if it is possible to have a named pattern within a repeated pattern during function desctiption. For example: f[(a_)..]:=

Why I am asking

I am trying to accept a single association as an argument and collect all keys into one list and all values into another list named keys and values respectively. I would like to do all of this during function construction.

My Attempt

Expected Input

f[<|(keys_->values_)..|>]:={keys, values}

Expected Output

{{key1, key2},{value1,value2}}

Similar Questions

I found one similar question called Naming Repeated Patterns. However, this question is about naming the repeated portion x:(_->_).. rather than the part within the repeated portion (k_->v_)..

  • $\begingroup$ Welcome! I hesitate to provide an exact answer, because this is something easily discoverable by just trying it out. Your specific attempt won't work, because it'll end up constraining all of the keys to be identical in order to match the pattern. But the function you want is easiliy definable using Keys and Values applied to the association passed in to f. Why don't you give that a go, and if you still need help, update your question. $\endgroup$
    – lericr
    Aug 5 at 19:09
  • $\begingroup$ f[(a_)..]:= works just fine; try f[(a_) ..] := {a} followed by f[1,1,1,1]. But it does not do what you want with the association, nor should it. You can do f[ass_Association] := With[{keys = Keys[ass], values = Values[ass]}, ...] which works just as well though. $\endgroup$
    – Jason B.
    Aug 5 at 19:09
  • $\begingroup$ LOL. I always love it when people use that argument name for an association. $\endgroup$
    – lericr
    Aug 5 at 19:11
  • 1
    $\begingroup$ @lericr every time I find myself writing assoc_ I think, "dammit, I'm an adult and I can swear in my code if I want to" $\endgroup$
    – Jason B.
    Aug 5 at 19:17
  • $\begingroup$ @JasonB. I’m giggling. I’m such child. $\endgroup$
    – lericr
    Aug 5 at 19:25

1 Answer 1


f[(a_)..] requests that all "a" are identical. To match different arguments you must match all of them like: f[a:(_ ..)]. "a" will then contain a "Sequence". This is an object that inserts its arguments into any head. E.g.

List[Sequence[a, b]]
(* {a, b} *)

Therefore, to access the different arguments in f[a:(_ ..)] we must wrap "a" e.g. in a list. Here is an example:

f[x : (_ ..)] := Print["First=", {x}[[1]], ",Second=", {x}[[2]]];
f[a, b]

(* First=a,Second=b *)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.