Search code examples
jsonhaskellhaskell-lensaeson

Haskell - Combine results from separate lenses


I'm trying to accomplish something like the following:

jsonStr = "{\"a\": \"hello\", 
            \"b\": [\"world\", \"everyone\", \"42\"]}"
someALens = ...
someBLens = ...
combinedJson = jsonStr ... someALens ... someBLens

to get as a result:

combinedJson == ["hello world", "hello everyone", "hello 42"]

However, the combination operators I've been finding (like <>~) seem to require a set value to mappend (or otherwise combine) over a lens. How could I create a compound lens which can combine values from multiple lenses?


Solution

  • As discussed in the comments, the answer is the rather-anticlimactic jsonStr ^.. (someALens <> someBLens). Two getters mappend into a fold. Monoids, our friends forever.