如何混合应用函子和箭头

发布于 2024-12-06 20:50:48 字数 2603 浏览 2 评论 0原文

我读过 Andrew Birkett 的博客 XML 的应用箭头 &&&回归纯粹,我们可以混合箭头和应用函子。

我自己尝试过,但没有达到我的预期。 我想要这个结果:

[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

但我得到这个:

[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "11111", origin = "333", alarm = "Sonde2"},
 Scenario {scenario = "11111", origin = "444", alarm = "Sonde1"},
 Scenario {scenario = "11111", origin = "444", alarm = "Sonde2"},
 Scenario {scenario = "22222", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "333", alarm = "Sonde2"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

我认为我的代码中有一个扭曲,但我不知道在哪里搜索。

下面是我的代码,如果有人可以提供一些帮助。

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}

import Text.XML.HXT.Core
import Control.Applicative
import Text.XML.HXT.Arrow.ReadDocument
import Data.Maybe
import Text.XML.HXT.XPath.Arrows
import Text.Printf


data Scenario = Scenario
  { scenario, origin, alarm    :: String
  }
  deriving (Show, Eq)


xml= "<DATAS LANG='en'>\
    \ <SCENARIO ID='11111'>\
    \   <ORIGIN ID='333'>\
    \       <SCENARIO_S ERR='0'></SCENARIO_S>\
    \       <SCENARIO_S ERR='2'></SCENARIO_S>\
    \       <ALARM_M NAME='Sonde1'></ALARM_M>\
    \   </ORIGIN>\
    \ </SCENARIO>\
    \ <SCENARIO ID='22222'>\
    \   <ORIGIN ID='444'>\
    \       <SCENARIO_S ERR='10'></SCENARIO_S>\
    \       <SCENARIO_S ERR='12'></SCENARIO_S>\
    \       <ALARM_M NAME='Sonde2'></ALARM_M>\
    \   </ORIGIN>\
    \ </SCENARIO>\
    \</DATAS>"

parseXML string = readString [ withValidate no
                         , withRemoveWS yes  -- throw away formating WS
                         ] string


parseVal tag name = WrapArrow $ getXPathTrees (printf "/DATAS/%s" tag) >>>  getAttrValue name

parseDatas = unwrapArrow $ Scenario <$> parseVal "SCENARIO"      "ID"
                                 <*> parseVal "SCENARIO/ORIGIN"        "ID"
                                 <*> parseVal "SCENARIO/ORIGIN/ALARM_M"        "NAME"

testarr1= runX (parseXML xml >>> parseDatas)

i read on Andrew Birkett’s blog Applicative arrows for XML &&& return to pure that we could mix arrows and applicative functors.

I tried it by my own but i don't have what i expect.
i would like this result:

[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

but i get this instead:

[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "11111", origin = "333", alarm = "Sonde2"},
 Scenario {scenario = "11111", origin = "444", alarm = "Sonde1"},
 Scenario {scenario = "11111", origin = "444", alarm = "Sonde2"},
 Scenario {scenario = "22222", origin = "333", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "333", alarm = "Sonde2"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde1"},
 Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

i think there is a twist in my code but i don't know where to search.

Below is my code if anyone can suggest some help.

{-# LANGUAGE Arrows, NoMonomorphismRestriction #-}

import Text.XML.HXT.Core
import Control.Applicative
import Text.XML.HXT.Arrow.ReadDocument
import Data.Maybe
import Text.XML.HXT.XPath.Arrows
import Text.Printf


data Scenario = Scenario
  { scenario, origin, alarm    :: String
  }
  deriving (Show, Eq)


xml= "<DATAS LANG='en'>\
    \ <SCENARIO ID='11111'>\
    \   <ORIGIN ID='333'>\
    \       <SCENARIO_S ERR='0'></SCENARIO_S>\
    \       <SCENARIO_S ERR='2'></SCENARIO_S>\
    \       <ALARM_M NAME='Sonde1'></ALARM_M>\
    \   </ORIGIN>\
    \ </SCENARIO>\
    \ <SCENARIO ID='22222'>\
    \   <ORIGIN ID='444'>\
    \       <SCENARIO_S ERR='10'></SCENARIO_S>\
    \       <SCENARIO_S ERR='12'></SCENARIO_S>\
    \       <ALARM_M NAME='Sonde2'></ALARM_M>\
    \   </ORIGIN>\
    \ </SCENARIO>\
    \</DATAS>"

parseXML string = readString [ withValidate no
                         , withRemoveWS yes  -- throw away formating WS
                         ] string


parseVal tag name = WrapArrow $ getXPathTrees (printf "/DATAS/%s" tag) >>>  getAttrValue name

parseDatas = unwrapArrow $ Scenario <
gt; parseVal "SCENARIO"      "ID"
                                 <*> parseVal "SCENARIO/ORIGIN"        "ID"
                                 <*> parseVal "SCENARIO/ORIGIN/ALARM_M"        "NAME"

testarr1= runX (parseXML xml >>> parseDatas)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夜巴黎 2024-12-13 20:50:48

正如 Rampion 所指出的,问题在于列表 monad 如何与 applicative 一起工作。看一下:

λ *Main > (+) <
gt; [1,2,3] <*> [1,2,3]
[2,3,4,3,4,5,4,5,6]

结果是 (+) 应用于 [1,2,3] 和 [1,2,3] 的笛卡尔积:结果列表有 9 个元素。

在您的代码中,parseVal "SCENARIO" "ID" 将返回包含 2 个元素的列表,parseVal "SCENARIO/ORIGIN" "ID"parseVal 也会返回“场景/ORIGIN/ALARM_M”“名称”。因此,结果将有 8 个元素。

相反,这就是我更改代码的方式:

--- parse a generic tag
parseVal tag name = WrapArrow $ getXPathTrees (printf "%s" tag) >>>  getAttrValue name

--- parse a "SCENARIO" xml element
parseScenario = unwrapArrow $ Scenario
        <
gt; (WrapArrow $ getAttrValue "ID")
        <*> (parseVal "SCENARIO/ORIGIN" "ID")
        <*> (parseVal "SCENARIO/ORIGIN/ALARM_M" "NAME")

--- parse the XML, extract a list of SCENARIOS and, for each, apply parseScenario
testarr1= runX (parseXML xml >>> getXPathTrees (printf "/DATAS/SCENARIO" ) >>> parseScenario)

结果如预期:

λ *Main > testarr1 
[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]

As pointed out by rampion, the problem is how the list monad works with applicative. Take a look at this:

λ *Main > (+) <
gt; [1,2,3] <*> [1,2,3]
[2,3,4,3,4,5,4,5,6]

The result is the carthesian product of (+) applied to [1,2,3] and [1,2,3]: the result list has 9 elements.

In your code, parseVal "SCENARIO" "ID" will return a list of 2 elements, and so will parseVal "SCENARIO/ORIGIN" "ID" and parseVal "SCENARIO/ORIGIN/ALARM_M" "NAME". Therefore, the result will have 8 elements.

Instead, this is how I would change your code:

--- parse a generic tag
parseVal tag name = WrapArrow $ getXPathTrees (printf "%s" tag) >>>  getAttrValue name

--- parse a "SCENARIO" xml element
parseScenario = unwrapArrow $ Scenario
        <
gt; (WrapArrow $ getAttrValue "ID")
        <*> (parseVal "SCENARIO/ORIGIN" "ID")
        <*> (parseVal "SCENARIO/ORIGIN/ALARM_M" "NAME")

--- parse the XML, extract a list of SCENARIOS and, for each, apply parseScenario
testarr1= runX (parseXML xml >>> getXPathTrees (printf "/DATAS/SCENARIO" ) >>> parseScenario)

The result is as desired:

λ *Main > testarr1 
[Scenario {scenario = "11111", origin = "333", alarm = "Sonde1"},Scenario {scenario = "22222", origin = "444", alarm = "Sonde2"}]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文