Skip to main content
Jump to: navigation, search

Difference between revisions of "VJET/Typing Arrays with VJETDocs"

(New page: {color:#333333}In JavaScript there is the Array type.{color} Below {color:#333333}are examples of using JavaScript Array.{color}<br/> <!-- code start--> <pre style="margin-left:20px; fo...)
 
(Fix syntax in previous examples. Cleanup text and formatting. Add example of Array of o-type.)
 
(7 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{color:#333333}In JavaScript there is the Array type.{color} Below {color:#333333}are examples of using JavaScript Array.{color}<br/>
+
<span style="color:#333333">In JavaScript there is the Array type.</span> Below <span style="color:#333333">are examples of using JavaScript Array.</span><br> <source lang="javascript">
 +
var browsers = ['Firefox', 'Chrome', 'Safari'] ; //< Array
 +
var browsers = new Array('Firefox', 'Chrome', 'Safari') ; //< Array
 +
var browsers = new Array() ; //< Array
 +
browsers[0] = 'FireFox' ;
 +
browsers[1] = 'Chrome' ;
 +
browsers[2] = 'Safari' ;
 +
</source>  
  
 +
What elements you put in the array are up to you. In essence, any JavaScript object can be put in the array. In our previous examples it appears that we only want browser names in our Array. Actually we only want Strings from a typing standpoint.&nbsp;
  
 +
<span style="color:#333333">VJETDocs allows you to be very specific about what type you want your array to be.&nbsp;</span>
  
<!-- code start-->
+
<br> <span style="color:#333333">''Array-Dimension''</span><span style="color:#333333">&nbsp;= "<nowiki>[</nowiki>" "<nowiki>]</nowiki>"</span> <span style="color:#333333">Ex: <nowiki>[</nowiki><nowiki>]</nowiki>&nbsp;</span>  
<pre style="margin-left:20px; font-size:1.4em; background-color:#fdfdfd">
+
var browsers = ['Firefox', 'Chrome', 'Safari'] ; //< Array
+
var browsers = new Array('Firefox', 'Chrome', 'Safari') ; //< Array
+
var browsers = new Array() ; //< Array
+
browsers[0] = 'FireFox' ;
+
browsers[1] = 'Chrome' ;
+
browsers[2] = 'Safari' ;
+
</pre><!-- code end-->
+
  
 +
<span style="color:#333333">''Array-Dimensions''</span><span style="color:#333333">&nbsp;= one or more&nbsp;</span><span style="color:#333333">''Array-Dimension's''</span><span style="color:#333333">&nbsp;back to back</span>
  
What elements you put in the  array are up to you. In essence, any JavaScript object can be put in the  array. In our previous examples it appears that we only want browser  names in our Array. Actually we only want Strings from a typing  standpoint.&nbsp;
+
<span style="color:#333333">Ex: <nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki> or <nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki>&nbsp;</span>
  
{color:#333333}VJETDocs allows you to be very specific about what type you want your array to be.&nbsp;{color}
+
<span style="color:#333333">Array-Type:&nbsp;</span><span style="color:#333333">''Limited-Type-Name''</span><span style="color:#333333">&nbsp;</span><span style="color:#333333">''Array-Dimensions''</span><span style="color:#333333">&nbsp;</span>
  
 +
<span style="color:#333333">Examples of a Vjet Array Type: String<nowiki>[</nowiki><nowiki>]</nowiki> or Number<nowiki>[</nowiki><nowiki>]</nowiki> or Date<nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki>or worker.Task<nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki>&nbsp;</span>
  
{color:#333333}{_}Array-Dimension{_}{color}{color:#333333}&nbsp;= "<nowiki>[[</nowiki>" "<nowiki>]]</nowiki>"{color}
+
<br> <span style="color:#333333">An array with a single <nowiki>[</nowiki><nowiki>]</nowiki> pairs is called a single dimension array.&nbsp;</span><br> <source lang="javascript">
{color:#333333}Ex: <nowiki>[[</nowiki><nowiki>]]</nowiki>&nbsp;{color}
+
var out = vjo.sysout.println;
 +
var scores = [91, 77, 84] ; //< Number[]
 +
var names = ['Mark', 'Sean', 'Juan'] ; //< String[]  
 +
for(var i = 0; i < 3; i++) {
 +
    var name = names[i] ; //< String
 +
    var score = scores[i] ; //< Number
 +
    out(name + ": " + score);
 +
}
 +
</source>
  
{color:#333333}{_}Array-Dimensions{_}{color}{color:#333333}&nbsp;= one or more&nbsp;{color}{color:#333333}{_}Array-Dimension's{_}{color}{color:#333333}&nbsp;back to back{color}
+
console&gt;<br> <span style="color:#333333">Mark: 91</span><br> <span style="color:#333333">Sean: 77</span><br> <span style="color:#333333">Juan: 84&nbsp;</span><br>
  
{color:#333333}Ex: <nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki> or <nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki>&nbsp;{color}
+
<span style="color:#333333">It is possible to define arrays  of arrays; these are called multi-dimension arrays. Multi-dimension  arrays are defined by how many consecutive <nowiki>[</nowiki><nowiki>]</nowiki> pairs.<br>
 +
The number of <nowiki>[</nowiki><nowiki>]</nowiki> pairs is often referred to as the n-dimensional array.
 +
<br>
 +
Thus <nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki> means a 2 dimensional array and <nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki><nowiki>[</nowiki><nowiki>]</nowiki> means a 3-dimensional array and so on</span><span style="color:#333333">.</span><br>
  
{color:#333333}Array-Type:&nbsp;{color}{color:#333333}{_}Limited-Type-Name{_}{color}{color:#333333}&nbsp;{color}{color:#333333}{_}Array-Dimensions{_}{color}{color:#333333}&nbsp;{color}
+
<span style="color:#333333">Here is an example of a two dimensional array of Number.</span><br> <source lang="javascript">
 +
var keys = [ //< Number[][]
 +
[1, 2, 3],
 +
[4, 5, 6],
 +
[7, 8, 9]
 +
];
 +
var total = 0 ; //< Number
 +
for(var row = 0; row < 3; row++) {
 +
  for (var col = 0; col < 3; col++) {
 +
    total += keys[row][col] ;
 +
    vjo.sysout.println('total: ' + total) ;
 +
  }
 +
}
 +
</source>
  
{color:#333333}Examples of a Vjet Array Type: String<nowiki>[[</nowiki><nowiki>]]</nowiki> or Number<nowiki>[[</nowiki><nowiki>]]</nowiki> or Date<nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki>or worker.Task<nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki>&nbsp;{color}
+
<span style="color:#333333">console&gt;&nbsp;total: 45&nbsp;</span><br>  
  
 +
We can define an Object Literal which defines characteristics of a CPU. &nbsp;We scope this definition in a VJO o-type called MachineInfo.
  
{color:#333333}An array with a single <nowiki>[[</nowiki><nowiki>]]</nowiki> pairs is called a single dimension array.&nbsp;{color}<br/>
+
<source lang="javascript">
 +
// This is a VJO o-type. O-types allow us to define types for Javascript constructs
 +
// such as object literals.
 +
// In this example, arrs.MachineInfo is just a scoping name.
 +
vjo.otype('arrs.MachineInfo') //< public
 +
.defs({
 +
cpu: {
 +
vendor: null, //< public String
 +
cores: null, //< public int
 +
clock: null //< public Number
 +
}
 +
})
 +
.endType();
 +
</source>
  
 +
We can then define an Array of CPU's.
  
 
<source lang="javascript">
 
<source lang="javascript">
var out = vjo.sysout.println;
+
//< needs(arrs.MachineInfo)
var scores = [91, 77, 84] ; //< Number[]
+
var names = ['Mark', 'Sean', 'Juan'] ; //< String[]
+
for(var i = 0; i < 3; i++) {
+
    var name = names[i] ; //< String
+
    var score = scores[i] ; //< Number
+
    out(name + ": " + score)
+
</source>
+
  
console>
+
var amd = { //< MachineInfo.cpu
{color:#333333}Mark: 91{color}
+
    clock: 2.8,
{color:#333333}Sean: 77{color}
+
    cores: 4,
{color:#333333}Juan: 84&nbsp;{color}<br/>
+
    vendor: 'AMD'
 +
};
  
{color:#333333}It is possible to define arrays  of arrays; these are called multi-dimension arrays. Multi-dimension  arrays are defined by how many consecutive <nowiki>[[</nowiki><nowiki>]]</nowiki> pairs. The number of <nowiki>[[</nowiki><nowiki>]]</nowiki>  pairs is often referred to as the n-dimensional array. Thus <nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki> means a  2 dimensional array and <nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki> means a 3-dimensional array and so on.&nbsp;{color}{color:#333333}For example <nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki> is a two-dimensional array. <nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki><nowiki>[[</nowiki><nowiki>]]</nowiki> is a three dimensional array and so on.{color}<br/>
+
// The order of elements in the Object Literal does not change the type contract.
 +
var intel = { //< MachineInfo.cpu
 +
    cores: 8,
 +
    clock: 3.1,
 +
    vendor: 'INTEL'
 +
};
  
{color:#333333}Here is an example of a two dimensional array of Number.{color}<br/>
+
// VJET "knows" this is a uniform array of Machine.cpu's so it auto-declares
 +
// for us.  We can see what the declaration would look like since we use it in the
 +
// following function overClock(...)
 +
var cpews = [amd, intel] ;
  
 +
//> void overClock(MachineInfo.cpu[ ] cpus)
 +
function overClock(cpus) {
 +
for(var i = 0; i < cpus.length; i++) {
 +
cpus[i].clock += 1.1 ;
 +
}
 +
}
  
 +
vjo.sysout.println(amd.clock) ; // outputs 2.8
  
<source lang="javascript">
+
// Wind them up!
var keys = [ //< Number[][]
+
overClock(cpews) ;
[1, 2, 3],
+
 
[4, 5, 6],
+
vjo.sysout.println(amd.clock) ; // outputs 3.9
[7, 8, 9]
+
];
+
var total = 0 ; //< Number
+
for(var row = 0; row < 3; row++) {
+
for (var col = 0; col < 3; col++) {
+
total += keys[row][col] ;
+
vjo.sysout.println('total: ' + total) ;
+
 
</source>
 
</source>
 +
<span style="color:#333333">console&gt;<br>
 +
&nbsp;2.8<br>
 +
&nbsp;3.9
 +
</span><br>
  
 +
We can have arrays of Javascript native types and VJET types. We can even have arrays of Functions, Typed-Functions and Type-References.
  
{color:#333333}console>&nbsp;total: 45&nbsp;{color}<br/>
+
[[Category:VJET]] [[Category:VJETDoc]] [[Category:JavaScript]]
 
+
{color:#333333}We can have arrays of native types and vjet types. We can even have arrays of functions (we haven't described them yet).&nbsp;{color}
+
{color:#333333}We can even have an array of type-reference (although it may not be too useful).{color}
+
[[Category:VJET]]
+

Latest revision as of 03:30, 15 February 2013

In JavaScript there is the Array type. Below are examples of using JavaScript Array.
var browsers = ['Firefox', 'Chrome', 'Safari'] ; //< Array 
var browsers = new Array('Firefox', 'Chrome', 'Safari') ; //< Array 
var browsers = new Array() ; //< Array
browsers[0] = 'FireFox' ;
browsers[1] = 'Chrome' ;
browsers[2] = 'Safari' ;

What elements you put in the array are up to you. In essence, any JavaScript object can be put in the array. In our previous examples it appears that we only want browser names in our Array. Actually we only want Strings from a typing standpoint. 

VJETDocs allows you to be very specific about what type you want your array to be. 


Array-Dimension = "[" "]" Ex: [] 

Array-Dimensions = one or more Array-Dimension's back to back

Ex: [][] or [][][] 

Array-Type: Limited-Type-Name Array-Dimensions 

Examples of a Vjet Array Type: String[] or Number[] or Date[][]or worker.Task[][][] 


An array with a single [] pairs is called a single dimension array. 
var out = vjo.sysout.println; 
var scores = [91, 77, 84] ; //< Number[]
var names = ['Mark', 'Sean', 'Juan'] ; //< String[] 
for(var i = 0; i < 3; i++) {
    var name = names[i] ; //< String
    var score = scores[i] ; //< Number
    out(name + ": " + score);
}

console>
Mark: 91
Sean: 77
Juan: 84 

It is possible to define arrays of arrays; these are called multi-dimension arrays. Multi-dimension arrays are defined by how many consecutive [] pairs.
The number of [] pairs is often referred to as the n-dimensional array.
Thus [][] means a 2 dimensional array and [][][] means a 3-dimensional array and so on
.

Here is an example of a two dimensional array of Number.
var keys = [ //< Number[][]
 [1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]
 ];
var total = 0 ; //< Number 
for(var row = 0; row < 3; row++) {
  for (var col = 0; col < 3; col++) {
    total += keys[row][col] ;
    vjo.sysout.println('total: ' + total) ;
  }
}

console> total: 45 

We can define an Object Literal which defines characteristics of a CPU.  We scope this definition in a VJO o-type called MachineInfo.

// This is a VJO o-type.  O-types allow us to define types for Javascript constructs
// such as object literals.
// In this example, arrs.MachineInfo is just a scoping name.
vjo.otype('arrs.MachineInfo') //< public
.defs({
	cpu: {
		vendor: null,	//< public String
		cores: null, 	//< public int
		clock: null 	//< public Number
	}
})
.endType();

We can then define an Array of CPU's.

//< needs(arrs.MachineInfo)
 
var amd = { 	//< MachineInfo.cpu
    clock: 2.8,
    cores: 4,
    vendor: 'AMD'
};
 
// The order of elements in the Object Literal does not change the type contract.
var intel = { 	//< MachineInfo.cpu
    cores: 8,
    clock: 3.1,
    vendor: 'INTEL'
};
 
// VJET "knows" this is a uniform array of Machine.cpu's so it auto-declares
// for us.  We can see what the declaration would look like since we use it in the
// following function overClock(...)
var cpews = [amd, intel] ;
 
//> void overClock(MachineInfo.cpu[ ] cpus)
function overClock(cpus) {
	for(var i = 0; i < cpus.length; i++) {
		cpus[i].clock += 1.1 ;
	}
}
 
vjo.sysout.println(amd.clock) ; // outputs 2.8
 
// Wind them up!
overClock(cpews) ;
 
vjo.sysout.println(amd.clock) ; // outputs 3.9

console>
 2.8
 3.9

We can have arrays of Javascript native types and VJET types. We can even have arrays of Functions, Typed-Functions and Type-References.

Back to the top