Browse Source

- added check for nullity in Lambda methods (php)

Franco Ponticelli 16 years ago
parent
commit
a9be99bf68
1 changed files with 36 additions and 0 deletions
  1. 36 0
      std/Lambda.hx

+ 36 - 0
std/Lambda.hx

@@ -33,6 +33,9 @@ class Lambda {
 		Creates an [Array] from an [Iterable]
 		Creates an [Array] from an [Iterable]
 	**/
 	**/
 	public static function array<A>( it : Iterable<A> ) : Array<A> {
 	public static function array<A>( it : Iterable<A> ) : Array<A> {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		var a = new Array<A>();
 		var a = new Array<A>();
 		for(i in it)
 		for(i in it)
 			a.push(i);
 			a.push(i);
@@ -43,6 +46,9 @@ class Lambda {
 		Creates a [List] from an [Iterable]
 		Creates a [List] from an [Iterable]
 	**/
 	**/
 	public static function list<A>( it : Iterable<A> ) : List<A> {
 	public static function list<A>( it : Iterable<A> ) : List<A> {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		var l = new List<A>();
 		var l = new List<A>();
 		for(i in it)
 		for(i in it)
 			l.add(i);
 			l.add(i);
@@ -54,6 +60,9 @@ class Lambda {
 		elements of the iterator 'it'.
 		elements of the iterator 'it'.
 	**/
 	**/
 	public static function map<A,B>( it : Iterable<A>, f : A -> B ) : List<B> {
 	public static function map<A,B>( it : Iterable<A>, f : A -> B ) : List<B> {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		var l = new List<B>();
 		var l = new List<B>();
 		for( x in it )
 		for( x in it )
 			l.add(f(x));
 			l.add(f(x));
@@ -64,6 +73,9 @@ class Lambda {
 		Similar to [map], but also pass an index for each item iterated.
 		Similar to [map], but also pass an index for each item iterated.
 	**/
 	**/
 	public static function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : List<B> {
 	public static function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : List<B> {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		var l = new List<B>();
 		var l = new List<B>();
 		var i = 0;
 		var i = 0;
 		for( x in it )
 		for( x in it )
@@ -79,6 +91,9 @@ class Lambda {
 		compare and returns a boolean value.
 		compare and returns a boolean value.
 	**/
 	**/
 	public static function has<A>( it : Iterable<A>, elt : A, ?cmp : A -> A -> Bool ) : Bool {
 	public static function has<A>( it : Iterable<A>, elt : A, ?cmp : A -> A -> Bool ) : Bool {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		if( cmp == null ) {
 		if( cmp == null ) {
 			for( x in it )
 			for( x in it )
 				if( x == elt )
 				if( x == elt )
@@ -95,6 +110,9 @@ class Lambda {
 		Tells if at least one element of the iterable is found by using the specific function.
 		Tells if at least one element of the iterable is found by using the specific function.
 	**/
 	**/
 	public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
 	public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		for( x in it )
 		for( x in it )
 			if( f(x) )
 			if( f(x) )
 				return true;
 				return true;
@@ -105,6 +123,9 @@ class Lambda {
 		Tells if all elements of the iterable have the specified property defined by [f].
 		Tells if all elements of the iterable have the specified property defined by [f].
 	**/
 	**/
 	public static function foreach<A>( it : Iterable<A>, f : A -> Bool ) {
 	public static function foreach<A>( it : Iterable<A>, f : A -> Bool ) {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		for( x in it )
 		for( x in it )
 			if( !f(x) )
 			if( !f(x) )
 				return false;
 				return false;
@@ -115,6 +136,9 @@ class Lambda {
 		Call the function 'f' on all elements of the [Iterable] 'it'.
 		Call the function 'f' on all elements of the [Iterable] 'it'.
 	**/
 	**/
 	public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
 	public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		for( x in it )
 		for( x in it )
 			f(x);
 			f(x);
 	}
 	}
@@ -123,6 +147,9 @@ class Lambda {
 		Return the list of elements matching the function 'f'
 		Return the list of elements matching the function 'f'
 	**/
 	**/
 	public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
 	public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		var l = new List<A>();
 		var l = new List<A>();
 		for( x in it )
 		for( x in it )
 			if( f(x) )
 			if( f(x) )
@@ -134,6 +161,9 @@ class Lambda {
 		Functional 'fold' using an [Iterable]
 		Functional 'fold' using an [Iterable]
 	**/
 	**/
 	public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
 	public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		for( x in it )
 		for( x in it )
 			first = f(x,first);
 			first = f(x,first);
 		return first;
 		return first;
@@ -143,6 +173,9 @@ class Lambda {
 		Count the number of elements in an [Iterable]
 		Count the number of elements in an [Iterable]
 	**/
 	**/
 	public static function count<A>( it : Iterable<A> ) {
 	public static function count<A>( it : Iterable<A> ) {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		var n = 0;
 		var n = 0;
 		for( _ in it )
 		for( _ in it )
 			++n;
 			++n;
@@ -153,6 +186,9 @@ class Lambda {
 		Tells if an iterable does not contain any element.
 		Tells if an iterable does not contain any element.
 	**/
 	**/
 	public static function empty( it : Iterable<Dynamic> ) : Bool {
 	public static function empty( it : Iterable<Dynamic> ) : Bool {
+#if php
+		if (it == null) throw "null iterable";
+#end
 		return !it.iterator().hasNext();
 		return !it.iterator().hasNext();
 	}
 	}