Message Boards Message Boards

0
|
1586 Views
|
3 Replies
|
4 Total Likes
View groups...
Share
Share this post:

KeyValueMap with optional argument

Posted 11 months ago

Hello

I have a association list and use the KeyValueMap function

list = <|"Dog" -> "Bard", "Cat" -> "Lisa"|>;
KeyValueMap[f, list]

which gives me

{f["Dog", "Bard"], f["Cat", "Lisa"]}

I'm trying to figure out how to apply a third argument to f, when calling it from KeyValueMap

f[key_, value_, third_] := 
  Module[{}, Print[key, " name is ", value, " (", third, ")"]];

but all my attempts has failed. The third argument is detached from the list, and is picked up somewhere else. It has the same value for the entire list.

Could some kind person in here, point me in the right direction?

Thanks in advance

POSTED BY: Jak SD
3 Replies
Posted 11 months ago

Thank you very much, Eric and Henrik - just what I hoped for. Much appreciated. Have a great day/evening!

POSTED BY: Jak SD
Posted 11 months ago

You can create an anonymous function:

KeyValueMap[f[##, "THIRD VALUE"] &, list]
POSTED BY: Eric Rimbey

Here is one simple way:

f[third_][key_, value_] := Print[key, " name is ", value, " (", third, ")"]

Then, e.g.:

enter image description here

POSTED BY: Henrik Schachner
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract