代码之家  ›  专栏  ›  技术社区  ›  Juliet

Seq模块的静态扩展方法

  •  27
  • Juliet  · 技术社区  · 16 年前

    this post ,F#支持对象实例和静态类上的扩展方法。例如:

    module CollectionExtensions = 
        type System.Linq.Enumerable with   
            static member RangeChar(first:char, last:char) = {first .. last}
    
    open ExtensionFSharp.CollectionExtensions 
    

    System.Linq.Enumerable. 静态法 RangeChar 出现在我的智能感知窗口中。

    我想添加一个静态方法, for_alli

    module SeqExtensions =
        type Microsoft.FSharp.Collections.Seq with   (* error on this line *)
            static member for_alli f l =
                l
                |> Seq.mapi (fun i x -> i, x)
                |> Seq.for_all (fun (i, x) -> f i x)
    

    虽然两段代码的结构相同, SeqExtensions 不编译。F#突出显示单词 Seq

    如何在Seq模块上创建静态扩展方法?

    1 回复  |  直到 16 年前
        1
  •  48
  •   Brian    16 年前

    扩展F# 单元 ,只需创建另一个同名模块:

    module Seq =
        let myMap f s = seq { for x in s do yield f x }
    
    Seq. // see your stuff here alongside normal stuff