Best way to use match..with conditionally insert values โ€‹โ€‹into sequence expression?

I thought it match .. withmight work like ifinside a sequence expression:

let foo (m: 'a option) =
    [ yield 'a'
      match m with
        | Some _ -> yield 'b'
      yield 'c' ]

That is, I only need to indicate the appropriate case (s), while the failure case is treated as no-op. Unfortunately, this is not the case; an exception causes a mismatch.

I found that I can still get the desired result if I use yield!as follows:

let bar (m: 'a option) =
    [ yield 'a'
      match m with
        | Some _ -> yield 'b'
        | _ -> yield! []
      yield 'c' ]

It works, but is this the best way? Just wondering if there is a more standard way to do this.

+4
source share
1 answer

Funk's answer is correct, but you can also replace it yield! []with a simple one ():

let bar (m: 'a option) =
    [ yield 'a'
      match m with
        | Some _ -> yield 'b'
        | _ -> ()
      yield 'c' ]

, , unit (.. ()), "" ( "" ). "" - . , if else: else else (). Funk if Option.isSome m then yield 'b' else (), if Option.isSome m then yield 'b' else yield! [].

, , ; , "click", . , .

+2

Source: https://habr.com/ru/post/1676430/


All Articles