2016-08-04 29 views
2

我是F#的全新品牌,所以我很抱歉如果我使用不正确的名称作为事情。使用管道向前嵌套序列

我试图使用F#分析一个网页看起来是这样的:

<!--This is simplified, in reality there are more divs, anchors etc. --> 
<html> 
<body> 
    <div class="pr-single"><a href="http://google.ca">Google</a></div> 
    <div class="pr-single"><a href="http://apple.com">Apple</a></div> 
    <div class="pr-single"><a href="http://microsoft.com">Microsoft</a></div> 
</body> 
</html> 

我已经声明的类型

type PromoterPage = FSharp.Data.HtmlProvider<"http://somewebpage.com"> 

现在我试图让页面上所有链接的列表。我的思维过程是:

  1. 获取所有类名外的div
  2. 获取所有这些div的后代
  3. 收集这些后代成平面列表
  4. 过滤该列表下降到仅<a>标签

我在此尝试如下:

let GetFirst (page:PromoterPage) = 
    page.Html.Descendants() 
    |> Seq.filter(fun n -> n.HasClass("pr-single"))     //Find the divs 
    |> Seq.map(fun n -> n.Descendants())       //Get the descendants 
    |> Seq.collect(fun n -> n |> Seq.where(fun m -> m.HasName("a")) //Filter and collect the anchors 

这个问题似乎是你不能嵌套Seq函数或我做得不对。我收到的错误:

Incomplete values or function definition. If this is an expression, the body of the expression must be indented to the same column as the keyword.

我可以巢Seq功能,我想在这里的呢?我是否以错误的方式思考这个问题?

回答

5

你缺少右括号:

|> Seq.collect(fun n -> n |> Seq.where(fun m -> m.HasName("a"))) 

Can I nest Seq functions the way I'm trying to here?

是的,这是完全没有用的lambda表达式管道嵌套函数。不过,我经常将它们放到本地函数中,因为它可以使代码长期可读。

+0

哇,这很尴尬。 :) 谢谢! – JoshVarty