1
$\begingroup$

Consider some example definitions:

abba["1"] = 1;
abba["2"] = 2;

For some reason, I do not want to introduce the other form of representation such as associations.

Is it possible at all to extract keys for which abba takes the predefined values (i.e., "1","2")?

$\endgroup$
1
  • 2
    $\begingroup$ lookUp = ReplaceAll[ Merge[Identity] @ Map[Reverse] @ ReleaseHold[ DownValues[#] /. {RuleDelayed -> Rule, #[x_] :> x}]] &; Use as lookUp[abba]@{1, 2}, lookUp[abba]@2 etc $\endgroup$
    – kglr
    May 17 at 11:07

1 Answer 1

4
$\begingroup$
Keys[DownValues[abba]][[All, 1, 1]]
(* {"1", "2"} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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