소스 검색

added "pred" to "count"

Nicolas Cannasse 15 년 전
부모
커밋
5c20689134
1개의 변경된 파일9개의 추가작업 그리고 4개의 파일을 삭제
  1. 9 4
      std/Lambda.hx

+ 9 - 4
std/Lambda.hx

@@ -140,12 +140,17 @@ class Lambda {
 	}
 
 	/**
-		Count the number of elements in an [Iterable]
+		Count the number of elements in an [Iterable] having [pred] returning true.
 	**/
-	public static function count<A>( it : Iterable<A> ) {
+	public static function count<A>( it : Iterable<A>, ?pred : A -> Bool ) {
 		var n = 0;
-		for( _ in it )
-			++n;
+		if( pred == null )
+			for( _ in it )
+				n++;
+		else
+			for( x in it )
+				if( pred(x) )
+					n++;
 		return n;
 	}