在F#中,您如何咖喱ParamArray函数(如sprintf)? [英] In F#, how do you curry ParamArray functions (like sprintf)?

查看:57
本文介绍了在F#中,您如何咖喱ParamArray函数(如sprintf)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在F#中,您如何咖喱接受可变数量参数的函数?

In F#, how do you curry a function that accepts a variable number of parameters?

我有这样的代码...(log函数只是一个例子,确切的实现没有关系)

I have code like this...(the log function is just an example, the exact implementation doesn't matter)

let log (msg : string) =
    printfn "%s" msg

log "Sample"

在整个代码中都使用sprintf格式的字符串调用它,例如.

It gets called throughout the code with sprintf formatted strings, ex.

log (sprintf "Test %s took %d seconds" "foo" 2.345)

我想在log函数中使用sprintf功能,使它看起来像...

I want to curry the sprintf functionality in the log function so it looks like...

logger "Test %s took %d seconds" "foo" 2.345

我尝试过类似的事情

let logger fmt ([<ParamArray>] args) =
    log (sprintf fmt args)

但是我不知道如何将ParamArray参数传递给sprintf调用.

but I cannot figure out how to pass the ParamArray argument through to the sprintf call.

这在F#中是如何完成的?

How is this done in F#?

推荐答案

let log (s : string) = ()
let logger fmt = Printf.kprintf log fmt

logger "%d %s" 10 "123"
logger "%d %s %b" 10 "123" true

这篇关于在F#中,您如何咖喱ParamArray函数(如sprintf)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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