F# apply sprintf to a list of strings - f#

How can I best create a function (lets call it myPrint) that takes sprintf, a format string, and a list of strings as arguments and produces a result such that each element in the list of strings is applied/folded into sprintf?
i.e.
myPrint (sprintf "one: %s two: %s three: %s") ["first"; "second"; "third"];;
would produce the output
val myPrint : string = "one: first two: second three: third"

Note that format strings in F# are specifically designed to take a statically known number of arguments, so there's not a particularly elegant way to do what you want. However, something like this should work:
let rec myPrintHelper<'a> (fmt:string) : string list -> 'a = function
| [] -> sprintf (Printf.StringFormat<_>(fmt))
| s::rest -> myPrintHelper<string -> 'a> fmt rest s
let myPrint fmt = myPrintHelper<string> fmt
myPrint "one: %s two: %s three: %s" ["first"; "second"; "third"]
This will throw an exception at runtime if the number of "%s"es in your string doesn't match the number of strings in the list.

List.map myPrint ["first"; "second"; "third;"]
Will return a new sprintf'ed List...
To just print out to console... List.iter will iterate a function over each value in a list, and perform that function - but will only work with functions that don't return a value. (i.e. printf...)
map takes an 'T -> 'U - and will convert a list of any format into any other
So
let myPrint in = sprintf "%s" in
would be allowed, but reading your question again - it doesn't do what you want.
- you could use mapi, which adds the index in (int -> 'T -> 'U) and would let you define myPrint as
let myPrint index val = sprintf "%d : %s" index val
Which would return ["1 : first"; "2 : second"; "3 : third"] which is getting closer...
But it looks like you want a single string returned - so either to String.Join on the output of that - or use fold:
let final = List.fold (fun (builder, index) in -> builder.AppendFormat("{0}: {1}", index, in), index + 1) (new StringBuilder()) ["first"; "second"; "third"]

Related

Writing unit tests against PrintfFormat

