Contains standard libraries for ZenCode.
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

Arrays.zs 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. [Native("stdlib::Arrays")]
  2. export expand <T : Comparable<T>> T[] {
  3. [Native("sort")]
  4. public extern sort() as void;
  5. [Native("sorted")]
  6. public extern sorted() as T[];
  7. }
  8. export expand <T : Hashable<T>> T[] {
  9. public implements Hashable<T[]> {
  10. public extern hashCode() as int;
  11. public extern == (other as T) as bool;
  12. }
  13. }
  14. [Native("stdlib::Arrays")]
  15. export expand <T> T[] {
  16. [Native("sortWithComparator")]
  17. public extern sort(comparator as function(a as T, b as T) as int) as void;
  18. [Native("sortedWithComparator")]
  19. public extern sorted(comparator as function(a as T, b as T) as int) as T[];
  20. [Native("copy")]
  21. public extern copy() as T[];
  22. [Native("copyResize")]
  23. public extern copy(newSize as int) as T[];
  24. [Native("copyTo")]
  25. public extern copyTo(target as T[], sourceOffset as int, targetOffset as int, length as int) as void;
  26. public get first as T?
  27. => this.isEmpty ? null : this[0];
  28. public get last as T?
  29. => this.isEmpty ? null : this[$ - 1];
  30. [Native("reverse")]
  31. public reverse() as void {
  32. for i in 0 .. length / 2 {
  33. var temp = this[i];
  34. this[i] = this[length - i - 1];
  35. this[length - i - 1] = temp;
  36. }
  37. }
  38. // TODO: fix compilation for this
  39. /*[Native("reversed")]
  40. public reversed() as T[] {
  41. return new T[](this, (i, value) => this[length - i - 1]);
  42. }*/
  43. [Native("mapValues")]
  44. public map<U>(projection as function(value as T) as U) as U[] {
  45. return new U[]<T>(this, projection);
  46. }
  47. [Native("mapKeyValues")]
  48. public map<U>(projection as function(index as int, value as T) as U) as U[] {
  49. return new U[]<T>(this, projection);
  50. }
  51. [Native("filterValues")]
  52. public filter(predicate as function(value as T) as bool) as T[] {
  53. var values = new List<T>();
  54. for value in this
  55. if predicate(value)
  56. values.add(value);
  57. return values as T[];
  58. }
  59. [Native("filterKeyValues")]
  60. public filter(predicate as function(index as int, value as T) as bool) as T[] {
  61. var values = new List<T>();
  62. for i, value in this
  63. if predicate(i, value)
  64. values.add(value);
  65. return values as T[];
  66. }
  67. public each(consumer as function(value as T) as void) as void {
  68. for value in this
  69. consumer(value);
  70. }
  71. public each(consumer as function(index as int, value as T) as void) as void {
  72. for i, value in this
  73. consumer(i, value);
  74. }
  75. public contains(predicate as function(value as T) as bool) as bool {
  76. for value in this
  77. if predicate(value)
  78. return true;
  79. return false;
  80. }
  81. public contains(predicate as function(index as int, value as T) as bool) as bool {
  82. for i, value in this
  83. if predicate(i, value)
  84. return true;
  85. return false;
  86. }
  87. public all(predicate as function(value as T) as bool) as bool {
  88. for value in this
  89. if !predicate(value)
  90. return false;
  91. return true;
  92. }
  93. public all(predicate as function(i as int, value as T) as bool) as bool {
  94. for i, value in this
  95. if !predicate(i, value)
  96. return false;
  97. return true;
  98. }
  99. public first(predicate as function(value as T) as bool) as T? {
  100. for value in this
  101. if predicate(value)
  102. return value;
  103. return null;
  104. }
  105. public first(predicate as function(i as int, value as T) as bool) as T? {
  106. for i, value in this
  107. if predicate(i, value)
  108. return value;
  109. return null;
  110. }
  111. public last(predicate as function(value as T) as bool) as T? {
  112. var i = length;
  113. while i > 0 {
  114. i--;
  115. if predicate(this[i])
  116. return this[i];
  117. }
  118. return null;
  119. }
  120. public last(predicate as function(index as int, value as T) as bool) as T? {
  121. var i = length;
  122. while i > 0 {
  123. i--;
  124. if predicate(i, this[i])
  125. return this[i];
  126. }
  127. return null;
  128. }
  129. public count(predicate as function(value as T) as bool) as int {
  130. var result = 0;
  131. for value in this
  132. if predicate(value)
  133. result++;
  134. return result;
  135. }
  136. public count(predicate as function(index as int, value as T) as bool) as int {
  137. var result = 0;
  138. for i, value in this
  139. if predicate(i, value)
  140. result++;
  141. return result;
  142. }
  143. }