Sets

Monkey Forums/Monkey Programming/Sets

Jimbo(Posted 2012) [#1]
I was quite excited to find the Monkey had a Set type, then very disappointed to find that it doesn't. The Set type in Monkey is really a Map variant.

It appears (but I may well be wrong) NOT to have union, exclusion, difference, and equality operators. These make sets very powerful.

I've used proper sets frequently over the last 30+ years. They save huge amounts of iteration. I see complex loops for card games in the forums that could be solved with a couple of proper set statements.

Has anybody produce a sensible Set module?


zoqfotpik(Posted 2012) [#2]
How long would it really take to do this? Seems like it would be fairly trivial to write such a thing.


Goodlookinguy(Posted 2012) [#3]
I've never personally used a set data structure, but I looked it up and it took me all of 20-minutes to hack monkey.set and add these. So I might as well share it.

monkey.set (EDIT: I optimized it a bit)


Test I made



Jimbo(Posted 2012) [#4]
That's very neat!

Normally, however, this will be done highly effectively at processor level by using the logical operators on a small array of bit-fields and involves no iteration. Set elements are enumerated types or sub-ranges.

Simple (very) example in Pascal:

Type TPersonType = (male,female,blonde,brunette,redhead,grey,bald,bearded,minor,senior);

TPerson = set of TPersonType;

var Fred,Mary,IllegalKisser: TPerson;

...
Fred := [male,bald,bearded,senior];
Mary := [female,blonde,minor];
IllegalKisser := [minor,senior];

If (Fred * Mary * IllegalKisser_) = IllegalKisser then // arrest Fred

In game terms the blistering speed of this is very useful for deep alpha-beta pruning, since for any position we can declare which moves are legal and which are not, include legal moves in a set, yielding a very fast evaluation. It's also valuable for token parsing in compilers, since after reading a token there will be a set of expected successors, and if the next token is not in the "expected" set we can throw an error.

I think this needs to be implemented as a core language element to be properly effective!


Goodlookinguy(Posted 2012) [#5]
I'm gonna be honest, I'm 20-years-old now and was born into an age of electronics getting faster and faster. From my experience, the amount of time it'd take to re-program this to run at these optimal speeds does not outweigh the actual benefits you'll receive in modern electronics. This becomes more true every day and follows a pattern much like that of diminishing returns. So I wouldn't put too much focus on this. That's my two cents anyway.


muddy_shoes(Posted 2012) [#6]
I think this needs to be implemented as a core language element to be properly effective!


It can't be implemented as a core language element of Monkey in the performant way that you imply it is for Pascal. Monkey is a language that is translated to other languages that aren't Pascal and don't have sets as first-class citizens of the language. There's nothing stopping you writing a bit-field based set class if you want, although it implies limitations on usage that implementations based on maps don't have.

From my experience, the amount of time it'd take to re-program this to run at these optimal speeds does not outweigh the actual benefits you'll receive in modern electronics.


Eh? Are you seriously implying that someone who wants a fast set implementation in Monkey would be better off waiting for all their customers to get faster computers?


Goodlookinguy(Posted 2012) [#7]
Eh? Are you seriously implying that someone who wants a fast set implementation in Monkey would be better off waiting for all their customers to get faster computers?


No, I'm not. I didn't imply anything like that. My statement simply means that you could waste time programming an optimal set or just use this one and it's unlikely that it'll be that much more of a burden on the machines that run it. Scripting languages were being used back in the 90's and those games ran fine. That's why I don't see any particular reason to put so much focus on something so minute.

Edit: Just to clarify, I referred to scripting languages because they are typically very heavy. Code like this is probably lighter than any average scripting language type code.


Jimbo(Posted 2012) [#8]
If bit-fields don't work there isn't much point in having the bit-manipulation operators in Monkey, is there? I think that Monkey is an elegant language, but I worry when I find statements such as the range of Int being platform-specific (in the Language docs). It would be nice to have explicit range-types: Int32, Int64, and unsigned equivalent types.

I can see why it would be problematic as a low-level implementation, obviously. Cross-platform code is inevitably lowest common denominator. I was merely disappointed to find that Monkey sets aren't really sets!

Thanks for the comments.


muddy_shoes(Posted 2012) [#9]
Whether it's minute or not would depend entirely on whether your algorithm makes heavy use of set operations or not. There's no possible way to make a general statement about the benefit of spending time optimising this for whatever purpose Jimbo has in mind from some broad observation that some games in the 90s used scripting languages and ran okay.


muddy_shoes(Posted 2012) [#10]
If bit-fields don't work there isn't much point in having the bit-manipulation operators in Monkey, is there?


I didn't say that bit-fields don't work. I said that the target languages "...don't have sets as first-class citizens of the language. There's nothing stopping you writing a bit-field based set class if you want...". In other words, you've got the tools to do something similar, but you can't expect Monkey to generate something as fast as a Pascal compiler could.


Goodlookinguy(Posted 2012) [#11]
Whether it's minute or not would depend entirely on whether your algorithm makes heavy use of set operations or not. There's no possible way to make a general statement about the benefit of spending time optimising this for whatever purpose Jimbo has in mind from some broad observation that some games in the 90s used scripting languages and ran okay.


You missed the key point of my statements and are being a bit too serious for my taste. So I'm gonna back off before this thread goes in some wayward direction downhill.

Edit: In case the point of my statements is still unclear. It's, finish a game first, if you want to go back and optimize, do it later.


Jimbo(Posted 2012) [#12]
I didn't mean to spark a controversy!

As Monkey claims to be a "brand new language" I think it's valid to discuss potential extensions. To implement sets properly would require operator overloading, which I don't think Monkey has.


zoqfotpik(Posted 2012) [#13]
Let's not let this sort of argument culture take root here.

I'll be implementing this just because it's interesting.