Finding the Missing 'Reduce' Typeclass from Finger Tree Article

孤人 提交于 2019-12-05 02:01:23

Given that reduce is a common alternate name for a "fold" function, I'd guess that it's something similar to the Foldable type class. The instance definitions seem to make sense as such, as well.

The Foldable class can be defined using just foldr, which has the type signature foldr :: (Foldable t) => (a -> b -> b) -> b -> t a -> b, whereas the reducer in that code appears to be reducer :: (Reduce t) => (a -> b -> b) -> t a -> b -> b. Other than a different argument order, it should work the same.

Note that the operators are just arguments to the function--you could replace them all with f or another similarly generic identifier. Using an operator as the name of a binary function argument is... a slightly unusual choice, but it does emphasize some aspects of the structure of the fold, I guess.

It's defined in the paper linked in the article: Finger Trees: A Simple General-purpose Data Structure.

class Reduce f where
  reducer :: (a -> b -> b) -> (f a -> b -> b)
  reducel :: (b -> a -> b) -> (b -> f a -> b)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!