gpt4 book ai didi

f# - Seq 模块上的静态扩展方法

转载 作者:行者123 更新时间:2023-12-03 09:52:52 24 4
gpt4 key购买 nike

根据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出现在我的 Intellisense 窗口中。

我想添加一个静态方法, for_alli , 到 Seq 模块。我将上面的代码修改如下:
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'”。

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

最佳答案

要扩展 F# 模块,只需创建另一个具有相同名称的模块:

module Seq =
let myMap f s = seq { for x in s do yield f x }

Seq. // see your stuff here alongside normal stuff

关于f# - Seq 模块上的静态扩展方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/671420/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com