text stringlengths 0 897 |
|---|
{gap:20} |
Let's reinforce it with a different example. Suppose you're writing a program to keep track of cookies — it could be cookies baked, or cookies eaten; it's up to you! But in any case your program will be dealing with a lot of cookies, and they are all pretty similar. So it's a good idea to make a class to represent t... |
```miniscript |
Cookie = {} |
Cookie.radius = 3 |
Cookie.area = function() |
return pi * self.radius^2 |
end function |
``` |
Here we have created a class called `Cookie`. Of course it's really just a map, but it's a map that we *think of* as a class; it's going to represent everything that's common about all cookies in our program. That includes `radius`, which (unless we change it) is 3 centimeters, and an `area` function that can use tha... |
Now let's actually instantiate (make an instance of) the `Cookie` class by using `new`. |
```miniscript |
c1 = new Cookie |
print c1.area |
``` |
This should print `28.274334`, which is the area of a cookie with a radius of `3`. But let's suppose this is a somewhat larger cookie. Change the radius to 4, and print its area again: |
```miniscript |
c1.radius = 4 |
c1.area |
``` |
This prints `50.265482`. When we call the `area` method on `c1`, it invokes the code that's actually stored on `Cookie`, but the `Cookie.area` function uses `self.radius`. In this case that is the radius of `c1`, which we just set to `4`. |
Let's make our `Cookie` class a little fancier. We'll have it also keep track of how many chips are in the cookie, and use that (along with the area) to estimate how many calories the cookie has. And while we're at it, let's also give our class a `description` method, similar to the one we had above for `Block`. Jus... |
```miniscript |
Cookie.chips = 0 |
Cookie.calories = function() |
return 5 * self.area + 2 * self.chips |
end function |
Cookie.desc = function() |
result = self.radius * 2 + " cm cookie" |
result = result + " with " + self.chips + " chip" |
if self.chips != 1 then result = result + "s" |
result = result + " (" + round(self.calories) + " cal)" |
return result |
end function |
``` |
This assigns a new value (`chips`) and two new methods (`calories` and `desc`) to the Cookie class. Because `c1` is a Cookie, it immediately gets to use this new stuff. Add `print c1.desc`, and you should see "8 cm cookie with 0 chips (251 cal)". |
Now instead of just creating a second cookie, let's create a whole batch of cookies! And we'll make them all different, with a random radius and number of chips. |
```miniscript |
cookies = [] |
for i in range(1, 9) |
c = new Cookie |
c.chips = floor(rnd * 10) |
c.radius = 2 + 3 * rnd |
cookies.push c |
end for |
``` |
This will create (and print) a list of ten random cookies. If you examine this list carefully, you'll see that each has a radius, a number of chips, and an `__isa` key that points to `Cookie`. But that list is hard to read; let's print them out a bit more nicely. |
```miniscript |
for item in cookies; print item.desc; end for |
``` |
D> Tip: you can put several short statements on one line by separating them with semicolons. This is especially handy in a REPL, since you can up-arrow to recall the whole multi-statement line at once, rather than having to repeat each statement one at a time. |
This should produce a result that looks something like this: |
``` |
7.679863 cm cookie with 0 chips (232 cal) |
6.053417 cm cookie with 4 chips (152 cal) |
7.978822 cm cookie with 0 chips (250 cal) |
7.69492 cm cookie with 9 chips (251 cal) |
9.87915 cm cookie with 3 chips (389 cal) |
7.336143 cm cookie with 1 chip (213 cal) |
8.179308 cm cookie with 7 chips (277 cal) |
9.685048 cm cookie with 9 chips (386 cal) |
9.216735 cm cookie with 9 chips (352 cal) |
``` |
{i:"`list.sort`;sorting;list, sorting"} |
Of course your numbers will be different, since they are random. Just for fun, let's sort this list by radius. The `list.sort` method takes an optional second parameter, which is the name of a key to sort by. So we can do: |
```miniscript |
list.sort "radius" |
for item in cookies; print item.desc; end for |
``` |
And now you should see the same batch of cookies, but ordered by radius. |
To review, in this example we created a class called `Cookie`, and then created a bunch of objects (which are also called instances of the class) and stored them in the list. Each of our objects is a `Cookie`, and inherits all the properties and methods that `Cookie` defines. |
## Writing Reusable Code |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.