This topic almost really belongs in Intermediate Muf
Programming, but blocks are such fun that I can't resist
introducing them here. Also, we need them for ]print
,
which is essential to civilized life under MUF.
There are many times when we want to operate on a whole group of values, or wish to write a function which does so. MUF has a dazzling (some might say bewildering!) assortment of functions to assist with this. We'll show just a few of of the essential ones here. Well... some of the fun ones too!
The seq[
function creates a block of values, and the
]pop
function discards a block of values. Notice
the square brackets in their names: functions which
create a block always end with a [
, and functions
which destroy a block always start with a ]
. If
you keep your brackets balanced, your code will
likely make sense:
Stack: 12 seq[ Stack: [ 0 1 2 3 4 5 6 7 8 9 10 11 | ]pop Stack: 18 seq[ Stack: [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
A block consists of some number of values sandwiched between
[
and |
.
The first block above consists of the twelve
values 0-11 The second
block above consists of the eighteen values 0-17.
You can use |mix
to shuffle a block randomly, and
|sort
to sort them. (Functions which modify a
block always begin with a |
. If you've played with
unix, you'll spot the similarity to pipe notation.)
Stack: 12 seq[ Stack: [ 0 1 2 3 4 5 6 7 8 9 10 11 | |mix Stack: [ 9 1 8 10 5 6 7 4 0 2 11 3 | |mix Stack: [ 2 5 10 1 6 4 8 0 3 11 9 7 | |sort Stack: [ 0 1 2 3 4 5 6 7 8 9 10 11 |
There are lots of ways other than seq[
to create
a block. One of the most mundane, but most useful is
simply to list them, between [
and |
:
Stack: [ "a" "b" "c" "d" | Stack: [ "a" "b" "c" "d" | |mix Stack: [ "b" "c" "d" "a" | |sort Stack: [ "a" "b" "c" "d" | 1 |rotate Stack: [ "b" "c" "d" "a" |
Another way is to explode a string into a block of words:
Stack: "The goddess is alive, and magic afoot" Stack: "The goddess is alive, and magic afoot" words[ Stack: [ "The" "goddess" "is" "alive" "and" "magic" "afoot" | |mix Stack: [ "goddess" "magic" "alive" "and" "afoot" "is" "The" | ]words Stack: "goddess magic alive and afoot is The" stringUpcase stringMixedcase Stack: "Goddess magic alive and afoot is the"
Go to the first, previous, next, last section, table of contents.