3 added 950 characters in body

## Alternatives to Length

An alternativeThis has been entirely rewritten with some suggestions from LegionMammal978 and Misha Lavrov. Many thanks to both of them.

In many cases, Length can be shortened a bit by making use of Tr. The basic idea is to turn the input into a list of Length@x1 on one-dimensional listss, so that Tr sums them up which will then equal the length of the list.

The most common way to do this is to use 1^x (and in some cases, higher-dimensional lists as wellfor a list x). This works because Power is Listable and 1^n for most atomic values n is just 1 (including all numbers, strings and symbols). So we can already save one byte with this:

Tr[0x+1]Length@x
Tr[1^x]

This convertsOf course, this assumes that x into a listis an expression with higher precedence than ^.

If x contains only 0s of the same length, addsand 1 to each zero and gets the sum.s, we can save another byte using Factorial (assuming Trx on 1D lists is effectively identical tohas higher precedence than Total!.):

Length@x
Tr[x!]

NowIn some rare cases, thesex might have the same byte count,lower precedence than ^ but juxtaposedstill higher precedence than multiplication has a. In that case it will also have lower precedence than prefix function application@, so we really need to compare with Length[x]. ThatAn example of such an operator is, in some .. In those cases, you can't use the former because of precedence, but you can still use the latter.save a byte with this form:

Length[x.y]
Tr[0x.y+1]

Finally, some remarks about what kind of lists this works on:

As an examplementioned at the top, take infix function applicationthis works on flat lists containing only numbers, like m~SomeFunc~nstrings and symbols. You can't doHowever, it will also work on some deeper lists, although it actually computes something slightly different. For an Length@m~SomeFunc~nn-D rectangular array, because that appliesusing LengthTr gives you the shortest dimension (as opposed to the first). If you know that the outermost dimension is the shortest, or you know they're all the same, than the mTr before calling-expressions are still equivalent to SomeFuncLength. Therefore, you'd have to write

Length[m~SomeFunc~n]

which is now one byte longer than

Tr[0m~SomeFunc~n+1]

An alternative to Length@x on one-dimensional lists (and in some cases, higher-dimensional lists as well) is

Tr[0x+1]

This converts x into a list 0s of the same length, adds 1 to each zero and gets the sum. (Tr on 1D lists is effectively identical to Total.)

Now, these have the same byte count, but juxtaposed multiplication has a lower precedence than prefix function application. That is, in some cases, you can't use the former because of precedence, but you can still use the latter.

As an example, take infix function application, like m~SomeFunc~n. You can't do Length@m~SomeFunc~n, because that applies Length to m before calling SomeFunc. Therefore, you'd have to write

Length[m~SomeFunc~n]

which is now one byte longer than

Tr[0m~SomeFunc~n+1]

## Alternatives to Length

This has been entirely rewritten with some suggestions from LegionMammal978 and Misha Lavrov. Many thanks to both of them.

In many cases, Length can be shortened a bit by making use of Tr. The basic idea is to turn the input into a list of 1s, so that Tr sums them up which will then equal the length of the list.

The most common way to do this is to use 1^x (for a list x). This works because Power is Listable and 1^n for most atomic values n is just 1 (including all numbers, strings and symbols). So we can already save one byte with this:

Length@x
Tr[1^x]

Of course, this assumes that x is an expression with higher precedence than ^.

If x contains only 0s and 1s, we can save another byte using Factorial (assuming x has higher precedence than !):

Length@x
Tr[x!]

In some rare cases, x might have lower precedence than ^ but still higher precedence than multiplication. In that case it will also have lower precedence than @, so we really need to compare with Length[x]. An example of such an operator is .. In those cases, you can still save a byte with this form:

Length[x.y]
Tr[0x.y+1]

Finally, some remarks about what kind of lists this works on:

As mentioned at the top, this works on flat lists containing only numbers, strings and symbols. However, it will also work on some deeper lists, although it actually computes something slightly different. For an n-D rectangular array, using Tr gives you the shortest dimension (as opposed to the first). If you know that the outermost dimension is the shortest, or you know they're all the same, than the Tr-expressions are still equivalent to Length.

Mod Removes Wiki by Doorknob
2 added 54 characters in body

An alternative to Length@x on one-dimensional lists (and in some cases, higher-dimensional lists as well) is

Tr[0x+1]

This converts x into a list 0s of the same length, adds 1 to each zero and gets the sum. (Tr on 1D lists is effectively identical to Total.)

Now, these have the same byte count, but juxtaposed multiplication has a lower precedence than prefix function application. That is, in some cases, you can't use the former because of precedence, but you can still use the latter.

As an example, take infix function application, like m~SomeFunc~n. You can't do Length@m~SomeFunc~n, because that applies Length to m before calling SomeFunc. Therefore, you'd have to write

Length[m~SomeFunc~n]

which is now one byte longer than

Tr[0m~SomeFunc~n+1]

An alternative to Length@x on one-dimensional lists is

Tr[0x+1]

This converts x into a list 0s of the same length, adds 1 to each zero and gets the sum. (Tr on 1D lists is effectively identical to Total.)

Now, these have the same byte count, but juxtaposed multiplication has a lower precedence than prefix function application. That is, in some cases, you can't use the former because of precedence, but you can still use the latter.

As an example, take infix function application, like m~SomeFunc~n. You can't do Length@m~SomeFunc~n, because that applies Length to m before calling SomeFunc. Therefore, you'd have to write

Length[m~SomeFunc~n]

which is now one byte longer than

Tr[0m~SomeFunc~n+1]

An alternative to Length@x on one-dimensional lists (and in some cases, higher-dimensional lists as well) is

Tr[0x+1]

This converts x into a list 0s of the same length, adds 1 to each zero and gets the sum. (Tr on 1D lists is effectively identical to Total.)

Now, these have the same byte count, but juxtaposed multiplication has a lower precedence than prefix function application. That is, in some cases, you can't use the former because of precedence, but you can still use the latter.

As an example, take infix function application, like m~SomeFunc~n. You can't do Length@m~SomeFunc~n, because that applies Length to m before calling SomeFunc. Therefore, you'd have to write

Length[m~SomeFunc~n]

which is now one byte longer than

Tr[0m~SomeFunc~n+1]
1

An alternative to Length@x on one-dimensional lists is

Tr[0x+1]

This converts x into a list 0s of the same length, adds 1 to each zero and gets the sum. (Tr on 1D lists is effectively identical to Total.)

Now, these have the same byte count, but juxtaposed multiplication has a lower precedence than prefix function application. That is, in some cases, you can't use the former because of precedence, but you can still use the latter.

As an example, take infix function application, like m~SomeFunc~n. You can't do Length@m~SomeFunc~n, because that applies Length to m before calling SomeFunc. Therefore, you'd have to write

Length[m~SomeFunc~n]

which is now one byte longer than

Tr[0m~SomeFunc~n+1]