Lambda.hx 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  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
  70. **/
  71. public static function has<A>( it : Iterable<A>, elt : A, ?cmp : A -> A -> Bool ) : Bool {
  72. if( cmp == null ) {
  73. for( x in it )
  74. if( x == elt )
  75. return true;
  76. } else {
  77. for( x in it )
  78. if( cmp(x,elt) )
  79. return true;
  80. }
  81. return false;
  82. }
  83. /**
  84. Tells if at least one element of the iterable if found by using the specific function.
  85. **/
  86. public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
  87. for( x in it )
  88. if( f(x) )
  89. return true;
  90. return false;
  91. }
  92. /**
  93. Tells if all elements of the iterable have the specified property defined by [f].
  94. **/
  95. public static function foreach<A>( it : Iterable<A>, f : A -> Bool ) {
  96. for( x in it )
  97. if( !f(x) )
  98. return false;
  99. return true;
  100. }
  101. /**
  102. Call the function 'f' on all elements of the [Iterable] 'it'.
  103. **/
  104. public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
  105. for( x in it )
  106. f(x);
  107. }
  108. /**
  109. Return the list of elements matching the function 'f'
  110. **/
  111. public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
  112. var l = new List<A>();
  113. for( x in it )
  114. if( f(x) )
  115. l.add(x);
  116. return l;
  117. }
  118. /**
  119. Functional 'fold' using an [Iterable]
  120. **/
  121. public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
  122. for( x in it )
  123. first = f(x,first);
  124. return first;
  125. }
  126. /**
  127. Count the number of elements in an [Iterable]
  128. **/
  129. public static function count<A>( it : Iterable<A> ) {
  130. var n = 0;
  131. for( _ in it )
  132. ++n;
  133. return n;
  134. }
  135. /**
  136. Tells if an iterable does not contain any element.
  137. **/
  138. public static function empty( it : Iterable<Dynamic> ) : Bool {
  139. return !it.iterator().hasNext();
  140. }
  141. }