text
stringlengths
0
897
for n in nums
print n.key + " in Spanish is " + n.value
print n.value + " in English is " + n.key
end for
```
Iterating over the map directly like this is usually the right thing to do. However, as noted before, you don't have any control over the order in which the data comes out this way. So, there is another approach that is often used, and that is to iterate over just the indexes (i.e. keys) of the map, in sorted order. ...
```miniscript
ages = {"Buffy":17, "Willow":16, "Xander":17, "Oz":18}
for name in ages.indexes.sort
print name + " is " + ages[name] + " years old."
end for
```
This prints out the age of each hero — but importantly, it prints them out in alphabetical order. How does this work? The `indexes` intrinsic function returns all the keys of a map, as a list. And `sort` is another intrinsic function that sorts a list into alphabetical or numeric order. So when we use them together...
But now we're iterating over just the keys. So inside the loop, if we also need the value, we just look up the value in the map using square brackets (like `ages[name]` in this example).
D> Confused? That's OK. There's a lot of new stuff here, and it's a lot to keep track of. Just take it slow, and break the example above into smaller parts, inspecting each one with the REPL: `ages`, then `ages.indexes`, then `ages.indexes.sort`. Then try a `for` loop over the result of that last one. Step by step...
{gap:40}
## Map Intrinsic Functions
It will come as no surprise to you by now that MiniScript has a number of built-in (intrinsic) functions that work with maps. You've already seen `indexes`, but there are many others. As before, we're going to present the complete set now, but you are not expected to memorize this list. Just familiarize yourself wit...
{i:"functions, map;map functions"}
{caption:"Intrinsic map functions", colWidths:"145,*"}
| `.hasIndex(k)` | 1 if k is a key contained in `self`; otherwise 0 |
| `.indexes` | a list containing all keys in `self`, in arbitrary order |
| `.indexOf(x, after=null)` | first key in `self` that maps to `x`, or null if none; optionally begins the search after the given key |
| `.len` | length (number of key/value pairs) of `self` |
| `.pop` | removes and returns an arbitrary key from `self` |
| `.push k` | equivalent to `self[k] = 1` |
| `.remove k` | removes the key/value pair where key=`k` from `self` (in place) |
| `.replace oldval, newval, maxCount=null` | replaces (in place) up to maxCount occurrences of oldval in the map with newval (if maxCount not specified, then all occurrences are replaced) |
| `.shuffle` | randomly remaps values for keys |
| `.values` | a list containing all values in `self`, in arbitrary order |
MiniScript tends to use functions that are named the same way for multiple data types. That's why the function that checks whether a map contains a certain key is called `hasIndex` rather than `hasKey`; this same function also works for lists and strings, and the term "index" works for all three, whereas "key" would m...
You're only likely to need a few of the functions on this table. We discussed `.indexes` earlier, for getting all the indexes (keys) in the map; in some cases you might also want `.values`, which returns all the values in the map. The `.hasIndex` function is very useful, since you often want to check whether the map ...
Let's wrap up this chapter with a slightly longer, more interesting example. Type in the program on the next page carefully, run it, and then teach it some vocabulary words. You can enter English words and their equivalents in some other language, or you can go the other way (French to English for example). If you d...
{caption:Language-learning program}
```miniscript
print "Teach me a new language!"
print "(or enter ""quit"" to quit)"
vocab = {}
while true
word = input("Word? ")
if word == "quit" then break
if vocab.hasIndex(word) then
print "I know that one! It's: " + vocab[word]
else
vocab[word] = input("OK, how do you say that? ")
print "OK, I'll remember!"
end if
end while
print "You taught me " + vocab.len + " words. Thank you!"
```
Then pause to think what you've accomplished here: a program that *learns*. It starts off knowing nothing, but the more you interact with it, the more it knows. Unlike people, it will remember each fact perfectly after being told only once, and it will never forget — until, of course, you stop the program!
{pageBreak}
A> **Chapter Review**
A> - You learned how to map keys to values in MiniScript.
A> - You discovered the two different ways to access elements of a map: with square brackets, or with dot syntax.
A> - You learned to iterate over the key/value pairs of a map... and an alternate way to do it in cases where you need them in sorted order.
A> - You got an overview of the built-in map functions.
A> - You made a program that learns vocabulary, growing ever smarter as it runs.
{chapterHead: "Day 11: Defining Functions", startingPageNum:115}
{width: "50%"}
![](Chapter11.svg)
Q> You need to organize the code by breaking it into pieces.
Q>— Barbara Liskov (2008 A. M. Turing award winner)
A> **Chapter Objectives**
A> - Learn how to define your own functions.
A> - Start thinking about how to break big problems up into smaller, easier problems.
A> - Learn the difference between local and global variables, and why these make your life easier.
You've seen a lot of intrinsic (built-in) functions by now. These are things like `rnd`, which returns a random number, and `ceil`, which returns the next integer greater than or equal to the number you give it, and `val`, which converts a string into a number. Today you're going to learn how to add new functions of ...
Let's start by looking at an example: making a new function to help you calculate how much tip to leave at a restaurant in the United States.
{caption:Tip example 1}
```miniscript
// Calculate the tip to leave at a restaurant.
// Leave 20%, but never less than a dollar.
tip = function(costOfMeal)
x = costOfMeal * 0.20
if x < 1 then x = 1
return round(x)
end function