R-simple.ecl 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. IMPORT R;
  2. /*
  3. This example illustrates and tests the use of embedded JavaScript
  4. */
  5. // Scalar parameters and resuls
  6. integer add1(integer VAL) := EMBED(R)
  7. VAL+1
  8. ENDEMBED;
  9. string cat(varstring what, string who) := EMBED(R)
  10. paste(what,who)
  11. ENDEMBED;
  12. data testData(data val) := EMBED(R)
  13. val[1] = val[2];
  14. val;
  15. ENDEMBED;
  16. // Sets
  17. set of integer testSet(set of integer val) := EMBED(R)
  18. t = val [1];
  19. val[1] = val[2];
  20. val[2] = t;
  21. val;
  22. ENDEMBED;
  23. set of unsigned2 testSet0(set of unsigned2 val) := EMBED(R)
  24. sort(val);
  25. ENDEMBED;
  26. set of string testSet2(set of string val) := EMBED(R)
  27. t = val [1];
  28. val[1] = val[2];
  29. val[2] = t;
  30. val;
  31. ENDEMBED;
  32. set of string testSet3(set of string8 val) := EMBED(R)
  33. t = val [1];
  34. val[1] = val[2];
  35. val[2] = t;
  36. val;
  37. ENDEMBED;
  38. set of varstring testSet4(set of varstring val) := EMBED(R)
  39. t = val [1];
  40. val[1] = val[2];
  41. val[2] = t;
  42. val;
  43. ENDEMBED;
  44. set of varstring8 testSet5(set of varstring8 val) := EMBED(R)
  45. t = val [1];
  46. val[1] = val[2];
  47. val[2] = t;
  48. val;
  49. ENDEMBED;
  50. set of boolean testSet6(set of boolean val) := EMBED(R)
  51. t = val [1];
  52. val[1] = val[2];
  53. val[2] = t;
  54. val;
  55. ENDEMBED;
  56. set of real4 testSet7(set of real4 val) := EMBED(R)
  57. t = val [1];
  58. val[1] = val[2];
  59. val[2] = t;
  60. val;
  61. ENDEMBED;
  62. set of real8 testSet8(set of real8 val) := EMBED(R)
  63. t = val [1];
  64. val[1] = val[2];
  65. val[2] = t;
  66. val;
  67. ENDEMBED;
  68. set of integer2 testSet9(set of integer2 val) := EMBED(R)
  69. sort(val);
  70. ENDEMBED;
  71. // datasets - fields are mapped by name
  72. mtcarsrec := RECORD
  73. real8 mpg;
  74. unsigned1 cyl;
  75. real8 disp;
  76. unsigned2 hp;
  77. real8 drat;
  78. real8 wt;
  79. real8 qsec;
  80. boolean vs;
  81. boolean am;
  82. unsigned1 gear;
  83. unsigned1 carb;
  84. END;
  85. // returning a dataset
  86. DATASET(mtcarsrec) testDsOut() := EMBED(R)
  87. mtcars;
  88. ENDEMBED;
  89. // returning a record
  90. mtcarsrec testRecordOut() := EMBED(R)
  91. mtcars[t,];
  92. ENDEMBED;
  93. mtcarsrec testRecordOut2() := EMBED(R)
  94. list( 1,2,3,4,5,6,7,1,0,11,12);
  95. ENDEMBED;
  96. // Dataset parameters
  97. DATASET(mtcarsrec) testDsIn(DATASET(mtcarsrec) l) := EMBED(R)
  98. l;
  99. ENDEMBED;
  100. // Now do the tests
  101. add1(10);
  102. cat('Hello', 'World');
  103. testData(D'ab');
  104. testSet([1,2,3]);
  105. testSet0([30000,40000,50000]);
  106. testSet2(['one','two','three']);
  107. testSet3(['uno','dos','tre']);
  108. testSet4(['un','deux','trois']);
  109. testSet5(['ein','zwei','drei']);
  110. testSet6([false,true,false,true]);
  111. testSet7([1.1,2.2,3.3]);
  112. testSet8([1.2,2.3,3.4]);
  113. testSet9([-111,0,113]);
  114. testDsOut();
  115. testRecordOut();
  116. testRecordOut2();
  117. testDsIn(SORT(testDsOut(), mpg));
  118. // Test some performance and multithreading - the + operation on datasets executes the two sides in parallel
  119. s1 :=DATASET(250000, TRANSFORM({ integer a }, SELF.a := add1(COUNTER)));
  120. s2 :=DATASET(250000, TRANSFORM({ integer a }, SELF.a := add1(COUNTER/2)));
  121. SUM(NOFOLD(s1 + s2), a);
  122. // For comparison, these versions do comparable operations but using pure ECL, to give an idea of the overhead
  123. s1b :=DATASET(250000, TRANSFORM({ integer a }, SELF.a := COUNTER+1));
  124. s2b :=DATASET(250000, TRANSFORM({ integer a }, SELF.a := (COUNTER/2)+1));
  125. SUM(NOFOLD(s1b + s2b), a);