2016-08-04 7 views
2

私はF#のまったく新しいものなので、間違った名前を使用していると謝ります。私はタイプにネストされたシーケンスでパイプを使用する

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

を宣言した

<!--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> 

そして今、私が取得しようとしている:私はこのようになり、Webページを解析するためのF#を使用しようとしている

ページ上のすべてのリンクのリスト私の思考プロセスだった:

  1. だけにダウンフラットリスト
  2. フィルターこのリストの中にこれらの子孫を集め
  3. これらのdiv要素のすべての子孫を取得し、クラス名によって、すべての外側のdivを取得この時<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?

はい、それはラムダにおける配管とネスト機能に完全に罰金です。しかし、私はしばしばそれらをローカル関数に引き出します。なぜなら、コードを長期にわたって読みやすくすることができるからです。

+0

うわー、恥ずかしいです。 :)ありがとう! – JoshVarty

関連する問題