简体   繁体   English

F#打印如果为真

[英]F# Print if true

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. 其中b是布尔值, a是整数。 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). 假设我有一个函数xyz ,它输出一个(int,bool)列表。 Id like to do something on these lines: 我喜欢在这些方面做点什么:

xyz |> printFactor

to print the true values only. 仅打印真实值。

You could do eg 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, eg this 但这可能是更惯用的,例如这个

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

that is, first filter, and then print. 也就是说,首先过滤,然后打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM