« July 2006 | Main | December 2006 »

September 30, 2006

Collections in Base Language

Currently, it is almost not possible to write code in Base Language and avoid using of java collections and other JDK classes like Object and String.
Such a dependency on Java is not good for BL (in the end, MPS is not a java development tool) and this is going to change, so far with respect of collections.

The Collections DSL is a domain-specific extension for the Base Language. The central concept here is the sequence, which strongly resembles concepts of iterable (Java) and enumerable (C#).

Collections DSL allows to define a sequence in very simple terms.
For instance, an infinite sequence of natural numbers can be defined as:

collections-new-natural-numbers-sequence.PNG


Collections DSL also defines foreach loop suitable for iteration of a sequence:

collections-foreach-loop.PNG


...and set of operations applicable to a sequence.

In the following example I will take the nums sequence (see above) as an input, then accept only even numbers and generate a string for each such number:

collections-where-select-oper.PNG


The last example (as it becomes a tradition) is the Fibonacci sequence:

collections-fibonacci-sequence.PNG


Note that the method parameter max is used inside the code-block in large square brackets. In fact, code-blocks (that is code framed in large square brackets) have property of closure, i.e. variables from an enclosing lexical context can be used and be modified.

The Collections DSL along with samples probably will be available in the next MPS build.

Posted by Igor Alshannikov at 04:35 AM | Comments (4)