I'm stuck trying to refactor code below into one that would be reusable in different contexts. I would like to print another triple, for "parent" sub-tree but without explicitly passing lens to each textFrom function.

Intuition tells me there should be a way to lift structure under Maybe (Text, Text, Text) to one that takes an argument and passes it everywhere.

{-# LANGUAGE OverloadedStrings #-}

import System.Environment
import Text.XML.Lens
import Text.XML
import Data.String
import Control.Lens
import Control.Applicative
import Data.Maybe
import Data.Text

main = do
  src:xs <- getArgs
  pom <- load src
  let textFrom name = listToMaybe $ pom ^.. root ./ ell name . text
      coordinates = (,,) <$> textFrom "groupId" <*> textFrom "artifactId" <*> textFrom "version"
  print $ coordinates  -- pass (pom ^.. root) here

load = Text.XML.readFile def . fromString
parent pom = pom ^.. root ./ ell "parent"
  • \$\begingroup\$ I went to #haskell after your last HXT question and was referred to github.com/silkapp/xml-arrow, maybe that's also something you'd like? \$\endgroup\$ – ferada Nov 28 '14 at 21:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.