I have a type that I'm trying to understand by writing unit tests against it, however I can't reason what to do with PrintfFormat
type ValueFormat<'p,'st,'rd,'rl,'t,'a> = {
format: PrintfFormat<'p,'st,'rd,'rl,'t>
paramNames: (string list) option
handler: 't -> 'a
}
with
static member inline construct (this: ValueFormat<_,_,_,_,_,_>) =
let parser s =
s |> tryKsscanf this.format this.handler
|> function Ok x -> Some x | _ -> None
let defaultNames =
this.format.GetFormatterNames()
|> List.map (String.replace ' ' '_' >> String.toUpperInvariant)
|> List.map (sprintf "%s_VALUE")
let names = (this.paramNames ?| defaultNames) |> List.map (sprintf "<%s>")
let formatTokens = this.format.PrettyTokenize names
(parser, formatTokens)
I feel confident that I can figure everything out but PrintfFormat is throwing me with all those generics.
The file I'm looking at for the code I want to unit test is here for the FSharp.Commandline framework.
My question is, what is PrintfFormat and how should it be used?
A link to the printf.fs file is here. It contains the definition of PrintfFormat
The PrintfFormat<'Printer,'State,'Residue,'Result,'Tuple> type, as defined in the F# source code, has four type parameters:
'Result is the type that your formatting/parsing function produces. This is string for sprintf
'Printer is a type of a function generated based on the format string, e.g. "%d and %s" will give you a function type int -> string -> 'Result
'Tuple is a tuple type generated based on the format string, e.g. "%d and %s" will give you a tuple type int * string.
'State and 'Residue are type parameters that are used when you have a custom formatter using %a, but I'll ignore that for now for simplicity (it's never needed unless you have %a format string)
There are two ways of using the type. Either for formatting, in which case you'll want to write a function that returns 'Printer as the result. The hard thing about this is that you need to construct the return function using reflection. Here is an example that works only with one format string:
open Microsoft.FSharp.Reflection
let myformat (fmt:PrintfFormat<'Printer,obj,obj,string,'Tuple>) : 'Printer =
unbox <| FSharpValue.MakeFunction(typeof<'Printer>, fun o ->
box (o.ToString()) )
myformat "%d" 1
myformat "%s" "Yo"
This simply returns the parameter passed as a value for %d or %s. To make this work for multiple arguments, you'd need to construct the function recursively (so that it's not just e.g. int -> string but also int -> (int -> string))
In the other use, you define a function that returns 'Tuple and it needs to create a tuple containing values according to the specified formatting string. Here is a small sample that only handles %s and %d format strings:
open FSharp.Reflection
let myscan (fmt:PrintfFormat<'Printer,obj,obj,string,'Tuple>) : 'Tuple =
let args =
fmt.Value
|> Seq.pairwise
|> Seq.choose (function
| '%', 'd' -> Some(box 123)
| '%', 's' -> Some(box "yo")
| _ -> None)
unbox <| FSharpValue.MakeTuple(Seq.toArray args, typeof<'Tuple>)
myscan "%d %s %d"

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#?
let log (s : string) = ()
let logger fmt = Printf.kprintf log fmt
logger "%d %s" 10 "123"
logger "%d %s %b" 10 "123" true
The behaviour of printf-like functions in F# is in some way special. They take a format string, which specifies what the expected arguments are. You can use Printf.kprintf as shown by desco to define your own function that takes a format string, but you cannot change the handling of format strings.
If you want to do something like C# params (where the number of arguments is variable, but does not depend on the format string), then you can use ParamArray attribute directly on a member:
open System
type Foo =
static member Bar([<ParamArray>] arr:obj[]) =
arr |> Seq.mapi (fun i v -> printfn "[%d]: %A" i v)
Then you can call Foo.Bar with any number of arguments without format string:
Foo.Bar("hello", 1, 3.14)
This is less elegant for string formatting, but it might be useful in other situations. Unfortunatelly, it will only work with members (and not with functions defined with let)

F#: Composing sprintf with a string -> unit function to allow formatting

There's information out there on how to do custom processing on a format and its parts. I want to do something a bit simpler, specifically, I want to do something to the effect of:
let writelog : string -> unit = ... // write the string to the log
let writelogf = sprintf >> writelog // write a formatted string to the log
I'm not too surprised that the compiler gets confused by this, but is there any way to get it to work?
The simplest way to define your own function that takes a formatting string like printf is to use Printf.kprintf function. The first argument of kprintf is a function that is used to display the resulting string after formatting (so you can pass it your writelog function):
let writelog (s:string) = printfn "LOG: %s" s
let writelogf fmt = Printf.kprintf writelog fmt
The fmt parameter that is passed as a second argument is the special format string. This works better than jpalmer's solution, because if you specify some additional arguments, they will be directly passed to kprintf (so the number of arguments can depend on the formatting string).
You can write:
> writelogf "Hello";;
LOG: Hello
> writelogf "Hello %d" 42;;
LOG: Hello 42
This works
> let writelog = fun (s:string) -> printfn "%s" s;;
val writelog : string -> unit
> let writelogf arg = sprintf arg >> writelog;;
val writelogf : Printf.StringFormat<('a -> string)> -> ('a -> unit)
> writelogf "hello %s" "world";;
hello world
val it : unit = ()
>
(session is from FSI)
key is in the extra argument to writelogf

What replaces the deprecated any_to_string function?

I guess it's now Object.ToString() but I'm unsure... Thanks!
I think the only way to get any_to_string functionality is with the "%A" formatter. The warning tells you this.
let any_to_string = sprintf "%A"
This does not invoke .ToString(). For 'simple' types like lists the .ToString() already yields a good representation. But when using your own custom types, the %A formatter is much more useful. For instance, in case of tree structures it walks along the tree.
If you do want to invoke .ToString() on an object, you can use the '%O' formatter.
Example:
type Tree = Node of Tree * Tree | Leaf
let myTree = Node(Node(Leaf,Leaf),Node(Leaf,Node(Leaf,Leaf)))
and in FSI:
> myTree.ToString();;
val it : string = "FSI_0002+Tree+Node"
> sprintf "%O" myTree;;
val it : string = "FSI_0002+Tree+Node"
> sprintf "%A" myTree;;
val it : string = "Node (Node (Leaf,Leaf),Node (Leaf,Node (Leaf,Leaf)))"
you can use sprintf:
let a = [1;2;3]
let b = sprintf "%A" a
What about the string function?
> string [1..3];;
val it : string = "[1; 2; 3]"

f# byte[] -> hex -> string conversion

I have byte array as input. I would like to convert that array to string that contains hexadecimal representation of array values. This is F# code:
let ByteToHex bytes =
bytes
|> Array.map (fun (x : byte) -> String.Format("{0:X2}", x))
let ConcatArray stringArray = String.Join(null, (ByteToHex stringArray))
This produces result I need, but I would like to make it more compact so that I have only one function.
I could not find function that would concat string representation of each byte at the end
of ByteToHex.
I tried Array.concat, concat_map, I tried with lists, but the best I could get is array or list of strings.
Questions:
What would be simplest, most elegant way to do this?
Is there string formatting construct in F# so that I can replace String.Format from System assembly?
Example input: [|0x24uy; 0xA1uy; 0x00uy; 0x1Cuy|] should produce string "24A1001C"
There is nothing inherently wrong with your example. If you'd like to get it down to a single expression then use the String.contcat method.
let ByteToHex bytes =
bytes
|> Array.map (fun (x : byte) -> System.String.Format("{0:X2}", x))
|> String.concat System.String.Empty
Under the hood, String.concat will just call into String.Join. Your code may have to be altered slighly though because based on your sample you import System. This may create a name resolution conflict between F# String and System.String.
If you want to transform and accumulate in one step, fold is your answer. sprintf is the F# string format function.
let ByteToHex (bytes:byte[]) =
bytes |> Array.fold (fun state x-> state + sprintf "%02X" x) ""
This can also be done with a StringBuilder
open System.Text
let ByteToHex (bytes:byte[]) =
(StringBuilder(), bytes)
||> Array.fold (fun state -> sprintf "%02X" >> state.Append)
|> string
produces:
[|0x24uy; 0xA1uy; 0x00uy; 0x1Cuy|] |> ByteToHex;;
val it : string = "24A1001C"
Here's another answer:
let hashFormat (h : byte[]) =
let sb = StringBuilder(h.Length * 2)
let rec hashFormat' = function
| _ as currIndex when currIndex = h.Length -> sb.ToString()
| _ as currIndex ->
sb.AppendFormat("{0:X2}", h.[currIndex]) |> ignore
hashFormat' (currIndex + 1)
hashFormat' 0
The upside of this one is that it's tail-recursive and that it pre-allocates the exact amount of space in the string builder as will be required to convert the byte array to a hex-string.
For context, I have it in this module:
module EncodingUtils
open System
open System.Text
open System.Security.Cryptography
open Newtonsoft.Json
let private hmacmd5 = new HMACMD5()
let private encoding = System.Text.Encoding.UTF8
let private enc (str : string) = encoding.GetBytes str
let private json o = JsonConvert.SerializeObject o
let md5 a = a |> (json >> enc >> hmacmd5.ComputeHash >> hashFormat)
Meaning I can pass md5 any object and get back a JSON hash of it.
Here's another. I'm learning F#, so feel free to correct me with more idiomatic ways of doing this:
let bytesToHexString (bytes : byte[]) : string =
bytes
|> Seq.map (fun c -> c.ToString("X2"))
|> Seq.reduce (+)
Looks fine to me. Just to point out another, in my opinion, very helpful function in the Printf module, have a look at ksprintf. It passes the result of a formated string into a function of your choice (in this case, the identity function).
val ksprintf : (string -> 'd) -> StringFormat<'a,'d> -> 'a
sprintf, but call the given 'final' function to generate the result.
To be honest, that doesn't look terrible (although I also have very little F# experience). Does F# offer an easy way to iterate (foreach)? If this was C#, I might use something like (where raw is a byte[] argument):
StringBuilder sb = new StringBuilder();
foreach (byte b in raw) {
sb.Append(b.ToString("x2"));
}
return sb.ToString()
I wonder how that translates to F#...

Resources