Ver Fonte

added Iterable, changed Lambda.

Nicolas Cannasse há 19 anos atrás
pai
commit
97bf617bbd
3 ficheiros alterados com 76 adições e 25 exclusões
  1. 66 23
      std/Lambda.hx
  2. 8 0
      std/StdTypes.hx
  3. 2 2
      std/haxe/rtti/XmlParser.hx

+ 66 - 23
std/Lambda.hx

@@ -30,9 +30,9 @@
 class Lambda {
 
 	/**
-		Creates an [Array] from an [Iterator]
+		Creates an [Array] from an [Iterable]
 	**/
-	public static function array<A>( it : Iterator<A> ) : Array<A> {
+	public static function array<A>( it : Iterable<A> ) : Array<A> {
 		var a = new Array<A>();
 		for(i in it)
 			a.push(i);
@@ -40,9 +40,9 @@ class Lambda {
 	}
 
 	/**
-		Creates a [List] from an [Iterator]
+		Creates a [List] from an [Iterable]
 	**/
-	public static function list<A>( it : Iterator<A> ) : List<A> {
+	public static function list<A>( it : Iterable<A> ) : List<A> {
 		var l = new List<A>();
 		for(i in it)
 			l.add(i);
@@ -50,52 +50,95 @@ class Lambda {
 	}
 
 	/**
-		Creates a new [Iterator] that will apply the function 'f' to all
+		Creates a new [Iterable] by appling the function 'f' to all
 		elements of the iterator 'it'.
 	**/
-	public static function map<A,B>( it : Iterator<A>, f : A -> B ) : Iterator<B> {
-		return {
-			hasNext : it.hasNext,
-			next : function() { return f(it.next()); }
+	public static function map<A,B>( it : Iterable<A>, f : A -> B ) : List<B> {
+		var l = new List<B>();
+		for( x in it )
+			l.add(f(x));
+		return l;
+	}
+
+	/**
+		Tells if the element is part of an iterable
+	**/
+	public static function has<A>( it : Iterable<A>, elt : A, ?cmp : A -> A -> Bool ) : Bool {
+		if( cmp == null ) {
+			for( x in it )
+				if( x == elt )
+					return true;
+		} else {
+			for( x in it )
+				if( cmp(x,elt) )
+					return true;
 		}
+		return false;
 	}
 
 	/**
-		Call the function 'f' on all elements of the [Iterator] 'it'.
+		Tells if at least one element of the iterable if found by using the specific function.
 	**/
-	public static function iter<A>( it : Iterator<A>, f : A -> Void ) {
+	public static function exists<A>( it : Iterable<A>, f : A -> Bool ) {
+		for( x in it )
+			if( f(x) )
+				return true;
+		return false;
+	}
+
+	/**
+		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 ) {
+		for( x in it )
+			if( !f(x) )
+				return false;
+		return true;
+	}
+
+	/**
+		Call the function 'f' on all elements of the [Iterable] 'it'.
+	**/
+	public static function iter<A>( it : Iterable<A>, f : A -> Void ) {
 		for( x in it )
 			f(x);
 	}
 
 	/**
-		Creates an [Array] from an [Array] 'a' by applying the function 'f'
-		on all elements of 'a'.
+		Return the list of elements matching the function 'f'
 	**/
-	public static function amap<A,B>(a : Array<A>,f : A -> B) : Array<B> {
-		var b = new Array();
-		for( x in a )
-			b.push(f(x));
-		return b;
+	public static function filter<A>( it : Iterable<A>, f : A -> Bool ) {
+		var l = new List<A>();
+		for( x in it )
+			if( f(x) )
+				l.add(x);
+		return l;
 	}
 
 	/**
-		Functional 'fold' using an [Iterator]
+		Functional 'fold' using an [Iterable]
 	**/
-	public static function fold<A,B>( it : Iterator<A>, f : A -> B -> B, first : B ) : B {
+	public static function fold<A,B>( it : Iterable<A>, f : A -> B -> B, first : B ) : B {
 		for( x in it )
 			first = f(x,first);
 		return first;
 	}
 
 	/**
-		Count the number of elements in an [Iterator]
+		Count the number of elements in an [Iterable]
 	**/
-	public static function count<A>( it : Iterator<A> ) {
+	public static function count<A>( it : Iterable<A> ) {
 		var n = 0;
 		for( _ in it )
 			++n;
 		return n;
 	}
 
-}
+	/**
+		Tells if an iterable does not contain any element.
+	**/
+	public static function empty( it : Iterable<Dynamic> ) : Bool {
+		return !it.iterator().hasNext();
+	}
+
+}

+ 8 - 0
std/StdTypes.hx

@@ -73,6 +73,14 @@ typedef Iterator<T> = {
 	function next() : T;
 }
 
+/**
+	An Iterable is a data structure which has an iterator() method.
+	See [Lambda] for generic functions on iterable structures.
+**/
+typedef Iterable<T> = {
+	function iterator() : Iterator<T>;
+}
+
 /**
 	ArrayAccess is used to indicate a class that can be accessed using brackets.
 	The type parameter represent the type of the elements stored.

+ 2 - 2
std/haxe/rtti/XmlParser.hx

@@ -38,7 +38,7 @@ class XmlParser {
 	}
 
 	function sortFields(fl) {
-		var a = Lambda.array(fl.iterator());
+		var a = Lambda.array(fl);
 		a.sort(function(f1 : ClassField,f2 : ClassField) {
 			var v1 = TypeApi.isVar(f1.type);
 			var v2 = TypeApi.isVar(f2.type);
@@ -54,7 +54,7 @@ class XmlParser {
 				return 1;
 			return -1;
 		});
-		return Lambda.list(a.iterator());
+		return Lambda.list(a);
 	}
 
 	public function process( x : Xml, platform ) {