问题描述
我从 https://www.stackage.org/haddock/lts-12.1/lens-aeson-1.0.2/Data-Aeson-Lens.html 和 https://www.stackage.org/haddock/lts-12.1/lens-4.16.1/Control-Lens-Fold.html 试图弄清楚如何编写一个表达式,使我可以构造以下类型的东西:
I've been starting at https://www.stackage.org/haddock/lts-12.1/lens-aeson-1.0.2/Data-Aeson-Lens.html and https://www.stackage.org/haddock/lts-12.1/lens-4.16.1/Control-Lens-Fold.html trying to figure out how to write an expression that allows me to construct something of the following type:
import Data.Aeson as A
functionIWant
:: (Vector A.Value)
-> (A.Value -> [a])
-> [a]
推荐答案
有两种镜头功能可用于将与常规 Foldable
相关的功能提升"到镜头世界:
There are two lens functions that are used to "lift" regular Foldable
-related functions to the lensy world:
folded
that folds over all the elements of aFoldable
container, in this case it would be theVector
.folding
that lifts a function that returns aFoldable
result.
您可以将它们组合成新的 Fold
,然后使用 toListOf
以获得最终列表.
You can compose them into a new Fold
and then use toListOf
to obtain the final list.
对于 Traversable
,还有一个 folded
的类似物,称为 已遍历
.
There is also an analogue of folded
for Traversable
, called traversed
.
这篇关于通过镜头折叠或连接Aeson阵列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!