﻿ 数据结构 – 透镜,fclabels,数据访问器 – 用于结构访问和突变的库更好 - 代码日志

#### 数据结构 – 透镜,fclabels,数据访问器 – 用于结构访问和突变的库更好

``````data Lens a b = Lens (a -> b) (b -> a -> a)
``````

``````get (Lens g _) = g
put (Lens _ s) = s
``````

``````get l (put l b a) = b
``````

``````put l (get l a) a = a
``````

``````put l b1 (put l b2 a) = put l b1 a
``````

fclabels

fclabels也许是最容易推理的镜头库，因为它的a： – > b可以直接转换为上述类型。它为(： – >)提供了一个Category实例，这是有用的，因为它允许你组成镜头。它还提供了一个无法统计的点类型，概括了这里使用的镜头的概念，和一些管道处理同构。

``````newtype T r a = Cons { decons :: a -> r -> (a, r) }
``````

``````newtype Lens s t = Lens (forall a. State t a -> State s a)
``````

``````newtype Lens a b = Lens (a -> Store b a)
``````

``````data Store b a = Store (b -> a) b
``````

``````newtype Lens a b = Lens (a -> (b, b -> a))
``````

``````extract . l = id
duplicate . l = fmap l . l
``````

``````data Iso a b = Iso { hither :: a -> b, yon :: b -> a }
``````

``````data Lens a b = forall c. Lens (Iso a (b,c))
``````

van Laarhoven镜片

``````type Lens a b = forall f. Functor f => (b -> f b) -> a -> f a
``````

``````_2 f (a,b) = (,) a <\$> f b
``````

``````type LensFamily a b c d = forall f. Functor f => (c -> f d) -> a -> f b
``````

``````_2 :: Functor f => (a -> f b) -> (c,a) -> f (c, b)
``````