元编程demo中如何添加展示一列

请问一下各位,下面元编程例子中如何添加展示city列,在不调用format函数的基础上:

def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(makeCall{format},sqlCol(colNames),colFormat),colNames), tbl).eval()
t = table(1..100 as id, (1..100 + 2018.01.01) as date, rand(100.0, 100) as price, rand(10000, 100) as qty, rand(`A`B`C`D`E`F`G, 100) as city);
generateReport(t, ["id", "date","price","qty"], ["0", "MM/dd/yyyy", "0.00", "#,###"]);
请先 登录 后评论

2 个回答

wale

可以用join(<-)函数:

def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(makeCall{format},sqlCol(colNames),colFormat),colNames)<-sqlCol("city"), tbl).eval();

执行generateReport(t, ["id", "date","price","qty"], ["0", "MM/dd/yyyy", "0.00", "#,###"])结果:

id	date	price	qty	city
1	01/02/2018	70.86	518	E
2	01/03/2018	81.32	8,132	A
3	01/04/2018	88.08	9,599	C
4	01/05/2018	77.98	4,101	G
5	01/06/2018	16.44	9,191	F
6	01/07/2018	48.81	8,953	A
7	01/08/2018	36.65	459	E
8	01/09/2018	35.80	4,012	C
……

请先 登录 后评论
Polly

或者可以写一个通用的:

def myf(colName, ft){
if(isValid(ft)){
return makeCall(format, sqlCol(colName), ft)
}else{
return sqlCol(colName)
}
}
def generateReport(tbl, colNames, colFormat): sql(sqlColAlias(each(myf, colNames, colFormat),colNames), tbl).eval()

colNames=["id", "date","price","qty", "city"]
colFormat=["0", "MM/dd/yyyy", "0.00", "#,###", ""]
t = table(1..100 as id, (1..100 + 2018.01.01) as date, rand(100.0, 100) as price, rand(10000, 100) as qty, rand(`A`B`C`D`E`F`G, 100) as city);
generateReport(t, ["id", "date","price","qty", "city"], ["0", "MM/dd/yyyy", "0.00", "#,###", ""]);




请先 登录 后评论