1
$\begingroup$

I frequently use this method with KeyValueMap in order to match and refer to named parts (ie, "myKey", "myValue")

EDIT: to refer to named parts in the function f without an external function definition of f - see comments.

<|"a" -> 1, "b" -> 2|> // KeyValueMap[List /* Replace[{myKey_, myValue_} :> f[myKey, myValue]]]

{f["a", 1], f["b", 2]}

Is there a syntax to match the pair without applying List or normalizing the Association? ( Normal // ReplaceAll[(myKey_ -> myValue_) :> f[myKey, myValue]])?

$\endgroup$
  • 5
    $\begingroup$ Why not just KeyValueMap[f]@<|"a" -> 1, "b" -> 2|>? $\endgroup$ – Leonid Shifrin Oct 3 '16 at 20:21
  • $\begingroup$ @LeonidShifrin, in order to match and refer to named parts in f - eg myKey <> ToString[myValue] $\endgroup$ – alancalvitti Oct 3 '16 at 20:28
  • 1
    $\begingroup$ What exactly are you attempting to do? KeyValueMap does access the key-value pair without applying List or Normal so if @LeonidShifrin comment is not what you seek then further explanation is needed. $\endgroup$ – Edmund Oct 4 '16 at 1:39
  • 1
    $\begingroup$ f[key_, value_] := key <> ToString[value]; KeyValueMap[f]@<|"a" -> 1, "b" -> 2|> what is not ok here? $\endgroup$ – Kuba Oct 4 '16 at 7:32
  • 2
    $\begingroup$ Maybe you could explain why you need that. p.s. what about <|"a" -> 1, "b" -> 2|> // KeyValueMap[Function[{myKey, myValue}, myKey <> ToString[myValue]]] $\endgroup$ – Kuba Oct 4 '16 at 18:04

Your Answer

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

Browse other questions tagged or ask your own question.