2016-08-04 33 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