我有一个简单的树结构:
data Tree a = Leaf | Node a (Tree a) (Tree a)
和可折叠的实现:
import qualified Data.Foldable as F
instance F.Foldable Tree where
foldMap f Leaf = mempty
foldMap f (Node x l r) = F.foldMap f l `mappend`
f x `mappend`
F.
我有一个场景,其中我调用一个呈现树结构的索引操作,现在我希望一旦创建了树结构,我希望在该结构中嵌入我从js文件创建的另一个结构,该js文件是同一控制器中另一个操作的响应,例如:-
def index
@pages.Page.all
end
def get_more_page
@more_pages=Page.where(---some sql to fetch----)
respond_to do |format|
format.js
end
end
我真的很喜欢以通用的方式处理变形/变形的想法,但在我看来,它有一个明显的性能缺陷:
假设我们想要以分类的方式使用树结构-使用通用描述不同的折叠
newtype Fix f = Fix { unfix :: f (Fix f) }
data TreeT r = Leaf | Tree r r
instance Functor TreeT where
fmap f Leaf = Leaf
fmap f (Tree l r) = Tree (f l) (f r)
type Tree = Fix TreeT
catam :: (Functor f) =>