我不明白因子的函子。我想这将有助于首先理解什么是“函子”。
谷歌说:
函数;操作员
在因子中,所有的函数(词)都是运算符,都是一流的.(事实上,我想不出多少不是头等舱的因素)。这个定义没什么用。
维基百科说:
工作人员可指:
"function“的页面上写着:
要调用或调用的对象,就好像它是一个普通函数一样,通常具有相同的语法(也可以是函数的函数参数)。
函子是一级函数吗?这并不是什么特别的事情,而且不管怎么说,词语、引语和东西在“因素”中已经是一流的了.
因子函子有奇怪的语法,让我想起泛型或什么的。
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这些文档极其稀少。他们是什么?我什么时候应该使用它们?
发布于 2016-06-10 13:29:01
不要把函子看作是“他们被命名为”函子“,以惹恼范畴理论、粉丝和语言纯粹主义者。” :)
它们主要用于生成样板或模板代码。就像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。
什么时候使用它们?我认为永远不会,除非你有性能问题,值得他们使用。它们对贪婪性非常不利。
https://stackoverflow.com/questions/37726477
复制相似问题