Skip to content

Commit 07ad274

Browse files
committed
read update
1 parent e96264a commit 07ad274

File tree

1 file changed

+175
-1
lines changed

1 file changed

+175
-1
lines changed

README.md

Lines changed: 175 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,6 +314,15 @@ true
314314
false
315315
```
316316

317+
###random(arg1, arg2)
318+
319+
The *random()* function takes two numbers as arguments, and returns a random number between the arguments.
320+
321+
```
322+
>> random(1, 90)
323+
67
324+
```
325+
317326
##Variables
318327

319328
In RoyalScript, variables are assigned to literals using the *=()* function. You can only assign one variable in each call to the function. You can use any variable name that starts with a letter, *$*, or *_*, and has the same characters or digits after that. For example:
@@ -973,6 +982,171 @@ map(range(0, 10),
973982

974983
The *filter()* function in RoyalScript takes a list and a function or proc that takes one argument and returns a boolean value. It goes through a list, and checks if eahc value returns true or false from the proc or function. It then returns a new list for all the values that made the function or proc return true.
975984

985+
This is a program that filters a list for only the even numbers.
986+
987+
```
988+
filter(range(0, 20),
989+
@(item, ==(0, %(item, 2)))
990+
)
991+
;[0,2,4,6,8,10,12,14,16,18];
992+
```
993+
994+
To make these more powerful, you can also chain map and filter calls to make a list comprehension function.
995+
996+
```
997+
map(
998+
filter(
999+
range(0, 20),
1000+
@(item, ==(0, %(item, 2)))),
1001+
@(elem, //(elem, 3))
1002+
)
1003+
1004+
;[0,0,1,2,2,3,4,4,5,6];
1005+
```
1006+
This is a chain using the previous filter and mapping that list to a new list with all the elements in the previous list floor divided by 3.
1007+
1008+
##Defining Functions
1009+
1010+
RoyalScript allows you to define your own full size functions, which take any amount of parameters and have any amount of statements you want. RoyalScript also uses a special function, *return()*, which allows you to return nothing, one value, or multiple values in a list.
1011+
1012+
###def(name, args\_exp, call\_exp...)
1013+
1014+
The *def()* function is the function which defines new named functions. Functions are much more flexible and powerful than procs, because they are named, they can have any amount of parameters, any amount of call expressions, and either return a value, or not, or have multiple return statements in different parts of the function, such as with control flow.
1015+
1016+
This is a simple function with calculates the factorial of some number recursively.
1017+
1018+
```
1019+
def(factorial,
1020+
args(n),
1021+
if(==(n, 1),
1022+
return(1),
1023+
return(
1024+
*(n, factorial(-(n, 1)))
1025+
)
1026+
)
1027+
)
1028+
,do(factorial(5))
1029+
;120;
1030+
```
1031+
1032+
You can also define functions with no parameters:
1033+
1034+
```
1035+
def(noparams,
1036+
args(),
1037+
return(true, true)
1038+
)
1039+
, do(noparams())
1040+
;[true,true];
1041+
```
1042+
1043+
However, you can't define a function that is already a standard library function in RoyalScript.
1044+
1045+
```
1046+
def(range,
1047+
args(),
1048+
return(list())
1049+
)
1050+
, do(noparams())
1051+
;Illegal Name Error: Cannot choose reserved function name;
1052+
```
1053+
1054+
You can also use a _ if you are using a function for a loop or map or filter that you don't need the name of.
1055+
1056+
Here is an example using a larger function for mapping
1057+
9761058
```
1059+
map(range(0, 10),
1060+
def(_, args(a),
1061+
;prints the value to console;
1062+
$(a),
1063+
return(a, +(a, 1))
1064+
)
1065+
)
1066+
;1
1067+
2
1068+
3
1069+
4
1070+
5
1071+
6
1072+
7
1073+
8
1074+
9;
1075+
;[[0,1],[1,2],[2,3],[3,4],[4,5],[5,6],[6,7],[7,8],[8,9],[9,10]];
1076+
```
1077+
1078+
##Structs
1079+
1080+
Perhaps the most unique feature to RoyalScript are structs. Structs are groups of data fields that don't have a specified type in each field, but are named. They are mutable, and can be passed around as references between functions. You create structs with the *new()* function. Every struct also has it's own type callable with the *type()* function.
1081+
1082+
###struct(name, field...)
1083+
1084+
The *struct()* function defines a new struct that can be initialized. Each struct can have any amount of fields. You don't have to initialize a struct with all it's field called either. However, like the defined functions you cannot use a name that is already taken by a native RoyalScript function.
1085+
1086+
```
1087+
struct(food,
1088+
calories, protein, fat, carbs
1089+
),
1090+
new(food, 100, `5g`, `1g`, `10g`)
1091+
1092+
;{"calories":100,"protein":"5g","fat":"1g","carbs":"10g"};
1093+
```
1094+
1095+
Or with no fields argued.
1096+
1097+
```
1098+
struct(food,
1099+
calories, protein, fat, carbs
1100+
),
1101+
new(food),
1102+
;{};
1103+
```
1104+
1105+
You can also put procs or functions on a struct:
1106+
1107+
```
1108+
struct(bank,
1109+
deposit, balance
1110+
),
1111+
=(a, new(bank, @(a, a), 50)),
1112+
do(
1113+
get(a, `balance`)
1114+
)
1115+
;50;
1116+
```
1117+
1118+
###new(name, arg...)
1119+
1120+
The *new()* function creates a new instance of a struct. You must use a previously defined struct, as RoyalScript does not support generic objects or keyed-value collections. Lists can accept a string as a key, but this behavior is not recommended or type/method safe.
1121+
1122+
You can also have functions return new instances of structs
1123+
1124+
```
1125+
def(factory,
1126+
args(amount, size),
1127+
struct(machine, size, built),
1128+
return(
1129+
make(new(machine, size, true), amount)
1130+
)
1131+
),
1132+
factory(5)
1133+
;[{"built":true},{"built":true},{"built":true},{"built":true},{"built":true}];
1134+
```
1135+
1136+
###type(struct_instance)
1137+
1138+
The *type()* function checks the type of some struct instantance, returning it as a string value.
1139+
1140+
```
1141+
struct(bank,
1142+
deposit, balance
1143+
),
1144+
=(a, new(bank, @(a, a), 50)),
1145+
type(a)
1146+
;"bank";
1147+
```
1148+
1149+
Another technique is also to use a switch statement with the type function called on some argument, so you can handle different types of structs much quicker and faster.
1150+
1151+
**Warning**: It's recommended not to use the *type()* function on primtive types in RoyalScript, such as lists or numbers, because primitive types do not have constructors like structs do.
9771152

978-
```

0 commit comments

Comments
 (0)