Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/fsharp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
F# 在模块中先调用函数再调用函数_F# - Fatal编程技术网

F# 在模块中先调用函数再调用函数

F# 在模块中先调用函数再调用函数,f#,F#,我有一个带有函数集的模块。每个函数都做一些工作,但在调用foo()函数之前 有没有办法避免在每个函数中写入foo() 更新:对不起,我弄错了,现在已经更改了问题您可以在函数签名中输入do\u something\u general(): module Sample let foo() = do-something-general let a() = foo() // another actions let b() = foo() // another act

我有一个带有函数集的模块。每个函数都做一些工作,但在调用foo()函数之前

有没有办法避免在每个函数中写入
foo()


更新:对不起,我弄错了,现在已经更改了问题

您可以在函数签名中输入
do\u something\u general()

module Sample

let foo() = 
   do-something-general

let a() =
   foo()
   // another actions

let b() =
   foo()
   // another actions

let c() =
   foo()
   // another actions
把它叫做

let a do_something = 
    do_something()
    //other acitons
<>你也可以考虑尝试这样的计算工作流:

a do_something_general
然后调用
a
将导致:

type FooBuilder() =
    let foo () = printfn "This is the doing general stuff"

    member this.Bind(x, f) = 
        foo ()
        f x

    member this.Return(x) = 
        x

let foo = new FooBuilder()

let a() = 
    foo
        {
        let! x= (fun () -> printfn "This happens after the general stuff")
        return x()
        }

另一个选项是定义一个简单的包装函数:

> a();;
This is the doing general stuff
This happens after the general stuff
val it : unit = ()
然后你可以定义你想要的函数

let wrap f () = 
    foo ()
    f ()
然后将其包裹以获得所需的效果:

let a () = printfn "stuff"
let b () = printfn "more stuff"

我想您可以编写一个函数,它接受对
执行一般操作的结果的继续操作,但我认为需要更多的上下文才能给出真正的答案。您想实现什么,为什么?比如说,我们找到了一些方法来隐藏单个函数代码的副作用。这真的能改善什么吗?阅读函数的人会得到这样的印象,即函数不会产生副作用,即使它们会产生副作用。据我所知,这是不可能的,对此我很高兴。你可以试着给出更具体的细节;也许有不同的方法来解决这个问题。为什么函数首先需要调用
do-something-general()
?如上所述,它们并没有被约束,所以我们可以假设它们有副作用吗?如果是这样,我会想到两个选择:1。看看你是否能让你的设计更实用。2.更加面向对象并使用。我需要记录每个函数的调用。它是一个foo()函数。
let wrappedA = wrap a
let wrappedB = wrap b