我正在努力理解“anamorphism”的概念。
在函数式编程中,anamorphism是对列表展开概念的一般化。形式上,anamorphisms是通用函数,可以递归地构造某种类型的结果,并且由决定构造下一个单步的函数参数化。
它的对偶,catamorphism,在这篇文章中得到了很好的描述:What is a catamorphism and can it be implemented in C# 3.0?。
C#中catamorphic行为的一个很好的例子是LINQ的Aggregate方法。
那么,anamorphic等效物会是什么呢?是否正确认为伪随机数生成器Random是一个anamorphic构造,或者展开过程总是包括像下面这个累加器函数(代码片段取自Intro to Rx)?
在函数式编程中,anamorphism是对列表展开概念的一般化。形式上,anamorphisms是通用函数,可以递归地构造某种类型的结果,并且由决定构造下一个单步的函数参数化。
它的对偶,catamorphism,在这篇文章中得到了很好的描述:What is a catamorphism and can it be implemented in C# 3.0?。
C#中catamorphic行为的一个很好的例子是LINQ的Aggregate方法。
那么,anamorphic等效物会是什么呢?是否正确认为伪随机数生成器Random是一个anamorphic构造,或者展开过程总是包括像下面这个累加器函数(代码片段取自Intro to Rx)?
IEnumerable<T> Unfold<T>(T seed, Func<T, T> accumulator)
{
var nextValue = seed;
while (true)
{
yield return nextValue;
nextValue = accumulator(nextValue);
}
}