有时需要执行一些复杂的例程来检索或保存正在处理的数据.在这种情况下,人们想要分离数据生成和数据处理逻辑.常见的方法是使用类似iteratee的功能.有很多不错的图书馆:管道,管道等.在大多数情况下,他们会做这件事.但是AFAIK它们(除了,可能是管道)受到处理顺序的限制.
但考虑一个日志查看器示例:人类可能希望随机来回漫步.他也可以放大和缩小.我担心迭代者在这里无能为力.
一个简单的解决方案可能如下所示:
-- True is for 'right', 'up', etc. and vice versa
type Direction = Bool
class Frame (f :: * -> *) where
type Dimension f :: *
type Origin f :: * -> *
grow', shrink' move' :: Monad m => Dimension f -> Direction -> f a -> m (f a)
move' dim dir f = grow' dim dir f >>= shrink' dim (not dir)
liftF' :: (Origin f a -> b) -> f a -> …
Run Code Online (Sandbox Code Playgroud) 有时需要将空类型传递给某个模板.例如:
template <typename X, typename Y>
struct BoundaryConditions {
X x; Y y;
BoundaryConditions(typename X::init xi, typename Y::init yi) : x(xi), y(yi) {
...
}
};
Run Code Online (Sandbox Code Playgroud)
我们可能希望实现不带任何参数的自由边界条件.使用类型检查实现这样的事情非常容易:
struct Nothing {};
Nothing nothing = Nothing();
struct Free {
typedef Nothing init;
...
};
BoundaryConditions<Free, Fixed> foo(nothing, 100);
Run Code Online (Sandbox Code Playgroud)
所以我的问题是:在标准库或者提升中是否有类似我的"Nothing"类型的实现?
每一种类型牛逼超载Traversable的带来了一个拉链牛逼.即Traversable T实例的存在是Zipper T的充分条件.
有证据证明这也是一个必要条件吗?(我想它一定是非常微不足道的,但到目前为止我还没有找到拉链的正式的一般定义.)
我重新发明了某种"状态箭头":
import Prelude hiding (id, (.))
import Control.Monad.State
import Control.Arrow
import Control.Category
data StateA s a b = StateA {runStateA :: s -> a -> (b, s)}
instance Category (StateA s) where
id = StateA (\s a -> (a, s))
(StateA f) . (StateA g) = StateA $ \s x -> let (b, s') = g s x in f s' b
instance Arrow (StateA s) where
arr f = StateA $ \s a -> (f a, s)
first (StateA …
Run Code Online (Sandbox Code Playgroud)