Skip to content

Assignment Operators

Simple vs Complex Assignment

Grapa's assignment system is designed for both performance and flexibility:

Simple Variable Assignment

x = 10;           /* Direct variable assignment */
name = "John";    /* String assignment */
counter = 0;      /* Integer assignment */

Complex Assignment (Array/Object Access)

/* Array element assignment */
array[5] = 10;              /* Direct index assignment */
array[-1] = "last";         /* Negative index assignment */

/* Object property assignment */
object.property = "value";  /* Dot notation assignment */
object["property"] = "value"; /* Bracket notation assignment */

/* Nested assignment */
data[0].name = "Alice";     /* Nested object assignment */
config.database.host = "localhost";  /* Deep property assignment */

/* Compound assignment on accessed elements */
array[0] += 5;              /* Add to accessed element */
object.count += 1;          /* Increment accessed property */

Performance Note: Simple variable assignments are optimized for speed, while complex assignments (array indexing, object properties) provide flexibility but may be slightly slower.

System Variable Assignment

Grapa supports special system variables with optimized handling:

@this = value;      /* Current context */
@parent = value;    /* Parent context */
$global = value;    /* Global namespace */
$local = value;     /* Local namespace */
$this = value;      /* Current object */
$parent = value;    /* Parent object */
$root = value;      /* Root object */
$self = value;      /* Self reference */

System Namespace Protection

Grapa uses the $ prefix to protect system namespace:

/* User namespace (recommended) */
x = 5;                    /* User variable */
name = "hello";           /* User string */

/* System namespace (reserved) */
$x = 5;                   /* System variable */
$name = "hello";          /* System string */

Guidelines: - Use user namespace (x, "hello") for your own code - Avoid system namespace ($x, $"hello") unless necessary - Both work but system namespace is protected to prevent accidental overrides

=

Assign.

+=

Add item to source.

Array/List Addition

Arrays ($ARRAY):

arr = [1, 2, 3, 4, 5];
arr += 6;                 /* Add element to end: [1,2,3,4,5,6] */
arr += [7, 8];            /* Add multiple elements: [1,2,3,4,5,6,7,8] */
arr += 10 arr[0];         /* Insert at position: [10,1,2,3,4,5,6,7,8] */
arr += 20 arr[2];         /* Insert at position 2: [10,1,20,2,3,4,5,6,7,8] */

Lists ($LIST):

list = {a:1, b:2, c:3};
list += (d:4);            /* Add single key-value pair: {"a":1,"b":2,"c":3,"d":4} */
list += {d:4, e:5};       /* Add multiple key-value pairs: {"a":1,"b":2,"c":3,"d":4,"e":5} */
list += (f:6) list[0];    /* Insert at beginning: {"f":6,"a":1,"b":2,"c":3,"d":4,"e":5} */
list += (g:7) list[1];    /* Insert at position 1: {"f":6,"g":7,"a":1,"b":2,"c":3,"d":4,"e":5} */

Vectors ($VECTOR):

vec = [1, 2, 3];
vec += 4;                 /* Add element to end */
vec += [5, 6];            /* Add multiple elements */

Widgets ($WIDGET):

widget = {name:"button", type:"click"};
widget += ("label", "Click Me");           /* Add widget with name and value */
widget += ("icon", "star.png", widget[0]); /* Add at specific position */

XML/TAG Elements:

xml = <root><item>1</item></root>;
xml += <newitem>2</newitem>;               /* Add XML element */
xml += <child>3</child> xml[0];            /* Add at specific position */

++=

Concatenate contents to source.

Syntax Variations

The ++= operator supports two syntax variations:

  1. Standard Concatenation: target ++= value
  2. Position-Based Insertion: target ++= value position

Array/List Concatenation

Arrays ($ARRAY):

arr1 = [1, 2, 3];
arr2 = [4, 5, 6];
arr1 ++= arr2;   /* Concatenate arrays: [1,2,3,4,5,6] */
arr1 ++= arr2 arr1[0];  /* Insert at beginning: [4,5,6,1,2,3,4,5,6] */

Lists ($LIST):

list1 = {a:1, b:2};
list2 = {c:3, d:4};
list1 ++= list2; /* Concatenate lists: {"a":1,"b":2,"c":3,"d":4} */
list1 ++= list2 list1[0];  /* Insert at beginning: {"c":3,"d":4,"a":1,"b":2} */

Vectors ($VECTOR):

/* Note: ++= operator for vectors is currently not working correctly */
/* Use alternative approaches for vector concatenation: */

/* For 1D vectors - convert to array, concatenate, convert back */
vec1 = #[1, 2, 3]#;
vec2 = #[4, 5, 6]#;
result = vec1.array() + vec2.array();  /* [1,2,3,4,5,6] */
vec_result = result.vector();          /* #[1,2,3,4,5,6]# */

