Tag Info

Hot answers tagged

29

Between Versions 7 and 8 Hash now gives the hash of a raw sequence of characters when applied to Strings. In past versions the string characters (quotation marks) were included in the calculation of the hash. (Reference) Use "\"" <> string <> "\"" before hashing if you want output to match older versions. \[Dash], \[LongDash] and ...


28

For me the operator forms of Map and Apply will probably provide the most important benefits in terms of code readability. Often I need to apply a sequence of transformations to some data, and I am fond of infix notation for this purpose. For example I find a ~Position~ 0 ~SortBy~ Last more readable than the "conventional" SortBy[Position[a, 0], Last] ...


17

I would have liked to have more experience with the operator forms before this question was asked as I am short on examples, and I'm sure my opinion will evolve over time. Nevertheless I think I have enough familiarity with similar syntax to provide some useful comments. Taliesin Beynon provided some background for this functionality in Chat: Operator ...


12

I find the value of the new operator forms becomes critical when working with datasets. Consider titanic = ExampleData[{"Dataset", "Titanic"}]; titanic[Count[#], "survived"] & /@ {True, False, _Missing} {500, 809, 0} Derive a data set for analyzing the survival of very young passengers. cutoff = 8; youngest = titanic[All, {"age", ...


7

tree = Function[x, Defer @ FullForm @ x, HoldAll]; Now: 2 + 2 // tree Plus[2, 2] I used Defer to allow the output to be evaluated. If you do not prefer this replace it with HoldForm. For some explanation of the mechanics of this code see: Why doesn't "Defer" work with "TableForm"? See also my standard methods for ...


6

Very nice answers. I wanted to add something else. One typical "Mathematica way" of coding involves overloading a function with several definitions, that do different things according to what arguments are passed (I actually abuse this). You can pattern match by head with things like f[x_Integer]:=... and f[x_Real]:=.... I see the Dataset/Query ...


4

In Mathematica version 10, you can also use Inactive to allow the Symbol to be created before doing the assignment. Here is an example: Clear["x"]; Activate[Inactive[Set][Symbol["x"], 3]] (* ==> 3 *) x (* ==> 3 *)


4

Here's a stab at a second pass: Syntactic sugar shouldn't be underestimated given its cumulative effects (also only a limited number of functions can have shortforms and sometimes for precedence reasons four symbols are needed in the pure form - (#)&) An example: Suppose it is desired to take keys/values "f" through to "h" and "p" through to "r" in ...


1

Another alternative: Clear[f, V] V = (a[1] + a[2]) b[1]; f[x_, y_, z_] := V /. Thread[Variables[V] :> {x, y, z}]; f[1, 2, 3] (* 9 *)


1

ClearAll[f,g]; f[a_[1], a_[2], b_[1]] := (a[1] + a[2]) b[1] f[a[1], a[2], b[1]] (* (a[1] + a[2]) b[1] *) f[z[1], z[2], w[1]] (* w[1] (z[1] + z[2]) *) f[z[1], z[2], w[2]] (* f[z[1], z[2], w[2]] --- f undefined for this input pattern *) Or, more generally, g[a_[x___], a_[y___], b_[z___]] := (a[x] + a[y]) b[z] g[a[1], a[3], b[5]] (* (a[1] + a[3]) b[5] *) ...


1

If, in an input cell, you type the expression, x1 ~ f ~ x2 ~ f ~ x3 it will evaluate to f[f[x1,x2],x3] UNLESS the symbol f has the attribute Flat in which case it will evaluate to f[x1,x2,x3]



Only top voted, non community-wiki answers of a minimum length are eligible