OCaml - 仿函数 - 如何使用?

标签 ocaml functor

关闭。这个问题需要更多 focused .它目前不接受答案。












想改进这个问题?更新问题,使其仅关注一个问题 editing this post .

8年前关闭。




Improve this question




有人可以向我解释仿函数吗?我想简单的例子。什么时候应该使用仿函数?

最佳答案

仿函数,本质上是一种根据其他模块编写模块的方法。

一个非常经典的例子是 Map.Make 标准库中的仿函数。这个仿函数让你定义一个具有特定键类型的映射。

这是一个微不足道且相当愚蠢的示例:

module Color = struct
    type t = Red | Yellow | Blue | Green | White | Black

    let compare a b =
        let int_of_color = function
            | Red -> 0
            | Yellow -> 1
            | Blue -> 2
            | Green -> 3
            | White -> 4
            | Black -> 5 in
        compare (int_of_color a) (int_of_color b)
end

module ColorMap = Map.Make(Color)

正在加载 ocaml显示生成模块的签名:
module Color :
  sig
    type t = Red | Yellow | Blue | Green | White | Black
    val compare : t -> t -> int
  end
module ColorMap :
  sig
    type key = Color.t
    type 'a t = 'a Map.Make(Color).t
    val empty : 'a t
    val is_empty : 'a t -> bool
    val mem : key -> 'a t -> bool
    val add : key -> 'a -> 'a t -> 'a t
    val singleton : key -> 'a -> 'a t
    val remove : key -> 'a t -> 'a t
    val merge :
      (key -> 'a option -> 'b option -> 'c option) -> 'a t -> 'b t -> 'c t
    val compare : ('a -> 'a -> int) -> 'a t -> 'a t -> int
    val equal : ('a -> 'a -> bool) -> 'a t -> 'a t -> bool
    val iter : (key -> 'a -> unit) -> 'a t -> unit
    val fold : (key -> 'a -> 'b -> 'b) -> 'a t -> 'b -> 'b
    val for_all : (key -> 'a -> bool) -> 'a t -> bool
    val exists : (key -> 'a -> bool) -> 'a t -> bool
    val filter : (key -> 'a -> bool) -> 'a t -> 'a t
    val partition : (key -> 'a -> bool) -> 'a t -> 'a t * 'a t
    val cardinal : 'a t -> int
    val bindings : 'a t -> (key * 'a) list
    val min_binding : 'a t -> key * 'a
    val max_binding : 'a t -> key * 'a
    val choose : 'a t -> key * 'a
    val split : key -> 'a t -> 'a t * 'a option * 'a t
    val find : key -> 'a t -> 'a
    val map : ('a -> 'b) -> 'a t -> 'b t
    val mapi : (key -> 'a -> 'b) -> 'a t -> 'b t
  end

简单的module ColorMap = Map.Make(Color)创建了一个模块,该模块实现了一个以颜色为键的 map 。现在可以调用ColorMap.singleton Color.Red 1并得到一张红色到数字 1 的 map 。

注意使用Map.Make工作,因为传递的模块( Color )满足 Map.Make 的要求仿函数。文档说仿函数的类型是 module Make: functor (Ord : OrderedType) -> S with type key = Ord.t . : OrderedType意味着输入模块( Color )必须与 OrderedType 一致(我相信有一个更正式的术语)模块签名。

OrderedType 保持一致输入模块必须有一个类型 t和一个函数 compare带签名t -> t -> int .换句话说,必须有一种方法来比较 t 类型的值。 .如果您查看 ocaml 报告的类型这正是Color补给品。

何时使用仿函数是一个更加困难的问题,因为通常有几种可能的设计,每一种都有自己的权衡。但大多数情况下,当图书馆提供仿函数作为推荐的做事方式时,您会使用仿函数。

关于OCaml - 仿函数 - 如何使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182290/

相关文章:

haskell - 在实践中使用单子(monad)、幺半群、仿函数和箭头

javascript - 如何将简单的命令行 OCaml 脚本编译成 Javascript

types - 模块 : type problem in functor

ocaml - ocaml中的匹配会调用构造函数吗?

haskell - 跨类型构造函数编写通用仿函数实例?

haskell - 如何从不纯方法中返回纯值

c++ - 传递给 std::for_each 时,仿函数能否保留值?

ocaml - 警告 40 : this record . .. 包含在当前范围内不可见的字段是什么意思

makefile - OCaml 生成文件 : No Rule to Make Target

类成员函数的 C++ Functor 模板