Skip to content

Commit 828d37d

Browse files
authored
Jango/minor improvements (#16)
* Fix typos * Chained let declarations * List multiplication * Infinite loop * rand function * Know to known * None for invalid indexes
1 parent f29522b commit 828d37d

File tree

1 file changed

+62
-11
lines changed

1 file changed

+62
-11
lines changed

readme.md

Lines changed: 62 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,11 @@ let b = 'world';
3838
let c = "100";
3939
```
4040

41+
Chaining declarations is also valid:
42+
```
43+
let a = "hello", b = 'world', c = "100";
44+
```
45+
4146
Strings may not have mismatching quotes.
4247
As such, the following is **invalid** syntax:
4348
```
@@ -162,7 +167,7 @@ let a = 1;
162167
# [ 0, 1, 2]
163168
let x = [ 0, :a, 2]
164169
165-
# Consequently, this is also possible
170+
# This is also possible, although not very useful
166171
# [ 3 ]
167172
let y = [ :3 ]
168173
```
@@ -189,18 +194,37 @@ print ( x[a] );
189194
print ( x['a'] );
190195
```
191196

192-
If you try to access data that is beyond the length of a list, it is a runtime error as opposed to returning `none`
197+
If you try to access a key that isn't in the list, it returns `none`
193198

194-
Note that this makes the following code **Invalid**:
199+
For example:
195200
```
196-
let a = 10;
197-
let x = [ 1, a: 2, 3 ];
201+
let x = [ 1, 2, 3 ];
198202
199-
if ( x[a] == none ) {
200-
# because the 10th index of the list does not exist this will error on runtime
203+
if ( x[10] == none ) {
204+
// 10 is way outside the list! So this executes
205+
print("Lists much be shorter than 10!");
201206
}
202207
```
203208

209+
By extention, setting a key to `none` deletes it:
210+
```
211+
let x = [ 1, 2, 3 ];
212+
213+
# 2
214+
print ( x[1] );
215+
216+
x[1] = none:
217+
218+
# 3
219+
print ( x[1] );
220+
```
221+
222+
However, this does mean that having `none` as keys is ignored:
223+
```
224+
# Prints 4, but because print returns none, x is set to [4]
225+
let x = [ 4, print(4) ];
226+
```
227+
204228
Lists can be appended to:
205229
```
206230
let x = [ 1, 2, 3 ];
@@ -229,6 +253,14 @@ x += [[ 4, 5, 6 ]];
229253
# x is now [ 1, 2, 3, [ 4, 5, 6 ] ]
230254
```
231255

256+
Lists can be multiplied:
257+
```
258+
let x = [ 1 ] * 3;
259+
x += [ 2, 3 ] * 2;
260+
261+
# x is now [ 1, 1, 1, 2, 3, 2, 3 ]
262+
```
263+
232264
### Files and Binary
233265

234266
File IO is a core feature of Sack, and as such it has it's own syntax.
@@ -406,12 +438,12 @@ functi add_numbers ( a, b, c ) {
406438
407439
}
408440
409-
// Is this add_numbers with 2 or 3 args? It's both!
441+
# Is this add_numbers with 2 or 3 args? It's both!
410442
let adder = add_numbers;
411443
412-
// Calls the 2 argument version
444+
# Calls the 2 argument version
413445
adder ( 47, 17 );
414-
// Calls the 3 argument version
446+
# Calls the 3 argument version
415447
adder ( 21, 5, 1940 );
416448
```
417449

@@ -509,7 +541,7 @@ if a > 1 {
509541
}
510542
```
511543

512-
Else if is valid because it's a combination of the else and the if operator, it is not sytatically unique.
544+
Else if is valid because it's a combination of the else and the if operator, it is not syntactically unique.
513545

514546
### Print
515547
Print is a language defined output function. It takes in a single variable and returns the output to the terminal appending a newline
@@ -599,6 +631,13 @@ loop ( while a < 100 ) {
599631
}
600632
```
601633

634+
There is a quicker way to write a infinite loop:
635+
```
636+
loop {
637+
print("This will print forever!");
638+
}
639+
```
640+
602641
The keywords `break` and `continue` will exit the loop, or continue to the next iteration respectively.
603642

604643
```
@@ -654,6 +693,11 @@ let x = 3;
654693
# prints `Number`
655694
print ( type ( x ) );
656695
696+
x = 5.25;
697+
698+
# prints `Decimal`
699+
print ( type ( x ) );
700+
657701
x = string ( x );
658702
659703
# prints `String`
@@ -691,6 +735,13 @@ Args will also return a list of arguments passed to the program if called outsid
691735
print ( args() );
692736
```
693737
If the function that is called has no arguments, `args()` will return `none`, however if args is called in the global scope it's length will always be greater than 0.
738+
The `rand()` function can be used to get a random number between 0 and 1 (0 included), for example:
739+
```
740+
let e = rand();
741+
# Nobody knows exactly what e is a now
742+
# But it's known that type(e) == "Decimal"
743+
# And that 0 <= e && e < 1
744+
``
694745
695746
### Importing functions
696747
By using the `import` keyword you can use functions from other sack programs.

0 commit comments

Comments
 (0)