我不理解因子的functors。我想首先要了解“仿函数”是什么有帮助。
谷歌说:一个功能;经营者。
在因子中,所有函数(单词)都是运算符,并且是一流的。 (事实上,我想不出因为不是头等舱)。这个定义没那么有用。
维基百科说:
Functor可以参考:
- ...
- 在计算机编程中
- 函数对象用于传递函数指针及其状态
- ...
- 在Haskell中,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
这些文档非常稀少。这些是什么?我什么时候应该使用它们?
答案 0 :(得分:2)
不要将算符视为'They're named "functors" to annoy category theory fanboys and language purists.':)
它们的用途主要是生成样板或模板代码。就像C ++模板是优化功能一样,因为泛型调度可能很慢,因此因子函数也是如此。
此处示例:
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
,因子会为您创建单词clear-person
,init-person
和person-datasource
。
何时使用它们?我认为除非你有性能问题保证使用,否则永远不会。他们对grepability非常不利。