F#: Is there a way to extend the monad keyword list?

前端 未结 2 1318
逝去的感伤
逝去的感伤 2021-01-30 14:11

Inside an F# monad, if you say let!, the compiler translates that to a Bind member that you\'ve defined on the monad builder.

Now I see there a

2条回答
  •  囚心锁ツ
    2021-01-30 14:39

    In F# 2.0 (that is Visual Studio 2010), there is no way to extend the keyword list (other than Ramon's extension). However, the query mechanism in F# 3.0 (Visual Sutdio 11) is extensible and you can define your own keywords similar to select and count.

    Here is a basic example that defines something like seq builder with reverse keyword:

    type SeqBuilder() =
        // Standard definition for 'for' and 'yield' in sequences
        member x.For (source : seq<'T>, body : 'T -> seq<'R>) =
          seq { for v in source do yield! body v }
        member x.Yield item =
          seq { yield item }
    
        // Define an operation 'select' that performs projection
        []
        member x.Select (source : seq<'T>, [] f: 'T -> 'R) : seq<'R> =
            Seq.map f source
    
        // Defines an operation 'reverse' that reverses the sequence    
        []
        member x.Expand (source : seq<'T>) =
            List.ofSeq source |> List.rev
    
    let mseq = SeqBuilder()
    

    The details how this works are not yet documented, but the CustomOperation attribute says that the operation should be treated as a special syntax (you can set various properties to specify how it behaves - MaintainsVariableSpace means that it does not change the values inside sequence). The Projectionparameter attribute specifies that the expression following the keyword should be implicitly converted to a function.

    Now, the mseq builder supports both select and reverse:

    let q = mseq { for i in 1 .. 10 do
                   select (i + 100)
                   reverse }
    

提交回复
热议问题