Преглед изворни кода

Merge pull request #3553 from m22spencer/patch-1

Add flatten and flatMap to Lambda.hx
Simon Krajewski пре 10 година
родитељ
комит
37329a5d6e
1 измењених фајлова са 24 додато и 0 уклоњено
  1. 24 0
      std/Lambda.hx

+ 24 - 0
std/Lambda.hx

@@ -86,6 +86,30 @@ class Lambda {
 			l.add(f(i++,x));
 		return l;
 	}
+	
+	/** 
+		Concatenate a list of lists.
+		
+		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;
+	}
+	
+	/** 
+		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> {
+		return Lambda.flatten(Lambda.map(it, f));
+	}
 
 	/**
 		Tells if `it` contains `elt`.