/* For 2D vectors - use array conversion */
matrix1 = #[[1,2],[3,4]]#;
matrix2 = #[[5,6],[7,8]]#;
result = matrix1.array() + matrix2.array();  /* [[1,2],[3,4],[5,6],[7,8]] */
matrix_result = result.vector();             /* #[[1,2],[3,4],[5,6],[7,8]]# */

Widgets ($WIDGET):

widget1 = {name:"button", type:"click"};
widget2 = {name:"input", type:"text"};
widget1 ++= widget2; /* Extend widget with another widget */
widget1 ++= widget2 widget1[0];  /* Insert at beginning */

Rules:

x = rule $INT {op(a:$1){a}};
x ++= rule $STR {op(a:$1){a}};
x
/* Result: $INT {...}| $STR {...} */

See Rule Composition and Concatenation for detailed examples.

-=

Subtract item from source.

Array/List Removal

Arrays ($ARRAY):

arr = [1, 2, 3, 4, 5];
arr -= arr[2];   /* Remove by index: [1,2,4,5] */
arr -= arr[-1];  /* Remove last element: [1,2,4] */

Lists ($LIST):

list = {a:1, b:2, c:3, d:4};
list -= list.c;          /* Remove by key reference: {"a":1,"b":2,"d":4} */
list -= list["c"];       /* Remove by key string: {"a":1,"b":2,"d":4} */
list -= list[-2];        /* Remove by negative index: {"a":1,"b":2,"d":4} */
list -= list[1];         /* Remove by positive index: {"a":1,"c":3,"d":4} */

Important Notes: - Key-based removal: list -= list.key or list -= list["key"] removes the key-value pair - Index-based removal: list -= list[index] removes the element at that position - Value-based removal: Direct value removal (e.g., list -= 5) is not supported - String key removal: Direct string removal (e.g., list -= "key") is not supported

*=

Multiply and assign.

Numeric Multiplication Assignment

/* Integer multiplication */
x = 10;
x *= 3;                    /* 30 */

/* Float multiplication */
y = 5.5;
y *= 2;                    /* 11.0 */

/* Mixed type multiplication (promotes to float) */
z = 10;
z *= 2.5;                  /* 25.0 */

Type Support: INT, FLOAT
Note: Only numeric types are supported. String and other types will return $ERR.

/=

Divide and assign.

Numeric Division Assignment

/* Integer division (promotes to float) */
x = 10;
x /= 2;                    /* 5.0 */

/* Float division */
y = 10.0;
y /= 3;                    /* 3.333... */

/* Mixed type division */
z = 10;
z /= 2.5;                  /* 4.0 */

Type Support: INT, FLOAT
Note: Division always promotes to float for precision. Only numeric types are supported.

%=

Modulo and assign.

Numeric Modulo Assignment

/* Integer modulo */
x = 10;
x %= 3;                    /* 1 */

/* Float modulo */
y = 10.5;
y %= 3;                    /* 1.5 */

/* Mixed type modulo */
z = 10;
z %= 3.5;                  /* 3.0 */

Type Support: INT, FLOAT
Note: Only numeric types are supported. String and other types will return $ERR.

**=

Power and assign.

Numeric Power Assignment

/* Integer power */
x = 2;
x **= 3;                   /* 8 */

/* Float power */
y = 2.5;
y **= 2;                   /* 6.25 */

/* Mixed type power (promotes to float) */
z = 2;
z **= 3.5;                 /* 11.313... */

Type Support: INT, FLOAT
Note: Only numeric types are supported. String and other types will return $ERR.

Advanced Operations

Search and Filter Operations

Grapa provides several methods for searching and filtering data:

/* Search for specific values in arrays */
arr = [1, 2, 3, 4, 5, 3];
matches = arr.filter(op(x){x == 3;});  /* Find all elements equal to 3: [3, 3] */

/* Search for objects with specific properties */
data = [{id:1, name:"Alice"}, {id:2, name:"Bob"}, {id:3, name:"Alice"}];
alice_users = data.filter(op(item){item.name == "Alice";});  /* Find all users named Alice */

/* Search for objects with multiple criteria */
active_users = data.filter(op(item){item.name == "Alice" && item.id > 1;});

Grep Method (For Strings)

/* Search for patterns in strings */
str = "hello world";
matches = str.grep("o", "o");  /* Find all 'o' characters: ["o", "o"] */
count = str.grep("o", "c")[0].int();  /* Count occurrences: 2 */

Find All Method (For Complex Structures)

/* Find elements in XML structures */
xml = <root><item>1</item><item>2</item><item>1</item></root>;
matches = xml.findall(<item>1</item>);  /* Find all item elements with content "1" */

/* Find elements in nested structures */
nested = {users:[{id:1, name:"Alice"}, {id:2, name:"Bob"}]};
matches = nested.findall({name:"Alice"}); /* Find all objects with name "Alice" */

Note: - filter() is the most reliable method for searching JSON objects and arrays - grep() is designed for string pattern matching - findall() is designed for complex data structures like XML, TAG, and nested lists/arrays

See also