Switch out l() syntax to [] in docs (#851)

* Switch out l() syntax to [] in docs

* Using map syntactic sugar as well.

Co-authored-by: gnembon <41132274+gnembon@users.noreply.github.com>
This commit is contained in:
replaceitemandgnembon authored and GitHub committed 2021-05-10 22:40:49 -03:00
1 parent 451b35b6c5
commit a343c5d9f2
10 files changed
+59 -60

No files matched your search

+5 -5
View File
@@ -202,7 +202,7 @@ It returns a new sorted list, not affecting the list passed to the argument
<pre>sort(3,2,1) => [1, 2, 3]
sort('a',3,11,1) => [1, 3, 11, 'a']
list = l(4,3,2,1); sort(list) => [1, 2, 3, 4]
list = [4,3,2,1]; sort(list) => [1, 2, 3, 4]
</pre>
### `sort_key(list, key_expr)`
@@ -212,15 +212,15 @@ Sorts a copy of the list in the order or keys as defined by the `key_expr` for e
<pre>
sort_key([1,3,2],_) => [1, 2, 3]
sort_key([1,3,2],-_) => [3, 2, 1]
sort_key(l(range(10)),rand(1)) => [1, 0, 9, 6, 8, 2, 4, 5, 7, 3]
sort_key(l(range(20)),str(_)) => [0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 3, 4, 5, 6, 7, 8, 9]
sort_key([range(10)],rand(1)) => [1, 0, 9, 6, 8, 2, 4, 5, 7, 3]
sort_key([range(20)],str(_)) => [0, 1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 2, 3, 4, 5, 6, 7, 8, 9]
</pre>
### `range(to), range(from, to), range(from, to, step)`
Creates a range of numbers from `from`, no greater/larger than `to`. The `step` parameter dictates not only the
increment size, but also direction (can be negative). The returned value is not a proper list, just the iterator
but if for whatever reason you need a proper list with all items evaluated, use `l(range(to))`.
but if for whatever reason you need a proper list with all items evaluated, use `[range(to)]`.
Primarily to be used in higher order functions
<pre>
@@ -247,7 +247,7 @@ arguments is a list of lists, they have to have two elements each, and then firs
In map creation context (directly inside `{}` or `m{}` call), `->` operator acts like a pair constructor for simpler
syntax providing key value pairs, so the invocation to `{foo -> bar, baz -> quux}` is equivalent to
`{l(foo, bar), l(baz, quux)}`, which is equivalent to somewhat older, but more traditional functional form of
`{[foo, bar], [baz, quux]}`, which is equivalent to somewhat older, but more traditional functional form of
`m(l(foo, bar),l(baz, quuz))`.
Internally, `{?}`(list syntax) and `m(?)`(function syntax) are equivalent. `{}` is simply translated to
@@ -47,7 +47,7 @@ and with `player` scope, each player hosts its own state for each app, so functi
<pre>
/script run a() -> global_list+=1; global_list = l(1,2,3); a(); a(); global_list // => [1, 2, 3, 1, 1]
/script run a() -> global_list+=1; global_list = [1,2,3]; a(); a(); global_list // => [1, 2, 3, 1, 1]
/script run a(); a(); global_list // => [1, 2, 3, 1, 1, 1, 1]
</pre>
@@ -76,7 +76,7 @@ a unique name, which you can pass somewhere else to get this function `call`ed.
by their value and `call` method.
<pre>
a(lst) -> lst+=1; list = l(1,2,3); a(list); a(list); list // => [1,2,3]
a(lst) -> lst+=1; list = [1,2,3]; a(list); a(list); list // => [1,2,3]
</pre>
In case the inner function wants to operate and modify larger objects, lists from the outer scope, but not global,
@@ -102,14 +102,14 @@ which variables you want to use, and borrow
This mechanism can be used to use static mutable objects without the need of using `global_...` variables
<pre>
list = l(1,2,3); a(outer(list)) -> list+=1; a(); a(); list // => [1,2,3,1,1]
list = [1,2,3]; a(outer(list)) -> list+=1; a(); a(); list // => [1,2,3,1,1]
</pre>
The return value of a function is the value of the last expression. This as the same effect as using outer or
global lists, but is more expensive
<pre>
a(lst) -> lst+=1; list = l(1,2,3); list=a(list); list=a(list); list // => [1,2,3,1,1]
a(lst) -> lst+=1; list = [1,2,3]; list=a(list); list=a(list); list // => [1,2,3,1,1]
</pre>
Ability to combine more statements into one expression, with functions, passing parameters, and global and outer
@@ -202,11 +202,11 @@ programmers with their own lambda arguments
<pre>
my_map(list, function) -> map(list, call(function, _));
my_map(l(1,2,3), _(x) -> x*x); // => [1,4,9]
profile_expr(my_map(l(1,2,3), _(x) -> x*x)); // => ~32000
sq(x) -> x*x; profile_expr(my_map(l(1,2,3), 'sq')); // => ~36000
sq = (_(x) -> x*x); profile_expr(my_map(l(1,2,3), sq)); // => ~36000
profile_expr(map(l(1,2,3), _*_)); // => ~80000
my_map([1,2,3], _(x) -> x*x); // => [1,4,9]
profile_expr(my_map([1,2,3], _(x) -> x*x)); // => ~32000
sq(x) -> x*x; profile_expr(my_map([1,2,3], 'sq')); // => ~36000
sq = (_(x) -> x*x); profile_expr(my_map([1,2,3], sq)); // => ~36000
profile_expr(map([1,2,3], _*_)); // => ~80000
</pre>
## Control flow
@@ -67,7 +67,7 @@ Evaluates expression `expr`, `num` number of times. code>expr receives `_` syste
<pre>
loop(5, game_tick()) => repeat tick 5 times
list = l(); loop(5, x = _; loop(5, list += l(x, _) ) ); list
list = []; loop(5, x = _; loop(5, list += [x, _] ) ); list
// double loop, produces: [[0, 0], [0, 1], [0, 2], [0, 3], [0, 4], [1, 0], [1, 1], ... , [4, 2], [4, 3], [4, 4]]
</pre>
@@ -75,7 +75,7 @@ In this small example we will search for first 10 primes, apparently including 0
<pre>
check_prime(n) -> !first( range(2, sqrt(n)+1), !(n % _) );
primes = l();
primes = [];
loop(10000, if(check_prime(_), primes += _ ; if (length(primes) >= 10, break())));
primes
// outputs: [0, 1, 2, 3, 5, 7, 11, 13, 17, 19]
+2 -2
View File
@@ -61,8 +61,8 @@ Interesting bit - `min` and `max` don't remove variable associations from argume
LHS of assignments (obvious case), or argument spec in function definitions (far less obvious).
<pre>
a = 1; b = 2; min(a,b) = 3; l(a,b) => [3, 2]
a = 1; b = 2; fun(x, min(a,b)) -> l(a,b); fun(3,5) => [5, 0]
a = 1; b = 2; min(a,b) = 3; [a,b] => [3, 2]
a = 1; b = 2; fun(x, min(a,b)) -> [a,b]; fun(3,5) => [5, 0]
</pre>
Absolutely no idea, how the latter might be useful in practice. But since it compiles, can ship it.
+2 -2
View File
@@ -253,8 +253,8 @@ flips boolean condition of the expression. Equivalent of `bool(expr)==false`
!false => true
!null => true
!5 => false
!l() => true
!l(null) => false
![] => true
![null] => false
</pre>
### `Unpacking Operator ...`
+4 -4
View File
@@ -20,9 +20,9 @@ or an OVERLY complex example:
/script run
block_check(x1, y1, z1, x2, y2, z2, block_to_check) ->
(
l(minx, maxx) = sort(l(x1, x2));
l(miny, maxy) = sort(l(y1, y2));
l(minz, maxz) = sort(l(z1, z2));
[minx, maxx] = sort([x1, x2]);
[miny, maxy] = sort([y1, y2]);
[minz, maxz] = sort([z1, z2]);
'Need to compute the size of the area of course';
'Cause this language doesn\'t support comments in the command mode';
xsize = maxx - minx + 1;
@@ -46,7 +46,7 @@ or an OVERLY complex example:
check_area_around_closest_player_for_block(block_to_check) ->
(
closest_player = player();
l(posx, posy, posz) = query(closest_player, 'pos');
[posx, posy, posz] = query(closest_player, 'pos');
total_count = block_check( posx-8,1,posz-8, posx+8,17,posz+8, block_to_check);
print('There is '+total_count+' of '+block_to_check+' around you')
)
+12 -12
View File
@@ -23,8 +23,8 @@ represent binary values.
bool(pi) => true
bool(false) => false
bool('') => false
bool(l()) => false
bool(l('')) => true
bool([]) => false
bool(['']) => true
bool('foo') => true
bool('false') => false
bool('nulL') => false
@@ -41,8 +41,8 @@ number(false) => 0
number(true) => 1
number('') => null
number('3.14') => 3.14
number(l()) => 0
number(l('')) => 1
number([]) => 0
number(['']) => 1
number('foo') => null
number('3bar') => null
number('2')+number('2') => 4
@@ -54,7 +54,7 @@ If called with one argument, returns string representation of such value.
Otherwise, returns a formatted string representing the expression. Arguments for formatting can either be provided as
each consecutive parameter, or as a list which then would be the only extra parameter. To format one list argument
, you can use `str(list)`, or `str('foo %s', l(list))`.
, you can use `str(list)`, or `str('foo %s', [list])`.
Accepts formatting style accepted by `String.format`.
Supported types (with `"%<?>"` syntax):
@@ -221,8 +221,8 @@ or length of the list
length(pi) => 1
length(pi*pi) => 1
length(pi^pi) => 2
length(l()) => 0
length(l(1,2,3)) => 3
length([]) => 0
length([1,2,3]) => 3
length('') => 0
length('foo') => 3
</pre>
@@ -312,11 +312,11 @@ Unlike the previous function, this can be used to get exact time, but it varies
### `convert_date(milliseconds)`
### `convert_date(year, month, date, hours?, mins?, secs?)`
### `convert_date(l(year, month, date, hours?, mins?, secs?))`
### `convert_date([year, month, date, hours?, mins?, secs?])`
If called with a single argument, converts standard POSIX time to a list in the format:
`l(year, month, date, hours, mins, secs, day_of_week, day_of_year, week_of_year)`
`[year, month, date, hours, mins, secs, day_of_week, day_of_year, week_of_year]`
eg: `convert_date(1592401346960) -> [2020, 6, 17, 10, 42, 26, 3, 169, 25]`
@@ -335,9 +335,9 @@ Example editing:
<pre>
date = convert_date(unix_time());
months = l('Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec');
months = ['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sep','Oct','Nov','Dec'];
days = l('Mon','Tue','Wed','Thu','Fri','Sat','Sun');
days = ['Mon','Tue','Wed','Thu','Fri','Sat','Sun'];
print(
str('Its %s, %d %s %d, %02d:%02d:%02d',
@@ -400,7 +400,7 @@ used for object counting
<pre>
/script run
$ count_blocks(ent) -> (
$ l(cx, cy, cz) = query(ent, 'pos');
$ [cx, cy, cz] = query(ent, 'pos');
$ scan(cx, cy, cz, 16, 16, 16, var('count_'+_) += 1);
$ for ( sort_key( vars('count_'), -var(_)),
$ print(str( '%s: %d', slice(_,6), var(_) ))