镜头/棱镜具有错误处理

编程入门 行业动态 更新时间:2024-10-22 19:44:01
本文介绍了镜头/棱镜具有错误处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

假设我有一对转换函数

string2int :: String -> Maybe Int int2string :: Int -> String

我可以使用Optics轻松地表示这些.

I could represent these fairly easily using Optics.

stringIntPrism :: Prism String Int

但是,如果要表示失败原因,则需要将它们保留为两个单独的函数.

However if I want to represent failure reason, I'd need to keep these as two separate functions.

string2int :: String -> Validation [ParseError] Int int2string :: Int -> String`

对于这个简单的示例,Maybe非常好,因为我们总是可以假设失败是解析失败,因此我们实际上不必使用Either或Validation类型对其进行编码.

For this simple example Maybe is perfectly fine, since we can always assume that a failure is a parse failure, thus we don't actually have to encode this using an Either or Validation type.

但是,想像一下,除了解析Prism之外,我还想进行一些验证

However imagine, in addition to my parsing Prism, I want to perform some validation

isOver18 :: Int -> Validation [AgeError] Int isUnder55 :: Int -> Validation [AgeError] Int

理想的是能够将这些内容组合在一起,这样我就可以拥有

It would be ideal to be able compose these things together, such that I could have

ageField = isUnder55 . isOver18 . string2Int :: ValidationPrism [e] String Int

这是手工建造的琐碎工作,但是,似乎很普通的概念是,在镜片/光学领域可能已经潜藏了一些东西.是否存在可以处理此问题的抽象?

This is fairly trivial to build by hand, however it seems like a common enough concept that there might be something lurking in the field of Lenses/Optics that does this already. Is there an existing abstraction that handles this?

tl; dr

是否存在实现可在任意函子上进行参数化而不是直接绑定到Maybe的部分透镜/棱镜/iso的标准方法?

Is there a standard way of implementing a partial lens / prism / iso that can be parameterised over an arbitrary functor instead of being tied directly to Maybe?.

我已经在上面使用了Haskell表示法,因为它更直接,但是我实际上是在Scala中使用Monocle来实现这一点.不过,对于特定于ekmett的Lens库的答案,我会感到非常满意.

I've used Haskell notation above since it's more straight forward, however I'm actually using Monocle in Scala to implement this. I would, however, be perfectly happy with an answer specific to i.e. ekmett's Lens library.

推荐答案

我最近写了有关索引光学的博客文章;对此进行了一些探讨,我们也可以做共折射率光学.

I have recently written a blog post about indexed optics; which explores a bit how we can do coindexed optics as well.

简而言之:可以使用共折射率光学,但是我们尚未在那里做进一步的研究.特别是,因为如果我们尝试将这种方法转换为镜头的lens编码(从Profunctor到VL),它将变得更加毛茸茸(但我认为我们只能使用 7个类型变量)

In short: Coindexed-optics are possible, but we have yet to do some further research there. Especially, because if we try to translate that approach into lens encoding of lenses (from Profunctor to VL) it gets even more hairy (but I think we can get away with only 7 type-variables).

我们不能真正做到这一点,而无需更改当前在lens中对分度光学元件的编码方式.因此,现在,您最好使用特定于验证的库.

And we cannot really do this without altering how indexed optics are currently encoded in lens. So for now, you'll better to use validation specific libraries.

提示困难之处:当我们尝试与Traversal组成时,我们应该拥有

To give a hint of the difficulties: When we try to compose with Traversals, should we have

-- like `over` but also return an errors for elements not matched validatedOver :: CoindexedOptic' s a -> (a -> a) -> s -> (ValidationErrors, s)

还是其他?如果我们只能撰写Coindexed Prisms,那么它们的价值就不能证明其复杂性.他们不会适应"光学框架.

or something else? If we could only compose Coindexed Prisms their value won't justify their complexity; they won't "fit" into optics framework.

更多推荐

镜头/棱镜具有错误处理

本文发布于:2023-11-15 04:03:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1590973.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:棱镜   镜头   错误

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!