# Tag Info

42

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 ...

34

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] ...

26

Clearly the @ notation is inspired by the usual mathematical notation for function composition. f@g[x] looks very similar to the mathematical notation $(f\circ g)(x)$. But it is important to understand that @ does not denote function composition. In mathematical notation $f\circ g$ is also a function. In Mathematica f@x is simply a different way to ...

25

Good News Everyone! Two-parameter syntax for Fold and FoldList has been (silently) implemented! Taliesin Beynon informs me that this was implemented in 2011, so check your older versions as well. As Naitree notes this is now documented in 10.0.2: Fold[f, a] FoldList[f, a] f[f[f[1, 2], 3], 4] {1, f[1, 2], f[f[1, 2], 3], f[f[f[1, 2], 3], 4]} And ...

22

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 ...

19

I see no mention of the new-in-10 PositionIndex in the other answers, which takes a list (or association) of values and returns a 'reverse lookup' that maps from values in the list to the positions where they occur: In[1]:= index = PositionIndex[{a, b, c, a, c, a}] Out[1]= <|a -> {1, 4, 6}, b -> {2}, c -> {3, 5}|> It doesn't take a level ...

19

Try this: Map[If[#==1,Unevaluated@Sequence[],#]&,{1,2,3}] Note the output. The 1 is gone. That's because Unevaluated@Sequence[] puts the empty sequence there, that is, "nothing". ##&[] is a shorthand that can be used in most places for same - ## is the sequence of arguments, & makes it a function to apply to something, [] is that something - ...

17

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", ...

13

As djp explains parentheses are unnecessary in the FullForm of an expression; it is logical for superfluous information to be removed. However if you want parentheses to persist you could use something like this: $PreRead = # /. RowBox[{"(", body___, ")"}] :> RowBox[{"paren", "[", body, "]"}] &; MakeBoxes[paren[body___], form_] := ... 12 So recently I've learned from John Fultz that RawBoxes are kind of verbatim indicator for MakeBoxes which is not well stressed out in documentation. This or I've missed the point but it doesn't matter, here we have handy way to do this: x = 5; ToExpression @ MakeBoxes[RawBoxes["x"] = 123]; x 123 11 You can have a single rule using Alternatives (|)... {a, b, c, d, e, f, g, h} /. x : (a | c | e | f) -> 12 Furthermore you can construct the rule on the fly... {a, b, c, d, e, f, g, h} /. x : Alternatives@@{a, c, e, f} -> 12 As noted by @Kuba below there is no requirement for the pattern to have a name (x) so... {a, b, c, d, e, f, g, h} /. (a | ... 10 A lot of functions in MMA have default values for Optional Arguments, for Example Flatten. It can take Flatten[expr] which means Flatten[expr, Infinity] Some functions don't have such option and you need to feed the Optional Arguments but you can go around by building your own function for your example, you can do this kind of trick like this: f[expr_, ... 10 The behaviour we see is due to the precedence of &, which is much lower than the precedence of /@. As a consequence, the expression Line /@ (Print[#]; #) is bound tightly together by the high precedence /@ infix operator, yielding the single argument to the low precedence & postfix operator. This means that the second expression is interpreted as ... 10 This behaviour is very common, possibly near universal, in programming languages with matchfix comment syntax. The reason is that the contents of a comment sequence is presumed not to be code. Usually that presumption is correct, but not in this case. The general rule is You should be able to put anything inside a comment and the only special tokens ... 10 We already have some answers explaining the issue. Let me give a solution to your problem. Let's say you have the code RegularExpression["((re)*)"] that you want to comment out. Since we have nested comments in Mathematica, just use a pair of (* to prevent your issue: Although I don't know the internal implementation of Mathematicas parser, the ... 9 ... why introduce Composition as a new feature? Composition is used to create a new anonymous function that can be used in all the standard ways such as Map and Apply etc. To achieve the same thing without it one needs a Function. Much like operator forms the use of Composition allows one to eliminate extraneous Function constructs which can make code ... 8 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 *) 8 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 ... 8 Also PlotRange[plot] PlotRange /. AbsoluteOptions[plot] Last @@ AbsoluteOptions[plot, PlotRange] PlotRange /. plot[[2]] all give (* {{0.,10.},{-0.999999,1.}} *) Note: Regarding usage of PlotRange as a function, it is undocumented, and the earliest reference I could find on this site is this answer dated Oct 11, 2012: The same range on each plot in a ... 8 You can make them an expression if you want. Let par[x.....] represent (x....). For example: (x+y)*z The FullForm would be: Times[par[Plus[x,y]], z] But in every such expression, the par[..] would only ever have on argument (in the example, Plus[x,y]). It would never modify the meaning of the argument. So in FullForm, there would be no point having ... 8 The documentation you linked to is more about how Infix is used to define output formats. You can find more information on how to use functions in infix form in the tutorials Special Ways to Input Expressions and Operator Input Forms. If you really want to use Partition in the infix form with an offset, you can use samplelist ~Partition~ Sequence[3, 2] ... 7 It comes down to the DRY principle: The DRY principle is stated as "Every piece of knowledge must have a single, unambiguous, authoritative representation within a system." The content management system Wordpress doesn't use object oriented paradigms and so for that reason it looks exactly like your code. Tens of thousands of lines of code like this. ... 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 ... 7 Differences /@ B or, if you don't like runes, Map[Differences, B] In v10, there is an operator form of Map which can be very useful, Map[Differences] @ B which allows you to slip a Map operation into a sequence of transformations without worrying about precedence issues. 7 As already explained, this happens because MatrixForm act as a wrapper. The answer to the question about how this behavior is implemented and how can eventually be reproduced is contained in the Informmation of the system symbol$OutputForms. Indeed ?? $OutputForms returns:$OutputForms is a list of the formatting functions that get stripped off ...

7

I believe it is important to get a fundamental understanding of what Pure Functions are that goes beyond the understanding using of a syntax. Hereafter an non-exhaustif summary of a few key understandings: 1) Pure Functions have they roots in Lambda calculus that forms the basis of functional programming paradigm implemented in Mathematica. 2) In ...

6

Algohi's answer is the most appropriate if one of the function's arguments is primary and the others secondary. However, you can get closer to the syntax that you suggested in your question using {x^2 + 9 x + 5, 3} // Apply @ PolynomialMod which works in version 10 using the operator form of Apply. Or, to include earlier versions, you could define your ...

6

Here's another way to achieve the same thing: Differences[{##}] & @@@ B {{1, 1}, {}, {1, 1, 1}} OR for fun Replace[B, x : {__} :> Differences[x], 1]

6

You should use Sequence PolynomialMod[x^2 + 9 x + 5, 3] PolynomialMod@Sequence[x^2 + 9 x + 5, 3] Sequence[x^2 + 9 x + 5, 3] // PolynomialMod 2 + x^2 all produce the same output. Works with any function Sequence[i, {i, 10}] // Table {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}

6

If you cannot resolve the keyboard behavior try an alternate input form; I propose: EscdinttEsc To input: Then use Tab to move between the Placeholder fields.

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