Kdb/q:编写一个函数,该函数始终占据列表的开头,并将值返回到列表的末尾
我想维护一个列表Kdb/q:编写一个函数,该函数始终占据列表的开头,并将值返回到列表的末尾,kdb,Kdb,我想维护一个列表L。所以我想写一个函数func[],它总是取列表L的最后一个表,处理它,返回一个附加到L末尾的值 例如: 让Lbe[a,b,c] func()将采用c,计算d=func[c] 现在L变成[a,b,c,d] func()将取d,计算e=func[d] 现在L变成[a,b,c,d,e] 等等 我如何在kdb/q中实现这一点?Do或while应该允许您实现这一点,例如 q)l:0 2;f:{x,2*last x} q) q)5 f/ l // do op 5 tim
L
。所以我想写一个函数func[]
,它总是取列表L
的最后一个表,处理它,返回一个附加到L
末尾的值
例如:
让L
be[a,b,c]
func()
将采用c
,计算d=func[c]
现在L
变成[a,b,c,d]
func()
将取d
,计算e=func[d]
现在L
变成[a,b,c,d,e]
等等
我如何在kdb/q中实现这一点?Do或while应该允许您实现这一点,例如
q)l:0 2;f:{x,2*last x}
q)
q)5 f/ l // do op 5 times
0 2 4 8 16 32 64
q)
q)(64>last@) f/ l // perform op while last item in list is less than 64
0 2 4 8 16 32 64
Do或while应允许您实现这一目标,例如
q)l:0 2;f:{x,2*last x}
q)
q)5 f/ l // do op 5 times
0 2 4 8 16 32 64
q)
q)(64>last@) f/ l // perform op while last item in list is less than 64
0 2 4 8 16 32 64
我认为over函数正是您所需要的。
q)l:123
q) f:{if[100我认为over函数就是你所需要的。
q)l:123
q) f:{if[100添加到@CWD的答案上,您可以使用.z.s
使用显式递归来实现这一点,如下所示:
func: {[x;f;threshold] $[threshold <= count x;x;.z.s [x,enlist f last x;f;threshold]]}
func[1 2 3;{x*10};5} /returns 1 2 3 30 300
func:{[x;f;threshold]$[threshold添加到@CWD的答案上,您可以使用.z.s
进行显式递归,如下所示:
func: {[x;f;threshold] $[threshold <= count x;x;.z.s [x,enlist f last x;f;threshold]]}
func[1 2 3;{x*10};5} /returns 1 2 3 30 300
func:{[x;f;阈值]$[threshold