mirror of
https://github.com/wren-lang/wren.git
synced 2026-01-11 22:28:45 +01:00
Tweak some docs.
This commit is contained in:
@ -20,20 +20,20 @@ element you want. Like most languages, indexes start at zero:
|
||||
|
||||
:::wren
|
||||
var hirsute = ["sideburns", "porkchops", "'stache", "goatee"]
|
||||
hirsute[0] //> sideburns
|
||||
hirsute[1] //> porkchops
|
||||
System.print(hirsute[0]) //> sideburns
|
||||
System.print(hirsute[1]) //> porkchops
|
||||
|
||||
Negative indices counts backwards from the end:
|
||||
|
||||
:::wren
|
||||
hirsute[-1] //> goatee
|
||||
hirsute[-2] //> 'stache
|
||||
System.print(hirsute[-1]) //> goatee
|
||||
System.print(hirsute[-2]) //> 'stache
|
||||
|
||||
It's a runtime error to pass an index outside of the bounds of the list. If you
|
||||
don't know what those bounds are, you can find out using count:
|
||||
|
||||
:::wren
|
||||
hirsute.count //> 4
|
||||
System.print(hirsute.count) //> 4
|
||||
|
||||
## Slices and ranges
|
||||
|
||||
@ -41,7 +41,7 @@ Sometimes you want to copy a chunk of elements from a list. You can do that by
|
||||
passing a [range](values.html#ranges) to the subscript operator, like so:
|
||||
|
||||
:::wren
|
||||
hirsute[1..2] //> [porkchops, 'stache]
|
||||
System.print(hirsute[1..2]) //> [porkchops, 'stache]
|
||||
|
||||
This returns a new list containing the elements of the original list whose
|
||||
indices are within the given range. Both inclusive and exclusive ranges work
|
||||
@ -85,9 +85,9 @@ back. Doing so counts back from the size of the list *after* it's grown by one:
|
||||
:::wren
|
||||
var letters = ["a", "b", "c"]
|
||||
letters.insert(3, "d") // OK: inserts at end.
|
||||
System.print(letters) //> [a, b, c, d]
|
||||
System.print(letters) //> [a, b, c, d]
|
||||
letters.insert(-2, "e") // Counts back from size after insert.
|
||||
System.print(letters) //> [a, b, c, e, d]
|
||||
System.print(letters) //> [a, b, c, e, d]
|
||||
|
||||
## Removing elements
|
||||
|
||||
|
||||
@ -10,13 +10,13 @@ curly braces. Each entry is a key and a value separated by a colon:
|
||||
:::wren
|
||||
{
|
||||
"George": "Harrison",
|
||||
"John": "Lennon",
|
||||
"Paul": "McCartney",
|
||||
"Ringo": "Starr"
|
||||
"John": "Lennon",
|
||||
"Paul": "McCartney",
|
||||
"Ringo": "Starr"
|
||||
}
|
||||
|
||||
This creates a map that maps the first names of the Beatles to their last
|
||||
names. Syntactically, in a map literal, keys can be any literal, a variable
|
||||
This creates a map that associates the first name of each Beatle with his last
|
||||
name. Syntactically, in a map literal, keys can be any literal, a variable
|
||||
name, or a parenthesized expression. Values can be any expression. Here, we're
|
||||
using string literals for both keys and values.
|
||||
|
||||
@ -28,12 +28,6 @@ You can also use a [class object][] as a key.
|
||||
[value types]: values.html
|
||||
[class object]: classes.html
|
||||
|
||||
In addition, even though they aren't strictly immutable, [fibers][] can be used
|
||||
as map keys. This is handy for storing data that's roughly "thread-local" by
|
||||
using the current fiber as a map key.
|
||||
|
||||
[fibers]: concurrency.html
|
||||
|
||||
The reason for this limitation—and the reason maps are called "*hash*
|
||||
tables" in other languages—is that each key is used to generate a numeric
|
||||
*hash code*. This lets a map locate the value associated with a key in constant
|
||||
@ -42,7 +36,7 @@ built-in types, only those can be used as keys.
|
||||
|
||||
## Adding entries
|
||||
|
||||
You add new key-value pairs to the map by using the [subscript operator][]:
|
||||
You add new key-value pairs to the map using the [subscript operator][]:
|
||||
|
||||
[subscript operator]: method-calls.html#subscripts
|
||||
|
||||
@ -98,8 +92,8 @@ If the key was found, this returns the value that was associated with it:
|
||||
|
||||
If the key wasn't in the map to begin with, `remove()` just returns `null`.
|
||||
|
||||
If you want to remove *everything* from the map, just like with [lists][], you
|
||||
can just call `clear()`:
|
||||
If you want to remove *everything* from the map, like with [lists][], you call
|
||||
`clear()`:
|
||||
|
||||
[lists]: lists.html
|
||||
|
||||
@ -133,10 +127,10 @@ iterate over the keys and use each to look up its value:
|
||||
System.print("The state bird of " + state + " is " + birds[state])
|
||||
}
|
||||
|
||||
This program will print the three states and their birds. However, the *order*
|
||||
This program prints the three states and their birds. However, the *order*
|
||||
that they are printed isn't defined. Wren makes no promises about what order
|
||||
keys and values will be iterated in when you use these methods. All it promises
|
||||
is that every entry will appear exactly once.
|
||||
keys and values are iterated in when you use these methods. All it promises is
|
||||
that every entry will appear exactly once.
|
||||
|
||||
<a class="right" href="method-calls.html">Method Calls →</a>
|
||||
<a href="lists.html">← Lists</a>
|
||||
|
||||
@ -4,10 +4,10 @@ Wren is deeply object oriented, so most code consists of invoking methods on
|
||||
objects, usually something like this:
|
||||
|
||||
:::wren
|
||||
System.print("hello")
|
||||
System.print("Heyoo!") //> Heyoo!
|
||||
|
||||
You have a *receiver* expression (here `System`) followed by a `.`, then a name
|
||||
(`print`) and an argument list in parentheses (`("hello")`). Multiple arguments
|
||||
(`print`) and an argument list in parentheses (`("Heyoo!")`). Multiple arguments
|
||||
are separated by commas:
|
||||
|
||||
:::wren
|
||||
@ -18,7 +18,7 @@ The argument list can also be empty:
|
||||
:::wren
|
||||
list.clear()
|
||||
|
||||
Semantically, all method calls work like this:
|
||||
The VM executes a method call like so:
|
||||
|
||||
1. Evaluate the receiver and arguments from left to right.
|
||||
2. Look up the method on the receiver's [class][].
|
||||
|
||||
@ -6,76 +6,76 @@ seem to like them, so here's a few:
|
||||
<h3>Method Call</h3>
|
||||
<table class="chart">
|
||||
<tr>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 11%;">0.15s </div></td>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 14%;">0.12s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>luajit (-joff)</th><td><div class="chart-bar" style="width: 17%;">0.23s </div></td>
|
||||
<th>luajit (-joff)</th><td><div class="chart-bar" style="width: 18%;">0.16s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ruby</th><td><div class="chart-bar" style="width: 26%;">0.36s </div></td>
|
||||
<th>ruby</th><td><div class="chart-bar" style="width: 23%;">0.20s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>lua</th><td><div class="chart-bar" style="width: 43%;">0.58s </div></td>
|
||||
<th>lua</th><td><div class="chart-bar" style="width: 41%;">0.35s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 88%;">1.18s </div></td>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 91%;">0.78s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 100%;">1.33s </div></td>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 100%;">0.85s </div></td>
|
||||
</tr>
|
||||
</table>
|
||||
<h3>DeltaBlue</h3>
|
||||
<table class="chart">
|
||||
<tr>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 27%;">0.13s </div></td>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 22%;">0.13s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 94%;">0.44s </div></td>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 83%;">0.48s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 100%;">0.47s </div></td>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 100%;">0.57s </div></td>
|
||||
</tr>
|
||||
</table>
|
||||
<h3>Binary Trees</h3>
|
||||
<table class="chart">
|
||||
<tr>
|
||||
<th>luajit (-joff)</th><td><div class="chart-bar" style="width: 20%;">0.16s </div></td>
|
||||
<th>luajit (-joff)</th><td><div class="chart-bar" style="width: 20%;">0.11s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 39%;">0.29s </div></td>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 41%;">0.22s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ruby</th><td><div class="chart-bar" style="width: 49%;">0.37s </div></td>
|
||||
<th>ruby</th><td><div class="chart-bar" style="width: 46%;">0.24s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 68%;">0.52s </div></td>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 71%;">0.37s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>lua</th><td><div class="chart-bar" style="width: 97%;">0.73s </div></td>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 73%;">0.38s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 100%;">0.75s </div></td>
|
||||
<th>lua</th><td><div class="chart-bar" style="width: 100%;">0.52s </div></td>
|
||||
</tr>
|
||||
</table>
|
||||
<h3>Recursive Fibonacci</h3>
|
||||
<table class="chart">
|
||||
<tr>
|
||||
<th>luajit (-joff)</th><td><div class="chart-bar" style="width: 17%;">0.15s </div></td>
|
||||
<th>luajit (-joff)</th><td><div class="chart-bar" style="width: 17%;">0.10s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 38%;">0.31s </div></td>
|
||||
<th>wren</th><td><div class="chart-bar wren" style="width: 35%;">0.20s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>ruby</th><td><div class="chart-bar" style="width: 40%;">0.33s </div></td>
|
||||
<th>ruby</th><td><div class="chart-bar" style="width: 39%;">0.22s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>lua</th><td><div class="chart-bar" style="width: 43%;">0.35s </div></td>
|
||||
<th>lua</th><td><div class="chart-bar" style="width: 49%;">0.28s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 96%;">0.78s </div></td>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 90%;">0.51s </div></td>
|
||||
</tr>
|
||||
<tr>
|
||||
<th>python</th><td><div class="chart-bar" style="width: 100%;">0.81s </div></td>
|
||||
<th>python3</th><td><div class="chart-bar" style="width: 100%;">0.57s </div></td>
|
||||
</tr>
|
||||
</table>
|
||||
|
||||
@ -184,6 +184,19 @@ means that we can copy down all inherited methods in the subclass when it's
|
||||
created since we know those will never change. Method dispatch then just
|
||||
requires locating the method in the class of the receiver.
|
||||
|
||||
### Method signatures
|
||||
|
||||
Wren supports overloading by arity using its concept of [signatures]. This makes
|
||||
the language more expressive, but also faster. When a method is called, we look
|
||||
it up on the receiver's class. If we succeed in finding it, we also know it has
|
||||
the right number of parameters.
|
||||
|
||||
This lets Wren avoid the extra checking most languages need to do at runtime to
|
||||
handle too few or too many arguments being passed to a method. In Wren, it's not
|
||||
*syntactically* possible to call a method with the wrong number of arguments.
|
||||
|
||||
[signatures]: method-calls.html#signature
|
||||
|
||||
### Computed gotos
|
||||
|
||||
On compilers that support it, Wren's core bytecode interpreter loop uses
|
||||
|
||||
Reference in New Issue
Block a user