什么是仿函数,我们为什么需要它们?
What are functors, and why do we need them?
我不明白 Factor 的 functors。我想首先了解什么是 "functor" 会有所帮助。
Google 说:
a function; an operator.
在Factor中,所有的函数(词)都是运算符,都是first-class。 (其实我在Factor中想不出太多不是先class)。这个定义不是很有用。
维基百科说:
Functor may refer to:
- ...
- In computer programming:
- function object used to pass function pointers along with its state
- ...
- In Haskell a Functor describes a generalisation of functions that perform mapping operations
"function object" 的页面说:
an object to be invoked or called as if it were an ordinary function, usually with the same syntax (a function parameter that can also be a function).
所以函子是第一个class函数?这没什么特别的,反正单词和引语之类的东西已经是第一个-class in Factor.
Factor Functor 的语法很奇怪,让我想起了泛型之类的东西。
resource:unmaintained/models/combinators/templates/templates.factor:
FROM: models.combinators => <collection> #1 ;
FUNCTOR: fmaps ( W -- )
W IS ${W}
w-n DEFINES ${W}-n
w-2 DEFINES 2${W}
w-3 DEFINES 3${W}
w-4 DEFINES 4${W}
w-n* DEFINES ${W}-n*
w-2* DEFINES 2${W}*
w-3* DEFINES 3${W}*
w-4* DEFINES 4${W}*
WHERE
MACRO: w-n ( int -- quot ) dup '[ [ _ narray <collection> ] dip [ _ firstn ] prepend W ] ;
: w-2 ( a b quot -- mapped ) 2 w-n ; inline
: w-3 ( a b c quot -- mapped ) 3 w-n ; inline
: w-4 ( a b c d quot -- mapped ) 4 w-n ; inline
MACRO: w-n* ( int -- quot ) dup '[ [ _ narray <collection> #1 ] dip [ _ firstn ] prepend W ] ;
: w-2* ( a b quot -- mapped ) 2 w-n* ; inline
: w-3* ( a b c quot -- mapped ) 3 w-n* ; inline
: w-4* ( a b c d quot -- mapped ) 4 w-n* ; inline
;FUNCTOR
关于这些的文档非常稀少。这些是什么?我应该什么时候使用它们?
不要把函子想成那样 'They're named "functors" to annoy category theory fanboys and language purists.' :)
它们的用途主要是生成样板或模板代码。就像 C++ 模板是一种优化功能一样,因为泛型分派可能很慢,Factor 仿函数也是如此。
此处示例:
USING: functors io lexer namespaces ;
IN: examples.functors
FUNCTOR: define-table ( NAME -- )
name-datasource DEFINES-CLASS ${NAME}-datasource
clear-name DEFINES clear-${NAME}
init-name DEFINES init-${NAME}
WHERE
SINGLETON: name-datasource
: clear-name ( -- ) "clear table code here" print ;
: init-name ( -- ) "init table code here" print ;
name-datasource [ "hello-hello" ] initialize
;FUNCTOR
SYNTAX: SQL-TABLE: scan-token define-table ;
您现在可以写 SQL-TABLE: person
,Factor 会为您创建单词 clear-person
、init-person
和 person-datasource
。
什么时候使用它们?我认为永远不会,除非你有性能问题需要使用它们。它们对 grepability 非常不利。
我不明白 Factor 的 functors。我想首先了解什么是 "functor" 会有所帮助。
Google 说:
a function; an operator.
在Factor中,所有的函数(词)都是运算符,都是first-class。 (其实我在Factor中想不出太多不是先class)。这个定义不是很有用。
维基百科说:
Functor may refer to:
- ...
- In computer programming:
- function object used to pass function pointers along with its state
- ...
- In Haskell a Functor describes a generalisation of functions that perform mapping operations
"function object" 的页面说:
an object to be invoked or called as if it were an ordinary function, usually with the same syntax (a function parameter that can also be a function).
所以函子是第一个class函数?这没什么特别的,反正单词和引语之类的东西已经是第一个-class in Factor.
Factor Functor 的语法很奇怪,让我想起了泛型之类的东西。
resource:unmaintained/models/combinators/templates/templates.factor:
FROM: models.combinators => <collection> #1 ;
FUNCTOR: fmaps ( W -- )
W IS ${W}
w-n DEFINES ${W}-n
w-2 DEFINES 2${W}
w-3 DEFINES 3${W}
w-4 DEFINES 4${W}
w-n* DEFINES ${W}-n*
w-2* DEFINES 2${W}*
w-3* DEFINES 3${W}*
w-4* DEFINES 4${W}*
WHERE
MACRO: w-n ( int -- quot ) dup '[ [ _ narray <collection> ] dip [ _ firstn ] prepend W ] ;
: w-2 ( a b quot -- mapped ) 2 w-n ; inline
: w-3 ( a b c quot -- mapped ) 3 w-n ; inline
: w-4 ( a b c d quot -- mapped ) 4 w-n ; inline
MACRO: w-n* ( int -- quot ) dup '[ [ _ narray <collection> #1 ] dip [ _ firstn ] prepend W ] ;
: w-2* ( a b quot -- mapped ) 2 w-n* ; inline
: w-3* ( a b c quot -- mapped ) 3 w-n* ; inline
: w-4* ( a b c d quot -- mapped ) 4 w-n* ; inline
;FUNCTOR
关于这些的文档非常稀少。这些是什么?我应该什么时候使用它们?
不要把函子想成那样 'They're named "functors" to annoy category theory fanboys and language purists.' :)
它们的用途主要是生成样板或模板代码。就像 C++ 模板是一种优化功能一样,因为泛型分派可能很慢,Factor 仿函数也是如此。
此处示例:
USING: functors io lexer namespaces ;
IN: examples.functors
FUNCTOR: define-table ( NAME -- )
name-datasource DEFINES-CLASS ${NAME}-datasource
clear-name DEFINES clear-${NAME}
init-name DEFINES init-${NAME}
WHERE
SINGLETON: name-datasource
: clear-name ( -- ) "clear table code here" print ;
: init-name ( -- ) "init table code here" print ;
name-datasource [ "hello-hello" ] initialize
;FUNCTOR
SYNTAX: SQL-TABLE: scan-token define-table ;
您现在可以写 SQL-TABLE: person
,Factor 会为您创建单词 clear-person
、init-person
和 person-datasource
。
什么时候使用它们?我认为永远不会,除非你有性能问题需要使用它们。它们对 grepability 非常不利。