Does F# have the ternary ?: operator?

北慕城南 提交于 2019-12-01 02:04:33

Yes, it's called if .. then .. else

In fact in F# everything is an expression, even an if .. then .. else block.

In C# var x = true ? 0 : 1;

In F# let x = if true then 0 else 1

So in your case:

let y = Seq.groupBy (fun x -> if x < p then -1 else if x = p then 0 else 1)

you can shorten it a bit with elif

let y = Seq.groupBy (fun x -> if x < p then -1 elif x = p then 0 else 1)

Another option to consider in F# specially when you have more than 2 cases is pattern matching:

let f p x =
    match x with
    | x when x < p -> -1
    | x when x = p ->  0
    | _ -> 1

let y = Seq.groupBy (f p)

But in your particular case I would use the if .. then .. elif .. then.

Finally note that the test-equality operator is = not == as in C#.

You can also implement this using pattern matching with function using guards:

    let y = Seq.groupBy  (function |x when x < p -> -1
                                   |x when x = p -> 0
                                   |_ -> 1)

Pattern matches may seem longer ternary operator but they are much easier to read when logic gets more complex.

If you want to save the typing you can define your own

let (?=) (q: bool) (yes: 'a, no: 'a) = if q then yes else no

Note that you can't use : in operators so ?= is the nearest you can get.

Usage: maybe ?= ("true", "false")

For more examples of C# expressions and statements in F# you can refer to this page. For example:

Ternary operator

C# has the ternary operator "?:" for conditional expressions:

condition ? trueVal : falseVal 

F# has the same operator, but its name is if-then-else:

if condition then trueVal else falseVal

(Note that "if" is used much less frequently in F# than in C#; in F#, many conditionalexpressions are done via pattern-matching rather than if-then-else.)

Switch statement

C# has a switch statement. It looks something like this:

switch (x) 
{ 
    case 1: 
        SomeCode(); 
        break; 
    default: 
        SomeCode(); 
        break; 
} 

In F#, this is just one of many things that pattern matching expresses more succinctly:

    match x with 
     | 1 -> SomeCode() 
     | _ -> SomeCode()  // _ is a ‘catch all’ default
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!