Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
Code-golf is a competition to solve a particular problem in the fewest bytes of source code.
6
votes
Tips for golfing in Mathematica
Mathematica 10 only
Operator forms
Mathematica 10 supports so-called "operator forms", which basically means some functions can be curried. …
11
votes
Tips for golfing in Mathematica
It's important that Mathematica keeps u[v,w] as it is. However, this works in most cases, including if u is a is a number, a string or a list. … use
{y,z}[[x]]
or
{u,v,w}[[x]]
In some rare cases, you can even make use of the fact that multiplication is not evaluated for some values:
{"abc","def"}[[x]]
("abc""def")[[x]]
Note though that Mathematica …
14
votes
Tips for golfing in Mathematica
Operators as Functions
Inspired by Dennis's recent discovery for Julia I thought I'd look into this for Mathematica. … I was aware that Mathematica defines a large number of unused operators, but never paid much attention to it. …
5
votes
Tips for golfing in Mathematica
Mathematica 10.2: BlockMap is Partition+Map
This tip could also be titled, "Read the release notes, all of them". …
25
votes
Tips for golfing in Mathematica
The reason this works is that , introduces two arguments to the list, but omitted arguments (anywhere in Mathematica) are implicit Nulls. …
5
votes
Tips for golfing in Mathematica
When you can make use of the REPL features, don't do this:
a=someLongExpression;some[other*a,expression@a,using^a]
Instead, remember that Mathematica stores the last evaluated (newline-terminated) expression …
22
votes
Tips for golfing in Mathematica
Accessing named arguments (new in V10)
One of the major new language features in Mathematica 10 is Associations, which are basically key-value maps with arbitrary key types, written like
<| x -> 1, "abc … In fact, the shortest Mathematica quine (I know of) is
ToString[#0][] & []
What's slightly annoying is that it won't give you the exact characters you entered. …
4
votes
Tips for golfing in Mathematica
You can stick an expression in Break which can save one or two characters. Example (other details not golfed for clarity):
result = False;
Break[]
can be turned into
Break[result = False]
to sav …
4
votes
Tips for golfing in Mathematica
Here is a list with loads of operator input forms which can shorten a lot of things. Some of these have been mentioned in other posts, but the list is long and I'm always surprised to find a few new t …
2
votes
Tips for golfing in Mathematica
You can save a byte by working around Prepend or PrependTo:
l~Prepend~x
{x}~Join~l
{x,##}&@@l
or
l~PrependTo~x
l={x}~Join~l
l={x,##}&@@l
Unfortunately, this doesn't help for the more common Appe …
5
votes
Tips for golfing in Mathematica
Checking if a list is sorted
This is essentially a corollary of this tip but this is a sufficiently common task that I think it warrants its own answer.
The naive way to check if a list is in order …
4
votes
Tips for golfing in Mathematica
To remove all whitespace from a string s, use
StringSplit@s<>""
That is, use StringSplit's default (split into non-whitespace components) and simply join them back together. The same is probably st …
4
votes
Tips for golfing in Mathematica
Alternatives to Range
A very common task is to apply some sort of function to all numbers from 1 to a n (usually given as input). There are essentially 3 ways to do this (using an unnamed identity fu …
7
votes
Tips for golfing in Mathematica
Don't write 0-argument functions
There is no need for code like this:
f[]:=DoSomething[1,2]
(*...*)
f[]
(*...*)
f[]
You can simply use a variable with := to force re-evaluation of the right-hand s …
6
votes
Tips for golfing in Mathematica
If you need a list of numbers sorted in reverse, don't use
Reverse@Sort@x
but
-Sort@-x
to save six bytes. Sorting by a negative value is also useful for SortBy scenarios:
Reverse@SortBy[x,Last] …