在阅读各种关于函数式编程的文章时,我遇到过几次“Functor”这个术语,但作者通常认为读者已经理解了这个术语。在网络上你可以看到一些过于技术性的描述(参见维基百科的文章),也可以看到一些非常模糊的描述(参见ocaml-tutorial网站上关于函数函数的部分)。
有没有人可以定义这个术语,解释它的用法,或者提供一个如何创建和使用函子的例子?
编辑:虽然我对这个术语背后的理论很感兴趣,但我对这个概念的实现和实际应用更感兴趣,而不是理论。
编辑2:看起来好像有一些交叉术语:我特别指的是函数式编程的函子,而不是c++的函数对象。
有三种不同的意思,没有太大的联系!
In Ocaml it is a parametrized module. See manual. I think the best way to grok them is by example: (written quickly, might be buggy)
module type Order = sig
type t
val compare: t -> t -> bool
end;;
module Integers = struct
type t = int
let compare x y = x > y
end;;
module ReverseOrder = functor (X: Order) -> struct
type t = X.t
let compare x y = X.compare y x
end;;
(* We can order reversely *)
module K = ReverseOrder (Integers);;
Integers.compare 3 4;; (* this is false *)
K.compare 3 4;; (* this is true *)
module LexicographicOrder = functor (X: Order) ->
functor (Y: Order) -> struct
type t = X.t * Y.t
let compare (a,b) (c,d) = if X.compare a c then true
else if X.compare c a then false
else Y.compare b d
end;;
(* compare lexicographically *)
module X = LexicographicOrder (Integers) (Integers);;
X.compare (2,3) (4,5);;
module LinearSearch = functor (X: Order) -> struct
type t = X.t array
let find x k = 0 (* some boring code *)
end;;
module BinarySearch = functor (X: Order) -> struct
type t = X.t array
let find x k = 0 (* some boring code *)
end;;
(* linear search over arrays of integers *)
module LS = LinearSearch (Integers);;
LS.find [|1;2;3] 2;;
(* binary search over arrays of pairs of integers,
sorted lexicographically *)
module BS = BinarySearch (LexicographicOrder (Integers) (Integers));;
BS.find [|(2,3);(4,5)|] (2,3);;
您现在可以快速添加许多可能的顺序,形成新顺序的方法,轻松地对它们进行二进制或线性搜索。泛型编程。
In functional programming languages like Haskell, it means some type constructors (parametrized types like lists, sets) that can be "mapped". To be precise, a functor f is equipped with (a -> b) -> (f a -> f b). This has origins in category theory. The Wikipedia article you linked to is this usage.
class Functor f where
fmap :: (a -> b) -> (f a -> f b)
instance Functor [] where -- lists are a functor
fmap = map
instance Functor Maybe where -- Maybe is option in Haskell
fmap f (Just x) = Just (f x)
fmap f Nothing = Nothing
fmap (+1) [2,3,4] -- this is [3,4,5]
fmap (+1) (Just 5) -- this is Just 6
fmap (+1) Nothing -- this is Nothing
因此,这是一种特殊的类型构造函数,与Ocaml中的函子关系不大!
在命令式语言中,它是指向函数的指针。
函子是对象和态射的映射,它保留了一个类别的组成和身份。
让我们定义什么是类别?
是一堆东西!
在a内部画几个点(现在是两个点,一个是“a”,另一个是“b”)
圆圈,并命名为圆圈A(类别)。
这个类别包含什么?
对象之间的组合和每个对象的恒等函数。
因此,在应用Functor之后,我们必须映射对象并保存组合。
让我们想象‘A’是我们的范畴,它有对象[' A', 'b'],并且存在一个态射A -> b
现在,我们必须定义一个函子,它可以将这些对象和态射映射到另一个类别“B”。
假设这个函子叫做Maybe
data Maybe a = Nothing | Just a
B类是这样的。
请再画一个圆,但这次用“也许a”和“也许b”代替“a”和“b”。
一切看起来都很好,所有的对象都被映射了
“a”变成了“也许a”,“b”变成了“也许b”。
但问题是我们也要把态射从a映射到b。
这意味着' a'中的态态a -> b应该映射到'可能a' -> '可能b'
来自a -> b的形态称为f,然后来自'Maybe a' -> 'Maybe b'的形态称为'fmap f'
现在让我们看看函数f在A中做了什么,看看我们能否在B中复制它
A中f的函数定义:
f :: a -> b
F取a并返回b
f在B中的函数定义:
f :: Maybe a -> Maybe b
f取也许a,返回也许b
让我们看看如何使用fmap将函数'f'从'A'映射到'B'中的函数'fmap f'
fmap的定义
fmap :: (a -> b) -> (Maybe a -> Maybe b)
fmap f Nothing = Nothing
fmap f (Just x) = Just(f x)
那么,我们在这里做什么?
我们将函数“f”应用于类型为“a”的“x”。“Nothing”的特殊模式匹配来自于Functor Maybe的定义。
因此,我们将对象[a, b]和形态[f]从类别' a '映射到类别' b '。
那是富克托!