소스 검색

[std] make Lambda functions return Array instead of List

closes #7097
Simon Krajewski 7 년 전
부모
커밋
adcb432722
3개의 변경된 파일27개의 추가작업 그리고 46개의 파일을 삭제
  1. 4 0
      extra/CHANGES.txt
  2. 19 42
      std/Lambda.hx
  3. 4 4
      tests/unit/src/unitstd/Lambda.unit.hx

+ 4 - 0
extra/CHANGES.txt

@@ -21,6 +21,10 @@ XXXX-XX-XX:
 	php : Escape `$` in field names of anonymous objects (#7230)
 	php : Generate `switch` as `if...else if...else...` to avoid loose comparison (#7257)
 
+	Standard Library:
+
+	all : [breaking] made Lambda functions return Array instead of List (#7097)
+
 2018-06-12: 4.0.0-preview.4
 
 	New features:

+ 19 - 42
std/Lambda.hx

@@ -37,7 +37,7 @@ import haxe.ds.List;
 **/
 
 class Lambda {
-	
+
 
 	/**
 		Creates an Array from Iterable `it`.
@@ -64,55 +64,38 @@ class Lambda {
 	}
 
 	/**
-		Creates a new List by applying function `f` to all elements of `it`.
-
+		Creates a new Array by applying function `f` to all elements of `it`.
 		The order of elements is preserved.
-
 		If `f` is null, the result is unspecified.
 	**/
-	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;
+	public static inline function map<A,B>( it : Iterable<A>, f : A -> B ) : Array<B> {
+		return [for (x in it) f(x)];
 	}
 
 	/**
 		Similar to map, but also passes the index of each element to `f`.
-
 		The order of elements is preserved.
-
 		If `f` is null, the result is unspecified.
 	**/
-	public static function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : List<B> {
-		var l = new List<B>();
+	public static inline function mapi<A,B>( it : Iterable<A>, f : Int -> A -> B ) : Array<B> {
 		var i = 0;
-		for( x in it )
-			l.add(f(i++,x));
-		return l;
+		return [for (x in it) f(i++, x)];
 	}
 
 	/**
-		Concatenate a list of lists.
-
+		Concatenate a list of iterables.
 		The order of elements is preserved.
 	**/
-	public static function flatten<A>( it : Iterable<Iterable<A>> ) : List<A> {
-		var l = new List<A>();
-		for (e in it)
-			for (x in e)
-				l.add(x);
-		return l;
+	public static inline function flatten<A>( it : Iterable<Iterable<A>> ) : Array<A> {
+		return [for (e in it) for (x in e) x];
 	}
 
 	/**
 		A composition of map and flatten.
-
 		The order of elements is preserved.
-
 		If `f` is null, the result is unspecified.
 	**/
-	public static function flatMap<A,B>( it : Iterable<A>, f: A -> Iterable<B> ) : List<B> {
+	public static inline function flatMap<A,B>( it : Iterable<A>, f: A -> Iterable<B> ) : Array<B> {
 		return Lambda.flatten(Lambda.map(it, f));
 	}
 
@@ -178,19 +161,13 @@ class Lambda {
 	}
 
 	/**
-		Returns a List containing those elements of `it` for which `f` returned
+		Returns a Array containing those elements of `it` for which `f` returned
 		true.
-
-		If `it` is empty, the result is the empty List even if `f` is null.
-
+		If `it` is empty, the result is the empty Array even if `f` is null.
 		Otherwise if `f` is null, the result is unspecified.
 	**/
-	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;
+	public static inline function filter<A>( it : Array<A>, f : A -> Bool ) {
+		return [for (x in it) if (f(x)) x];
 	}
 
 	/**
@@ -271,17 +248,17 @@ class Lambda {
 	}
 
 	/**
-		Returns a new List containing all elements of Iterable `a` followed by
+		Returns a new Array containing all elements of Iterable `a` followed by
 		all elements of Iterable `b`.
 
 		If `a` or `b` are null, the result is unspecified.
 	**/
-	public static function concat<T>( a : Iterable<T>, b : Iterable<T> ) : List<T> {
-		var l = new List();
+	public static function concat<T>( a : Iterable<T>, b : Iterable<T> ) : Array<T> {
+		var l = new Array();
 		for( x in a )
-			l.add(x);
+			l.push(x);
 		for( x in b )
-			l.add(x);
+			l.push(x);
 		return l;
 	}
 

+ 4 - 4
tests/unit/src/unitstd/Lambda.unit.hx

@@ -31,9 +31,9 @@ e2.length == 0;
 var a = [1, 2, 3];
 var b = Lambda.map(a,function(i) return i * 2);
 b.length == 3;
-b.pop() == 2;
-b.pop() == 4;
 b.pop() == 6;
+b.pop() == 4;
+b.pop() == 2;
 
 // mapi
 var a = [1, 2, 3];
@@ -45,9 +45,9 @@ function myMap(index, i) {
 var b = Lambda.mapi(a, myMap);
 total == 3;
 b.length == 3;
-b.pop() == 2;
-b.pop() == 4;
 b.pop() == 6;
+b.pop() == 4;
+b.pop() == 2;
 
 // has
 Lambda.has([1,2,3],1) == true;