10
再帰的クロージャを返す関数を実装しようとしていますが、関数シグネチャでそれを表現する方法がわかりません。以下は、Pythonで動作する実装のコード例です。再帰的クロージャを返すための関数シグネチャ
def counter(state):
def handler(msg):
if msg == 'inc':
print state
return counter(state + 1)
if msg == 'dec':
print state
return counter(state - 1)
return handler
c = counter(1)
for x in range(1000000):
c = c('inc')
と擬似コードfor Rustです。
enum Msg {
Inc,
Dec
}
fn counter(state: Int) -> ? {
move |msg| match msg {
Msg::Inc => counter(state + 1),
Msg::Dec => counter(state - 1),
}
}