小编Zin*_*ust的帖子

F#常量模式匹配和检查消除

我对F#世界很陌生,目前正试图尝试功能.试图找到新的技术,使它们更紧凑和灵活,并从C#类似的编码风格.所以我有一个非常难看的代码,我不断检查和模式匹配:

type StringChecking =
    | Success of string
    | Fail

    let StringProcessor(str : string) =
        let newstr = modifyFn str
        let result = checkFn newstr
        match result with
        | true -> Success result
        | false ->
            let newstr' = modifyFn' str
            let result' = checkFn newstr'
            match result' with
            | true -> Success newstr'
            | false -> 
                let newstr'' = modifyFn'' str
                let result'' = checkFn newstr''
                match result'' with
                | true -> Success newstr''
                | false -> 
                    Fail
Run Code Online (Sandbox Code Playgroud)

是否有一些有用的技术可以帮助我消除所有这些阶段,使我的代码更优雅.对不起,如果问题看起来很尴尬或愚蠢,但我真的想对这个问题做些什么,因为我想在我的某个项目中使用它.我已经厌倦了在C#中不断检查.感谢您的时间和建议.

f# functional-programming pattern-matching

5
推荐指数
1
解决办法
146
查看次数