I'm processing a large number of object housed in an array. This processing takes a long time, and I'd like to be able to monitor were the fx is in the processing step.
My goal is to be able to print to the console some sort of Processing thing number *x*
while continuing to operate. For example, with this
let x = [|1..10..100000|]
x
|> Array.mapi (fun i n -> (i, n))
|> Array.map (fun (i, n) -> printfn "Processing n %i" i, (n * 2)))
|> Array.map snd
I get an output for every line. What'd I'd like to have is every 10th or 100th or 1000 print a statement, rather than every line. So I've tried
x
|> Array.mapi (fun i n -> (i, n))
|> Array.map (fun (i, n) -> (if (i % 100 = 0) then printfn "Processing n %i" i, (n * 2)))
|> Array.map snd
but this provides an error over the printfn...
bit with
The 'if' expression is missing an else branch. The 'then' branch has type
''a * 'b'. Because 'if' is an expression, and not a statement, add an 'else'
branch which returns a value of the same type.
I essentially want the else...
branch to do nothing, print nothing to the console, just be ignored.
Interestingly, in writing this question and trying things in FSI
, I tried this:
x
|> Array.mapi (fun i n -> (i, n))
|> Array.map (fun (i, n) -> match (i % 100 = 0) with
| true -> printfn "Processing n %i" i, (n * 2)
| false -> (), n * 2)
|> Array.map snd
which seems to work. Is this the best way to provide the console text?
It looks like you want:
let x' = x |> Array.mapi (fun i n ->
if i % 100 = 0 then
printfn "Processing n %i" i
n)
Both branches of an if
expression must have the same type and
if (i % 100 = 0) then printfn "Processing n %i" i, (n * 2)
returns a value of type (unit, int)
for the true case. The missing else
case implicitly has type ()
so the types do not match. You can just print the value, ignore the result and then return the current value.