Group Abstract Group Abstract

Message Boards Message Boards

Wolfram Language vs. Python - which one calls itself better? Part 1

Posted 1 year ago

Attachments:
POSTED BY: Thomas Adler
4 Replies
POSTED BY: Marcel Wild
Posted 11 months ago

Hi Darren,

Thanks for pointing that out, that is a mistake from my part. I've rectified it my original post.

POSTED BY: Thomas Adler

Hi Thomas, This is not correct:

In Mathematica, on the other hand, arguments are passed as values, meaning that the arguments are copied into the function’s local environment. This means that any modifications made to the arguments inside the function do not affect the original variables.

...associations are immutable data types, so we cannot modify them in-place anyways..

ClearAll[fChangeAssoc];
SetAttributes[fChangeAssoc, HoldFirst];
fChangeAssoc[assoc_, key_, newVal_] := AppendTo[assoc, key -> newVal];

assoc = <| "this" -> 1, "that" -> 2 |>

fChangeAssoc[assoc, "this", 3]

assoc

<| "that" -> 2, "this" -> 3 |>

Note that the first argument is NOT restricted as _Association when using the HoldFirst:

ClearAll[fChangeAssocWRONG];
SetAttributes[fChangeAssocWRONG, HoldFirst];
fChangeAssocWRONG[assoc_Association, key_, newVal_] := AppendTo[assoc, key -> newVal];

The following is not matched:

fChangeAssocWRONG[assoc]

fChangeAssocWRONG[assoc]
POSTED BY: Darren Kelly

enter image description here -- you have earned Featured Contributor Badge enter image description here Your exceptional post has been selected for our editorial column Staff Picks http://wolfr.am/StaffPicks and Your Profile is now distinguished by a Featured Contributor Badge and is displayed on the Featured Contributor Board. Thank you!

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