07 February 2016

Lesson 15 – Using maps in 8th

Conclusion of the lessons on arrays

In the previous lessons we have seen the most advanced words for the array namespace.

Now it is time for you to discover for yourself the other words in that namespace. Learning a language is best done using it. So please play with these words!

What is a map in 8th?

A map is a collection of key / value pairs. The keys are unique within a map. Instead of referencing an item by index (like with arrays) you must reference an item by key.

Keys must be strings in 8th. Values can be of any type.

Creating a new map



So, a literal map is surrounded by braces.

Keys and values must be separated by a colon and key/value pairs must be delimited by a comma.

Note that the colon and the comma (here) are NOT 8th words, but just syntax.

Retrieving a value for a specific key






In order to retrieve the map itself, we need G:@.
In order to retrieve the value for a key we need m:@.

Storing a value in a map using m:!







Note that the order of key/value pairs is arbitrary! This kind of container is not suited for sequential in order access.

Removing a key with it’s value from a map using m:-


Iterating over a map using m:each


Note that this works almost exactly the same as for arrays.

Conclusion on maps

There are a few more very useful words for maps.

For now I will leave you with “homework”

  • Study the SED’s for the array and map namespace
  • Try out some words for yourself
  • Never ever give up!
  • Post any interesting piece of code in the forum

If you need help, please ask any question and report any problem you may have in the forum! Today I made a stupid mistake and finally asked in the forum. It’s really not a shame whatsoever.

More info about 8th

8th Website:   http://8th-dev.com
8th Forum:   http://8th-dev.com/forum
My YouTube Channel:   https://goo.gl/DscYVD
My BLOG:   http://goo.gl/n1NocJ
The Big 8th Lessons Book:  http://goo.gl/qDa6fH

02 February 2016

Lesson 14 – Using arrays in 8th – part 2

More on arrays

Now we are going to see a few more advanced words in the a (array) namespace.

Iterating over an array – the word a:each

Later we will see that 8th has special words for iterating / looping.

However, there are also words like a:each that make it very easy to use iteration under the hood, without having to bother with the details.

The SED of a:each is \ a w – a

  • a = input array
  • w = the helper word

The SED of show (in our example below) is \ index value --

  • index = index of current array item
  • value = value of current array item

So a:each is such a word, that takes another word as a parameter (as we saw in the previous lesson).

The show word will be handed the value of the item at TOS and it’s index in 2nd position on the stack.

Example





Note that we have to use swap in order to show the index before the value!

Note that 8th arrays can contain values of different types (unlike e.g. in C).

Output:



Note that a:each leaves the array on the stack after executing!

Reducing an array using a:reduce

Reducing is a process like this:

  1. use an array
  2. get a start value x (which will be used as an accumulator)
  3. for every item in the array:
    1. do something with the values of the current array element and x (and also consume them)
    2. push the result on the stack; that value will then be used as the x value in the next iteration

The SED of a:reduce is \ a w x – x

  • a = input array
  • w = the helper word
  • x = initial value, also used as an accumulator

The SED of sum (just an example word below) is \ value x -- newx

  • value = value of current array item
  • x = initial value, also used as the accumulator

Example


Output:



Filtering an array using a:filter

This word uses a helper word that returns either true or false.

Only if that helper word retruns true, the corresponding element will be put in an output array.

The SED of a:filter is \ a1 w – a2

  • a1 = input array
  • w = the helper word
  • a2 = output array

The SED of iseven (just an example word below) is \ value -- boolean
value = value of current array item
boolean = true or false

Example


Note that the not word regards the value 0 (zero) as false! So when value 0 is encountered, not will output true and otherwise false.

Output:


More info about 8th

8th Website:   http://8th-dev.com
8th Forum:   http://8th-dev.com/forum
My YouTube Channel:   https://goo.gl/DscYVD
My BLOG:   http://goo.gl/n1NocJ