Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In F#, how do you curry ParamArray functions (like sprintf)?

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

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"

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

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

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

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

I've tried something like

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

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

How is this done in F#?

like image 956
Fendy Avatar asked Jun 21 '12 19:06

Fendy


1 Answers

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

logger "%d %s" 10 "123"
logger "%d %s %b" 10 "123" true
like image 57
desco Avatar answered Sep 23 '22 13:09

desco