Jump to content

Redefining language elements


Recommended Posts

  • Moderators

I recently discovered the possibility to "redefine" some language elements by using the js prototype mechanism.

 

As an example see code below which implements an "array" object based directly on a modified js array and gives it customised methods as well.

In this case array.push is arbitrarily renamed to array.add, array.splice to array.remove and lookUpIndex and lookUpValue are implemented as search methods.

 

Code is based on and inspired by http://blog.benoitvallon.com/category/data-structures-in-javascript/

 

While an array example is maybe somewhat trivial, the same method can be used to redefine for instance "sets" - which are only partially supported natively by SMS.

The github link in the article features example code for implementation of sets including union of sets etc but also other data structures like FIFO queues with methods which only support first-in first-out operations.

And more.

Invocation of array object
 
...
type
  TForm1 = class(TW3Form)
    procedure W3Button1Click(Sender: TObject);
  private
    {$I 'Form1:intf'}
  protected
   ...
  public
    MyArray : TArray;
  end;
 
implementation
 
{ TForm1 }
 
procedure TForm1.W3Button1Click(Sender: TObject);
begin
  MyArray.Add('AA');
  MyArray.Add('AB');
  MyArray.Add(33);
  showmessage(MyArray.LookUpIndex(1));          // 'AB'
  MyArray.Remove('AB');
  showmessage(MyArray.LookUpIndex(1));          // 33
  MyArray.Print;                                // 'AA' 33
  ShowMessage(MyArray.LookUpValue(33));         // index 1
  ShowMessage(MyArray.Length);                  // length = 2
end;
 
procedure TForm1.InitializeForm;
begin
  inherited;
  // this is a good place to initialize components
  MyArray := TArray.Create;
end;
 

and the component code

unit Unit2;
 
interface
 
uses 
  SmartCL.System, System.Types;
 
type
  TArray = class (TObject)
  public
    Constructor Create; virtual;
    Procedure Add(Item: Variant);
    Function LookUpIndex(Item:Integer): Variant;
    Procedure Remove(Item: Variant);
    Procedure Print;
    Function LookUpValue(Item:Variant): Variant;
    Function Length : Variant;
    FHandle: THandle;
  end;
 
implementation
 
Constructor TArray.Create;
begin
  inherited Create;
 
asm
function MyArray() {
  this.array = [];
}
 
MyArray.prototype.add = function(data) {
  this.array.push(data);
};
 
MyArray.prototype.remove = function(data) {
  if(~this.array.indexOf(data)) {
    this.array.splice(this.array.indexOf(data), 1);
  }
};
 
MyArray.prototype.lookUpValue = function(data) {
  if(~this.array.indexOf(data)) {
    return this.array.indexOf(data);
  } else {
    return null;
  }
};
 
MyArray.prototype.lookUpIndex = function(index) {
  return this.array[index];
};
 
MyArray.prototype.length = function() {
  return this.array.length;
};
 
MyArray.prototype.print = function() {
  console.log(this.array.reduce(function(prev, curr) {
    return prev + curr + ' ';
  }, '').trim());
};
 
  @FHandle = new MyArray();
end;
 
end;
 
Procedure TArray.Add(Item: Variant);
begin
  FHandle.add(Item);
end;
 
Procedure TArray.Remove(Item: String);
begin
  FHandle.remove(Item);
end;
 
Function TArray.LookUpIndex(Item:integer):Variant;
begin
   result := FHandle.lookUpIndex(Item);
end;
 
Procedure TArray.Print;
begin
  FHandle.print();
end;
 
Function TArray.LookUpValue(Item:variant):Variant;
begin
   result := FHandle.lookUpValue(Item);
end;
 
Function TArray.Length:Variant;
begin
   result := FHandle.length();
end;
 
end.
 

And as numbers are objects in js as well, there is no barrier to redefine them too and give all floats a method 'Number.ConvertInchToCentimeter' or something or implement irrational number handling :)

 

.

Link to post
Share on other sites
  • 4 years later...

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...