I have been planning how to add Hashmaps (Tables) to the ‘I’ programming language.
//Something like this. var scores = table() scores["Player 1"] = 1 scores["Quentin"] = 22 scores["anonymous"] = -1
Unfortunately, universal assembly does not have a specific feature to do this yet and trying to create a hashmap in pure 'I' is more difficult than it should be.
Instead, I added a questionably-practical type called a Set! A Set is a collection of arbitrary items with an unspecified order.
For example here is a collection of ingredients:
var ingredients = <chicken, flour, eggs, oats, butter, rice, pasta, cheese, milk, sugar>
It turns out these are the ingredients I have in my pantry. The thing is now I want to make cookies. Luckily, I have my cookie recipe defined as a set too!
var cookies = <flour, oats, sugar, butter>
Great, so I'm not sure if I have all the ingredients I need to make cookies. The good thing about sets is that this is possible:
if cookies <= ingredients print("You can make cookies!") end
It is easy to check if I have the ingredients with a set, it may be easy to just look at both lists but imagine if I had a list of 100 ingredients! Then this is a very simple way to check if I have the ingredients for a particular recipe.
Look! I programmed these cookies into existence.
Technical Details
In 'I', sets are implemented as integers which are the product of each contained label (each label has a prime number assoicated with it). This makes it quick to check membership of the Set. It is as simple as a modulo operation on the product. This is awesome because of the complexity values of each set operation:
This is at the expense of O(n) to list the members of the Set (where n is the number of elements in the universal set). It's a set though.. you don't need to traverse it!
The other downside is that the labels need to be defined at compile time but I can see this changing when I implement hashmaps and the associations can be stored in a hashmap.