Tuesday, November 8, 2011

What's new in 1.6.6

In this post I would like to discuss some things done in 1.6.6. The migration of ELENA code base to LIB2 (and simultaneously to GCX ) was finally completed. Up'N'Down sample (the most complicated program written in ELENA so far) is now playable again. As a result I was able to continue work on ELENA library. So I would like to present two new classes (actually external roles) introduced in 1.6.6: std'patterns'esort and std'routines'estrop.

First of all I have to remind some basic things about roles and groups. A role is an alternative set of methods which can be used for context-dependant programming. The role cannot have fields so it could be considered as stateless class. There are two types of roles: internal and external ones.

A group is a collection of objects sharing the common instance variable (self). The group is used for dynamic code extending. We could consider it either as a JIT (just in time) inheritance or as a code mutation. For example if I would like to add or override the object method I could extend it by creating a group with my custom object containing the required code. The given group will be practically identical to the original object and could be used instead of it.

So if we would like to apply the role to the object we could actually group them. For internal role it is done with a help of #shift statement. For external we should declare the group ourselves with tilde operator (in reality it is a bit more complicated but I will simplify for the sake of clarity).

So far so good. Now let's turn to our new classes (roles). std'patterns'ESort is used to extend any collection / array with sorting functionality. Here some examples how it could be used:

#var a := (1,7,4,-5).
a~esort run: aPair => (aPair former < aPair later).

#var b := ("b","ab","a","aa").
b~esort run: aPair => (aPair former < aPair later).

#var c := list += 3 += -4 += 1.
c~eindexeditem~esort run: aPair => (aPair former < aPair later).

#var d := list += "b" += "av" += "a".
d~eindexeditem~esort run: aPair => (aPair former < aPair later).

Note that std'routines'EIndexedItem extends any collection with "@" operator (used for array-like access to its members). Run method sorts the collection basing on the provided expression.

Our another role - std'routines'EStrOp extends the literal value with some basic string operations like find, insert, delete and substring.

#var p1 := "abba"~estrop find &first:"b".
#var p2 := "abba"~estrop find &last:"a".

#var s2 := "abba"~estrop replace &from:"b" &to:"B".
#var s3 := "abba"~estrop replace_all &from:"b" &to:"B".
#var s4 := "abba"~estrop substring &from:1 &to:2.

#var s41 := "abba"~estrop substring &from:1 &length:2.

#var s5 := "aa"~estrop insert &literal:"bb" &index:1.
#var s6 := "abbba"~estrop delete &from:2 &to:2.