Math operators apply to many classes, including arrays and other collections.

Using a basic math operator on a Symbol swallows the operation (returns the symbol)

\symbol * 5

symbol

number + numberaddition

number - numbersubtraction

number * numbermultiplication

number / numberdivision

number % numbermodulo

number ** numberexponentiation

Bitwise arithmetic

number & numberbitwise and

number | numberbitwise or

number << numberbitwise left shift

number >> numberbitwise right shift

number +>> numberunsigned bitwise right shift

Logical operators

object == objectequivalence

object === objectidentity

object != objectnot equal to

object !== objectnot identical to

Objects may be equivalent but not identical.

[1, 2, 3] == [1, 2, 3]

true

[1, 2, 3] === [1, 2, 3]

false// a and b are two different array instances with the same contents

a = b = [1, 2, 3];

a === b;

true// a and b are the same array instance

number < numbercomparison (less than)

number <= numbercomparison (less than or equal to)

number > numbercomparison (greater than)

number >= numbercomparison (greater than or equal to)

Boolean && Booleanlogical And

Boolean || Booleanlogical Or

When a function is the second operand, these operators perform short-circuiting (i.e., the function is executed only when its result would influence the result of the operation). This is recommended for speed.

With and: and or: second-argument functions will be inlined. If you use && or ||, no inlining will be done and performance will be slower.

a = 1;

a == 1 and: { "second condition".postln; [true, false].choose }

second condition

true

a == 1 or: { "second condition".postln; [true, false].choose }

true

a != 1 and: { "second condition".postln; [true, false].choose }

false

a != 1 or: { "second condition".postln; [true, false].choose }

second condition

true

In this case, the second condition will cause an error if a is nil, because nil does not understand addition. a.notNil is a safeguard to ensure the second condition makes sense.

stream << objectrepresent the object as a string and add to the stream

A common usage is with the Post class, to write output to the post window.

Post << "Here is a random number: " << 20.rand << ".\n";

Here is a random number: 13.

stream <<* collectionadd each item of the collection to the stream

Post << [0, 1, 2, 3]

[ 0, 1, 2, 3 ]

Post <<* [0, 1, 2, 3]

0, 1, 2, 3

stream <<< objectadd the object's compile string to the stream

Post <<< "a string"

"a string"

stream <<<* collectionadd each item's compile string to the stream

Conditional execution operators

object ? objectnil check (no .value)

object ?? functionnil check (.value, function is inlined)

If the object is nil, the second expression's value will be used; otherwise, it will be the first object.

a = [nil, 5];

10.do({ (a.choose ? 20.rand).postln });

10.do({ (a.choose ?? { 20.rand }).postln });

?? { } is generally recommended. ? always evaluates the second expression, even if its value will not be used. ?? evaluates the function conditionally (only when needed). If the function defines no variables, the function will be inlined for speed.

Especially useful when the absence of an object requires a new object to be created. In this example, it's critical that a new SCSlider not be created if the object was already passed in.

f = { |slider, parent|

slider = slider ?? { SCSlider.new(parent, Rect(0, 0, 100, 20)) };

slider.value_(0);

};

If the first line inside the function instead read slider = slider ? SCSlider.new(parent, Rect(0, 0, 100, 20));, a new slider would be created even if it is not needed, or used.

object !? functionexecute function if object is not nil

a = [10, nil].choose;

a !? { "ran func".postln };

// equivalent of:

if (a.notNil) { "ran func".postln };

Used when an operation requires a variable not to be empty.

f = { |a| a + 5 };

f.value

// error: nil does not understand +

f = { |a| a !? { a+5 } };

f.value

nil// no error

f.value(2)

7

Miscellaneous operators

object ! numberobject.dup(number)

15 ! 5

[ 15, 15, 15, 15, 15 ]

If the object is a function, it behaves like Array.fill(number, function).

{ 10.rand } ! 5

[ 8, 9, 3, 8, 0 ]

object -> objectcreates an Association, used in dictionaries

expression <! expressionbypass value of second expression

This operator evaluates both expressions, and returns the value of the first.

a = 0;

0

// a is incremented twice, but the return value (1)

// comes from the first increment (0 + 1)

(a = a + 1) <! (a = a + 1)

1

a// a's value reflects both increments

2

function <> functionfunction composition operator

This operator returns a new function, which evaluates the second function and passes the result to the first function.

f = { |a| a * 5 } <> {|a| a + 2 };

f.(10);

60// == (10+2) * 5

An array as argument is passed through the chain:

f.([10, 75, 512]);

[ 60, 385, 2570 ]// == ([10, 75, 512]+2) * 5

Symbolic notations to define literals/other objects

$character prefix: "ABC".at(0) == $A

'' or \define a literal Symbol: 'abc' === \abc

""define a literal String

[item, item...]define an Array containing given items

Set[item, item...]define a Set -- any Collection class name can be used other than Set