DotdotConverter

Discussions about extensions and frameworks

DotdotConverter

Postby oliver » Fri Jan 06, 2012 5:20 am

The range function is very useful. It takes two parameters and creates a vector, like so:
1 8 range => [1 2 3 4 5 6 7 8]

Here's a tiny extension that permits one to write 1..8 to achieve the same.
Code: Select all
var DotdotConverter = {
   type: "dotdot",
   isLoaded: true,
   isStringRepresentation: function(str) { return (str.match(/^(\w+)[.][.](\w+)$/) != null); },
   fromString: function(str) { var rng = str.match(/^(\w+)[.][.](\w+)$/);
      for (var i=1; i<=2; i++) { rng[i] = parseFloat(rng[i]) || vars[rng[i]] || calculator.vars[calculator.currentDataCategory][rng[i]]; }
      return ME.range(rng[1], rng[2]);
   }
};
calculator.registerType(DotdotConverter);

With this, it's now possible to use start..end syntax on the edit line, in RPL programs, and in literal vector initializations.
Besides numbers, local variables and user variables can be used for the start and end values.

For example, all of the below are thus enabled:
Code: Select all
1..1e6

Code: Select all
≪ 1..5 ≫

Code: Select all
≪ 1..n ≫

Code: Select all
≪ [1..3 4..6 7..9] ≫

On the edit line: [1..6,5..8 => [[1 2 3 4 5 6][5 6 7 8]]

This extension will be part of v1.4.1.
oliver
Site Admin
 
Posts: 433
Joined: Sat May 01, 2010 2:11 pm

Return to Extensions

Who is online

Users browsing this forum: No registered users and 1 guest

cron