瀏覽代碼

allow runtime calls to map and filter on structures and dynamic

frabbit 11 年之前
父節點
當前提交
64120f3f66
共有 3 個文件被更改,包括 18 次插入4 次删除
  1. 2 2
      genpy.ml
  2. 2 2
      std/python/_std/Array.hx
  3. 14 0
      std/python/internal/HxOverrides.hx

+ 2 - 2
genpy.ml

@@ -1339,7 +1339,7 @@ module Printer = struct
 		in
 		let call_override s =
 			match s with
-			| "iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift" | "join" | "push" -> true
+			| "iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift" | "join" | "push" | "map" | "filter" -> true
 			| _ -> false
 		in
 		match fa with
@@ -1504,7 +1504,7 @@ module Printer = struct
 
 	and print_call pctx e1 el =
 		match e1.eexpr, el with
-			| TField(e1,((FAnon {cf_name = (("join" | "push") as s)}) | FDynamic (("join" | "push") as s))), [x] ->
+			| TField(e1,((FAnon {cf_name = (("join" | "push" | "map" | "filter") as s)}) | FDynamic (("join" | "push" | "map" | "filter") as s))), [x] ->
 				Printf.sprintf "HxOverrides.%s(%s, %s)" s (print_expr pctx e1) (print_expr pctx x)
 			| TField(e1,((FAnon {cf_name = (("iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift") as s)}) | FDynamic (("iterator" | "toUpperCase" | "toLowerCase" | "pop" | "shift") as s))), [] ->
 				Printf.sprintf "HxOverrides.%s(%s)" s (print_expr pctx e1)

+ 2 - 2
std/python/_std/Array.hx

@@ -107,11 +107,11 @@ extern class Array<T> implements ArrayAccess<T> extends ArrayImpl {
 		return ArrayImpl.splice(this, pos, len);
 	}
 
-	public inline function map<S>( f : T -> S ) : Array<S> {
+	@:runtime public inline function map<S>( f : T -> S ) : Array<S> {
 		return ArrayImpl.map(this, f);
 	}
 
-	public inline function filter( f : T -> Bool ) : Array<T> {
+	@:runtime public inline function filter( f : T -> Bool ) : Array<T> {
 		return ArrayImpl.filter(this,f);
 	}
 

+ 14 - 0
std/python/internal/HxOverrides.hx

@@ -59,6 +59,20 @@ class HxOverrides {
 		return Syntax.callField(x, "join", sep);
 	}
 
+	static public function filter(x, f) {
+		if (Boot.isArray(x)) {
+			return (x:Array<Dynamic>).filter(f);
+		}
+		return Syntax.callField(x, "filter", f);
+	}
+
+	static public function map(x, f) {
+		if (Boot.isArray(x)) {
+			return (x:Array<Dynamic>).map(f);
+		}
+		return Syntax.callField(x, "map", f);
+	}
+
 	static public function toUpperCase(x) {
 		if (Boot.isString(x)) {
 			return (x:String).toUpperCase();