如何使用镜头在Aeson.Object中的所有键/值对上进行concatMap [英] How to concatMap over all key/value pairs in an Aeson.Object using lenses
本文介绍了如何使用镜头在Aeson.Object中的所有键/值对上进行concatMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我一直在愚弄Control.Lens.Indexed中的组合器,尤其是 数据.Aeson.Lens.members
镜头:
I've been fooling around with the combinators in Control.Lens.Indexed, especially iconcatMap
, but I haven't been able to come-up with a function with the following type-sig using the Data.Aeson.Lens.members
lens:
func
-- list of key/value pairs, essentially
:: Aeson.Object
-- function for the concatMap operation to which the
-- key (Text) and value is passed
-> (Text -> Aeson.Value -> [a])
-- resultant concatenated list
-> [a]
推荐答案
The desired function is iconcatMapOf
. Also, note that members
traverses types with AsValue
instance, and there's no such instance for Object
, so we need to wrap it to Value
.
import qualified Data.Aeson as Aeson
import Control.Lens
import Data.Aeson.Lens
import Data.Text
func :: Aeson.Object -> (Text -> Aeson.Value -> [a]) -> [a]
func obj f = iconcatMapOf members f (Aeson.Object obj)
这篇关于如何使用镜头在Aeson.Object中的所有键/值对上进行concatMap的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文