Lambda.hx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /*
  2. * Copyright (c) 2005, The haXe Project Contributors
  3. * All rights reserved.
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions are met:
  6. *
  7. * - Redistributions of source code must retain the above copyright
  8. * notice, this list of conditions and the following disclaimer.
  9. * - Redistributions in binary form must reproduce the above copyright
  10. * notice, this list of conditions and the following disclaimer in the
  11. * documentation and/or other materials provided with the distribution.
  12. *
  13. * THIS SOFTWARE IS PROVIDED BY THE HAXE PROJECT CONTRIBUTORS "AS IS" AND ANY
  14. * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  15. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  16. * DISCLAIMED. IN NO EVENT SHALL THE HAXE PROJECT CONTRIBUTORS BE LIABLE FOR
  17. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  18. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  19. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  20. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  21. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  22. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  23. * DAMAGE.
  24. */
  25. /**
  26. The [Lambda] class is a collection of functional methods in order to
  27. use functional-style programming with haXe.
  28. **/
  29. class Lambda {
  30. /**
  31. Creates an [Array] from an [Iterable]
  32. **/
  33. public static function array<A>( it : Iterable<A> ) : Array<A> {
  34. var a = new Array<A>();
  35. for(i in it)
  36. a.push(i);
  37. return a;
  38. }
  39. /**
  40. Creates a [List] from an [Iterable]
  41. **/
  42. public static function list<A>( it : Iterable<A> ) : List<A> {
  43. var l = new List<A>();
  44. for(i in it)
  45. l.add(i);
  46. return l;
  47. }
  48. /**
  49. Creates a new [Iterable] by appling the function 'f' to all
  50. elements of the iterator 'it'.
  51. **/
  52. public static function map<A,B>( it : Iterable<A>, f : A -> B ) : List<B> {
  53. var l = new List<B>();
  54. for( x in it )
  55. l.add(f(x));
  56. return l;
  57. }
  58. /**
  59. Similar to [map], but also pass an index for each item iterated.
  60. **/
  61. public static function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : List<B> {
  62. var l = new List<B>();
  63. var i = 0;
  64. for( x in it )
  65. l.add(f(i++,x));
  66. return l;
  67. }
  68. /**
  69. Tells if the element is part of an iterable. The comparison
  70. is made using the [==] operator. Optionally you can pass as
  71. a third parameter a function that performs the comparison.
  72. That function must take as arguments the two items to
  73. compare and returns a boolean value.
  74. **/
  75. public static function has<A>( it : Iterable<A>, elt : A, ?cmp : A -> A -> Bool ) : Bool {
  76. if( cmp == null ) {
  77. for( x in it )
  78. if( x == elt )
  79. return true;
  80. } else {
  81. for( x in it )
  82. if( cmp(x,elt) )
  83. return true;
  84. }
  85. return false;
  86. }
  87. /**
  88. Tells if at least one element of the iterable is found by using the specific function.
  89. **/
  90. public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
  91. for( x in it )
  92. if( f(x) )
  93. return true;
  94. return false;
  95. }
  96. /**
  97. Tells if all elements of the iterable have the specified property defined by [f].
  98. **/
  99. public static function foreach<A>( it : Iterable<A>, f : A -> Bool ) {
  100. for( x in it )
  101. if( !f(x) )
  102. return false;
  103. return true;
  104. }
  105. /**
  106. Call the function 'f' on all elements of the [Iterable] 'it'.
  107. **/
  108. public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
  109. for( x in it )
  110. f(x);
  111. }
  112. /**
  113. Return the list of elements matching the function 'f'
  114. **/
  115. public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
  116. var l = new List<A>();
  117. for( x in it )
  118. if( f(x) )
  119. l.add(x);
  120. return l;
  121. }
  122. /**
  123. Functional 'fold' using an [Iterable]
  124. **/
  125. public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
  126. for( x in it )
  127. first = f(x,first);
  128. return first;
  129. }
  130. /**
  131. Count the number of elements in an [Iterable] having [pred] returning true.
  132. **/
  133. public static function count<A>( it : Iterable<A>, ?pred : A -> Bool ) {
  134. var n = 0;
  135. if( pred == null )
  136. for( _ in it )
  137. n++;
  138. else
  139. for( x in it )
  140. if( pred(x) )
  141. n++;
  142. return n;
  143. }
  144. /**
  145. Tells if an iterable does not contain any element.
  146. **/
  147. public static function empty( it : Iterable<Dynamic> ) : Bool {
  148. return !it.iterator().hasNext();
  149. }
  150. /**
  151. Returns the index of the item in the given Iterable, depending on the order of the Iterator.
  152. Returns -1 if the item was not found.
  153. **/
  154. public static function indexOf<T>( it : Iterable<T>, v : T ) : Int {
  155. var i = 0;
  156. for( v2 in it ) {
  157. if( v == v2 )
  158. return i;
  159. i++;
  160. }
  161. return -1;
  162. }
  163. /**
  164. Returns a list containing all items of 'a' followed by all items of 'b'
  165. **/
  166. public static function concat<T>( a : Iterable<T>, b : Iterable<T> ) : List<T> {
  167. var l = new List();
  168. for( x in a )
  169. l.add(x);
  170. for( x in b )
  171. l.add(x);
  172. return l;
  173. }
  174. }