LAMBDA返回另一个lambda [英] Lambda returning another lambda

查看:113
本文介绍了LAMBDA返回另一个lambda的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法如何从另一个lambda递归返回拉姆达?

is there any way how to return lambda from another lambda recursively?

所有我想要做的是有限状态机,为lambda实现,它返回的lambda实现另一个国家(或空)。

All I want to do is finite state machine, implemented as lambda, which returns lambda implementing another state (or null).

嵌套函数功能<因为我想>将无法正常工作。

nesting Func<> won't work as I want.

C#.NET 3.5

C#, .NET 3.5

例如:

机,3种状态,pseudolanguage

machine, 3 states, pseudolanguage

private Lambda State1()
{  
    if (SomeConditionIsMet)
        return State2;
    else
        return State1;
}

private Lambda State2()
{  
    while (SomeConditionIsMet)
        return State2;
    else
        return State3;
}

private Lambda State3()
{  
    LogEnd();
    return NULL;
}

public void FSM()
{
    Lambda _currentState = State1;

    while(_currentState != NULL)
    {
        _currentState = _currentState();
    }
}



我知道,我可以使用枚举解决此+开关,例如,但我只是好奇,如果我能做到这一点。

I know, that I can workaround this using enum+switch, for example, but I'm just curious if I can do this.

推荐答案

我相信你可以声明委托类型:公共委托LAMBDA LAMBDA()返回自己的类型的委托。它编译,反正。

I believe you can declare a delegate type: public delegate Lambda Lambda() which returns a delegate of its own type. It does compile, anyway.

这篇关于LAMBDA返回另一个lambda的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