text
stringlengths
0
897
Now notice that when invoking a call without any arguments, you don't need any parentheses. (Technically you can put empty parentheses after the function name, but this is considered bad MiniScript style.)
You also don't need parentheses when the function you're calling is the first (main) word on the statement, as with `print`. This won't come as a surprise to you, since you've been using `print` without any parentheses since Day 1!
There's one more trick when it comes to invoking functions. Many functions that operate on a particular type of data can be invoked in a different way, using a dot (i.e. period) after the data, and before the function name. The value before the dot then gets passed in as the first argument. This seems a little pointle...
```miniscript
print "Hello world".len
```
This uses the `len` function, which returns the length of the string (that is, a count of how many characters it contains). The above could also be written as:
```miniscript
print len("Hello world")
```
...but the dot syntax is more convenient in many cases we'll get to in the future.
{i:"functions, numeric;numeric functions"}
## Built-In Numeric Functions
In the table below is a list of built-in (or *intrinsic*) MiniScript functions that work with numbers. In this table, x and y are any number, i is an integer, and r is a number of radians.
intrinsic
: built into MiniScript, rather than added by your own code
{i:"intrinsic functions, numeric;numeric functions"}
{caption:"Intrinsic numeric functions"}
| `abs(x)` | absolute value of `x` |
| `acos(x)` | arccosine of `x`, in radians |
| `asin(x)` | arcsine of `x`, in radians |
| `atan(y, x=1)` | arctangent of `y/x`, in radians |
| `ceil(x)` | next whole number equal to or greater than `x` |
| `char(i)` | returns Unicode character with code point `i` |
| `cos(r)` | cosine of `r` radians |
| `floor(x)` | next whole number less than or equal to `x` |
| `pi` | 3.14159265358979 |
| `round(x, d=0)` | `x` rounded to `d` decimal places |
| `rnd(seed=null)` | returns random number which is at least 0 but less than 1 |
| `sign(x)` | sign of `x`: -1 if `x` < 0; 0 if `x` == 0; 1 if `x` > 0 |
| `sin(r)` | sine of `r` radians |
| `sqrt(x)` | square root of `x` |
| `str(x)` | converts `x` to a string |
| `tan(r)` | tangent of `r` radians |
It looks like a lot, but that's OK! You don't need to remember or even understand all of this table yet. It's included here just so you'll have a complete table to refer to later. For now, let's just touch on a few of the highlights.
Some of these functions you have seen already; we covered `sign`, `sqrt`, `pi`, and `round` above. Many of the others -- `sin`, `cos`, `tan`, `asin`, `acos`, and `atan` -- relate to trigonometry. Unless you are a mathematician or engineer, you may not have much use for these yet, but you may need them someday.
Two of these functions convert a number to a string, but in different ways. The `char` function gets a character from its Unicode code point; for example char(65) is "A", char(66) is "B", and so on.
Unicode
: a standard assignment of a unique number to every character in every human language
On the other hand, `str` converts a number to a string in the same way that `print` does. So `str(65)` returns the string "65". (If you print this out, this will look the same as printing 65 directly; for now you'll just have to trust that you really did convert it to a string yourself.)
Here's an example that uses the numeric function `sqrt`.
```miniscript
print "The square root of 100 is " + sqrt(100)
```
{i:"functions, string;string functions"}
## Built-In String Functions
All the string functions except `slice` are designed to be invoked on strings using dot syntax. But as noted above, they can also be invoked by putting the function name first, and then passing the string in as the first argument. The table on the next page shows them with dot syntax. In this table, `self` refers to...
Again, don't worry about understanding all of this. You'll only need a few of these for quite a while. `len`, which you've already seen, is handy to get the length of a string. You may use `upper` and `lower` to convert a string to uppercase or lowercase.
To find the Unicode code point of the first character in a string, you can call the `code` function. This is the inverse of the `char` function you saw in the *numeric functions* table. To do the inverse of `str`, that is convert a string into a number, you'll use `val`, but that's covered in detail on Day 4.
{pageBreak}
{i:"intrinsic functions, string;string functions"}
{caption:"Intrinsic string functions", colWidths:"150,*"}
| `.code` | Unicode code point of first character of `self` |
| `.hasIndex(i)` | 1 if `i` is >= 0 and \< self.len; otherwise 0 |
| `.indexes` | a list of all valid indexes of the string |
| `.indexOf(s, after=null)` | 0-based position of first substring `s` within `self`, or `null` if not found; optionally begins the search after the given position |
| `.len` | length (number of characters) of `self` |
| `.lower` | lowercase version of `self` |
| `.remove(s)` | `self`, but with first occurrence of substring s removed (if any) |
| `.replace(oldVal, newVal, maxCount=null)` | returns a string with the first `maxCount` elements matching `oldVal` replaced with `newVal`; if `maxCount` not specified, replaces all |
| `.split(delimiter=" ", maxCount=null)` | splits `self` into a list of at most `maxCount` elements |
| `.upper` | uppercase version of `self` |
| `.val` | converts `self` to a number (if `self` is not a valid number, returns 0) |
| `.values` | list of individual characters in `self` (e.g. "spam".values = ["s", "p", "a", "m"] |
| `slice(s, from, to)` | a substring of `s` starting at index `from` and going up to index `to` |
Example usage:
```miniscript
print "Hello World!".upper
```
## Coding is an Open-Book Process
Some readers will feel a strong urge to study and memorize all the details thrown at you in this chapter. Don't bother! There is no final exam at the end of this book. The only test is how well you can apply your new coding skills to solve your own problems, and when you do that, you are allowed -- nay, encouraged! ...
So, far more valuable than memorizing all those functions is just remembering that you saw them. When you need to do something with a number or a string, say to yourself, "hmm, there's probably a function for that," and then either come back to this chapter, or use one of the other resources you know about to find the...