Lambda.hx 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (C)2005-2017 Haxe Foundation
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a
  5. * copy of this software and associated documentation files (the "Software"),
  6. * to deal in the Software without restriction, including without limitation
  7. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. * and/or sell copies of the Software, and to permit persons to whom the
  9. * Software is furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. * DEALINGS IN THE SOFTWARE.
  21. */
  22. /**
  23. The `Lambda` class is a collection of methods to support functional
  24. programming. It is ideally used with `using Lambda` and then acts as an
  25. extension to Iterable types.
  26. On static platforms, working with the Iterable structure might be slower
  27. than performing the operations directly on known types, such as Array and
  28. List.
  29. If the first argument to any of the methods is null, the result is
  30. unspecified.
  31. @see https://haxe.org/manual/std-Lambda.html
  32. **/
  33. class Lambda {
  34. /**
  35. Creates an Array from Iterable `it`.
  36. If `it` is an Array, this function returns a copy of it.
  37. **/
  38. public static function array<A>( it : Iterable<A> ) : Array<A> {
  39. var a = new Array<A>();
  40. for(i in it)
  41. a.push(i);
  42. return a;
  43. }
  44. /**
  45. Creates a List form Iterable `it`.
  46. If `it` is a List, this function returns a copy of it.
  47. **/
  48. public static function list<A>( it : Iterable<A> ) : List<A> {
  49. var l = new List<A>();
  50. for(i in it)
  51. l.add(i);
  52. return l;
  53. }
  54. /**
  55. Creates a new List by applying function `f` to all elements of `it`.
  56. The order of elements is preserved.
  57. If `f` is null, the result is unspecified.
  58. **/
  59. public static function map<A,B>( it : Iterable<A>, f : A -> B ) : List<B> {
  60. var l = new List<B>();
  61. for( x in it )
  62. l.add(f(x));
  63. return l;
  64. }
  65. /**
  66. Similar to map, but also passes the index of each element to `f`.
  67. The order of elements is preserved.
  68. If `f` is null, the result is unspecified.
  69. **/
  70. public static function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : List<B> {
  71. var l = new List<B>();
  72. var i = 0;
  73. for( x in it )
  74. l.add(f(i++,x));
  75. return l;
  76. }
  77. /**
  78. Concatenate a list of lists.
  79. The order of elements is preserved.
  80. **/
  81. public static function flatten<A>( it : Iterable<Iterable<A>> ) : List<A> {
  82. var l = new List<A>();
  83. for (e in it)
  84. for (x in e)
  85. l.add(x);
  86. return l;
  87. }
  88. /**
  89. A composition of map and flatten.
  90. The order of elements is preserved.
  91. If `f` is null, the result is unspecified.
  92. **/
  93. public static function flatMap<A,B>( it : Iterable<A>, f: A -> Iterable<B> ) : List<B> {
  94. return Lambda.flatten(Lambda.map(it, f));
  95. }
  96. /**
  97. Tells if `it` contains `elt`.
  98. This function returns true as soon as an element is found which is equal
  99. to `elt` according to the `==` operator.
  100. If no such element is found, the result is false.
  101. **/
  102. public static function has<A>( it : Iterable<A>, elt : A ) : Bool {
  103. for( x in it )
  104. if( x == elt )
  105. return true;
  106. return false;
  107. }
  108. /**
  109. Tells if `it` contains an element for which `f` is true.
  110. This function returns true as soon as an element is found for which a
  111. call to `f` returns true.
  112. If no such element is found, the result is false.
  113. If `f` is null, the result is unspecified.
  114. **/
  115. public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
  116. for( x in it )
  117. if( f(x) )
  118. return true;
  119. return false;
  120. }
  121. /**
  122. Tells if `f` is true for all elements of `it`.
  123. This function returns false as soon as an element is found for which a
  124. call to `f` returns false.
  125. If no such element is found, the result is true.
  126. In particular, this function always returns true if `it` is empty.
  127. If `f` is null, the result is unspecified.
  128. **/
  129. public static function foreach<A>( it : Iterable<A>, f : A -> Bool ) {
  130. for( x in it )
  131. if( !f(x) )
  132. return false;
  133. return true;
  134. }
  135. /**
  136. Calls `f` on all elements of `it`, in order.
  137. If `f` is null, the result is unspecified.
  138. **/
  139. public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
  140. for( x in it )
  141. f(x);
  142. }
  143. /**
  144. Returns a List containing those elements of `it` for which `f` returned
  145. true.
  146. If `it` is empty, the result is the empty List even if `f` is null.
  147. Otherwise if `f` is null, the result is unspecified.
  148. **/
  149. public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
  150. var l = new List<A>();
  151. for( x in it )
  152. if( f(x) )
  153. l.add(x);
  154. return l;
  155. }
  156. /**
  157. Functional fold on Iterable `it`, using function `f` with start argument
  158. `first`.
  159. If `it` has no elements, the result is `first`.
  160. Otherwise the first element of `it` is passed to `f` alongside `first`.
  161. The result of that call is then passed to `f` with the next element of
  162. `it`, and so on until `it` has no more elements.
  163. If `it` or `f` are null, the result is unspecified.
  164. **/
  165. public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
  166. for( x in it )
  167. first = f(x,first);
  168. return first;
  169. }
  170. /**
  171. Returns the number of elements in `it` for which `pred` is true, or the
  172. total number of elements in `it` if `pred` is null.
  173. This function traverses all elements.
  174. **/
  175. public static function count<A>( it : Iterable<A>, ?pred : A -> Bool ) {
  176. var n = 0;
  177. if( pred == null )
  178. for( _ in it )
  179. n++;
  180. else
  181. for( x in it )
  182. if( pred(x) )
  183. n++;
  184. return n;
  185. }
  186. /**
  187. Tells if Iterable `it` does not contain any element.
  188. **/
  189. public static function empty<T>( it : Iterable<T> ) : Bool {
  190. return !it.iterator().hasNext();
  191. }
  192. /**
  193. Returns the index of the first element `v` within Iterable `it`.
  194. This function uses operator `==` to check for equality.
  195. If `v` does not exist in `it`, the result is -1.
  196. **/
  197. public static function indexOf<T>( it : Iterable<T>, v : T ) : Int {
  198. var i = 0;
  199. for( v2 in it ) {
  200. if( v == v2 )
  201. return i;
  202. i++;
  203. }
  204. return -1;
  205. }
  206. /**
  207. Returns the first element of `it` for which `f` is true.
  208. This function returns as soon as an element is found for which a call to
  209. `f` returns true.
  210. If no such element is found, the result is null.
  211. If `f` is null, the result is unspecified.
  212. **/
  213. public static function find<T>( it : Iterable<T>, f : T -> Bool ) : Null<T> {
  214. for( v in it ) {
  215. if(f(v)) return v;
  216. }
  217. return null;
  218. }
  219. /**
  220. Returns a new List containing all elements of Iterable `a` followed by
  221. all elements of Iterable `b`.
  222. If `a` or `b` are null, the result is unspecified.
  223. **/
  224. public static function concat<T>( a : Iterable<T>, b : Iterable<T> ) : List<T> {
  225. var l = new List();
  226. for( x in a )
  227. l.add(x);
  228. for( x in b )
  229. l.add(x);
  230. return l;
  231. }
  232. }