Joe*_*Joe 15 haskell functional-programming scala lenses monocle-scala
Many types of optics have a van Laarhoven representation.
For example, a Lens of type Lens s t a b can be represented as:
Functor f => (a -> f b) -> s -> f t
Run Code Online (Sandbox Code Playgroud)
Similarly a Traversal, can be represented in a similar way, swapping the Functor constraint for Applicative:
Applicative f => (a -> f b) -> s -> f t
Run Code Online (Sandbox Code Playgroud)
几种光学框架(例如Monocle和Arrow)定义了一种类型Optional。
在《单片眼镜光学》中,层次结构 Optional介于Lens和之间Traversal
根据我的理解:如果Traversal是像一个Lens可能具有零到多个目标,那么Optional就像是一个Lens可能具有零到一的目标。
在Monocle中,Optional定义为一对功能:
getOrModify :: s -> Either t a
set :: (b, s) -> t
Run Code Online (Sandbox Code Playgroud)
Monocle源代码中的注释表明,也有可能将“表示Optional为越来越PLens弱PPrism”
是否可以将Optionalvan Laarhoven函数表示为?
Car*_*arl 10
如果Functor / Applicative / Monad层次结构更细粒度,则将有一种表示方式。特别是:
class Functor f => Pointed f where
pure :: a -> f a
type Optional s t a b = forall f. Pointed f => (a -> f b) -> s -> f t
Run Code Online (Sandbox Code Playgroud)
请注意,Affine如果在类层次结构中巧妙地将其命名,则可以在镜头库中命名该类型。
| 归档时间: |
|
| 查看次数: |
557 次 |
| 最近记录: |