New answers tagged

3

It is a bug that the local variable c isn't shared with Condition when the latter appears at the LHS outside of the KeyValuePattern. The following simplified examples demonstrate the bug: gfail1[KeyValuePattern@{"c" -> c_}] /; c == 0 := 0 gfail2[KeyValuePattern@{"c" -> c_} /; c == 0] := 0 gfail1[<|"c" -> 0|>] gfail2[<|"c" -> 0|>] ...


1

Consider test[p : KeyValuePattern@{"c" -> c_List}] /; First[p["c"]] == 0 := $Succeeded test[p : KeyValuePattern@{"c" -> c_List}] := $Failed then test[<|"c" -> {0, 1}|>] $Succeeded test[<|"c" -> {1, 1}|>] $Failed Perhaps that is form you are looking for.


0

I am a little uncertain about exactly the behaviour that you hope to achieve. However, if Mathematica complains that something is protected you have the option of Unprotecting it. In this case, it is unlikely you are going to break any fundamental functionality (Unprotect elsewhere at your own risk!) Needs["Notation`"] Symbolize[ParsedBoxWrapper[...


0

You could do this: Function[{a, b, c, d, e}, Graphics[{{PointSize@Medium, Point[{a, b, c, d, e}]}, Line[{{a,b}, {a,c}, {a,d}, {a,e}, {b,c}, {b,d}, {c, e}}]}]]@@CirclePoints[5] I used a Function to localize the variables and then applied it to the desired points with @@.


2

The answer is no. The 1st argument of With does not allow destructuring. It only accepts a list of simple assignments. I would rewrite the example you give as Module[{a, b, c, d, e}, {a, b, c, d, e} = CirclePoints[5]; Graphics[ {{PointSize @ Medium, Point[{a, b, c, d, e}]}, Line[{{a, b}, {a, c}, {a, d}, {a, e}, {b, c}, {b, d}, {c, e}}]}]]


3

Fundamentally this is a very common question: Reassign values to symbols How do you programatically load data into symbols? How to pass a symbol name to a function with any of the Hold attributes? MapThread gives different results from ToExpression when trying to assign variables from a list Assigning values to a list of variable names Elegant ...


3

I'd not rely on Module variables after Module's evaluation is done. They have Temporary attribute and it can surprise you later. One way around is to inject Own/DownValues: func[a_] := Module[{primaryOutput, secondaryOutput} , primaryOutput = (Pause@5; 2 a); secondaryOutput := (Pause@2; 0.5 a); Association[ "1" -> primaryOutput, "2" :> ...



Top 50 recent answers are included