You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: manual/src/features/augmented-assignment.md
+5-7Lines changed: 5 additions & 7 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,8 +1,8 @@
1
1
# Augmented assignment
2
2
3
-
In Andy C++ you can augment assignment operators to quickly perform operations on existing variables like you might expect from any language.
3
+
Andy C++ supports augmented assignment for operations on existing variables.
4
4
5
-
In this example we're augmenting the assignment operator with another operator to quickly increment a number.
5
+
This example increments a number with augmented assignment.
6
6
7
7
```
8
8
let my_number = 3;
@@ -13,15 +13,13 @@ assert_eq(my_number, 8);
13
13
14
14
## Optimization
15
15
16
-
You might expect `list ++= [1,2,3]` desugar to something like `list = list ++ [1,2,3]` but this would be needlessly inefficient. This is why
17
-
languages like Andy C++ have special handling for some of these assignment operators. For instance the earlier example simply appends `[1,2,3]` to
18
-
list without creating an intermediary list first.
16
+
You might expect `list ++= [1,2,3]` to desugar to `list = list ++ [1,2,3]`, but that would waste work. Andy C++ handles some augmented assignments directly. In this case, it appends `[1,2,3]` without creating an intermediate list.
19
17
20
18
## Flexibility
21
19
22
20
Note: I stole this feature from [Noulith](https://github.com/betaveros/noulith).
23
21
24
-
Augmented assignment is not limited to builtin operators, you can also use built in function or user created functions to augment assignment. Consider the following example:
22
+
Augmented assignment also works with built-in functions and user-defined functions. For example:
25
23
26
24
```ndc
27
25
let x = 3;
@@ -30,7 +28,7 @@ x f= 5; // similar to: x = f(x, 5);
30
28
assert_eq(x, 8);
31
29
```
32
30
33
-
A common situation where you might want to use is when finding the highest or lowest value in an iteration:
31
+
One common use case is tracking the highest or lowest value in a loop:
Copy file name to clipboardExpand all lines: manual/src/features/memoization.md
+5-10Lines changed: 5 additions & 10 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,12 +1,10 @@
1
1
# Memoization
2
2
3
-
Andy C++ natively supports memoization through the `pure` keyword. Declaring a function as `pure` indicates that
4
-
it has no side effects and always produces the same output for the same inputs. This allows the language to
5
-
automatically cache and reuse results, improving performance for repeated calls with the same arguments.
3
+
Andy C++ supports memoization through the `pure` keyword. Mark a function as `pure` when it has no side effects and returns the same output for the same inputs. Andy C++ can then cache and reuse previous results.
6
4
7
5
### Syntax
8
6
9
-
You can mark both named and anonymous functions as `pure`:
7
+
Mark both named and anonymous functions as `pure`:
10
8
11
9
```ndc
12
10
pure fn add(x, y) {
@@ -16,18 +14,15 @@ pure fn add(x, y) {
16
14
let multiply = pure fn (x, y) { x * y };
17
15
```
18
16
19
-
> **Note:** the interpreter does not perform any checks to see if the function is actually pure. It's your
20
-
> responsibility to ensure that functions don't have side-effects.
17
+
> **Note:** The interpreter does not check whether a function is actually pure. You must avoid side effects yourself.
21
18
22
19
### Performance: keep memoization keys small
23
20
24
21
The cache key is computed by hashing **all arguments**. For container types like maps and lists this
25
22
is an O(n) operation proportional to the number of elements. Passing large containers as arguments
26
-
to a `pure fn` therefore adds hashing overhead on every call — even on cache hits.
23
+
to a `pure fn` therefore adds hashing overhead on every call, even on cache hits.
27
24
28
-
If a container is large and doesn't change between recursive calls (e.g. a lookup table or graph),
29
-
capture it as an **upvalue** instead of passing it as an argument. The upvalue is not part of the
30
-
cache key, so the memoization cost is proportional only to the arguments that actually vary.
25
+
If a container is large and does not change between recursive calls, such as a lookup table or graph, capture it as an **upvalue** instead of passing it as an argument. The upvalue is not part of the cache key, so memoization cost depends only on the arguments that change.
Control flow in Andy C++ uses expressions and keywords instead of punctuation-heavy syntax. This section covers branching, loops, and the lazy logical operators that often appear in conditions.
0 commit comments