3
votes

I'm trying to print the output of function only when it is true but so far all attempts have been unsuccsessful.

Something on the lines of:

let printFactor a b =  if b then print_any((a,b)) 

Where b is a boolean and a is an integer. When I try it I get:

val printFactor : 'a -> bool -> unit

Any suggestions?

EDIT:

To put things in context im trying to use this with a pipe operator. Lets say I have a function xyz that outputs a list of (int, bool). Id like to do something on these lines:

xyz |> printFactor

to print the true values only.

1
I am unclear what you are asking or what you are trying to do. - Brian
that is the correct type signature for the function you wrote. what is the function print_any do? look there next. - nlucaroni

1 Answers

6
votes

You could do e.g. this

let xyz() = [ (1,true); (2,false) ]

let printFactor (i,b) = 
    if b then
        printfn "%A" i

xyz() |> List.iter printFactor

but it would probably be more idiomatic to do, e.g. this

xyz() 
|> List.filter (fun (i,b) -> b) 
|> List.iter (fun (i,b) -> printfn "%d" i)

that is, first filter, and then print.