本文介绍了如何使用镜头在Aeson.Object中的所有键/值对上进行concatMap的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在愚弄Control.Lens.Indexed中的组合器,尤其是 iconcatMap ,但是我无法使用 数据.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]

推荐答案

所需的功能是 iconcatMapOf .另外,请注意, members 会使用 AsValue 实例遍历类型,并且 Object 没有此类实例,因此我们需要将其包装到.

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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-23 21:18