问题
Im trying to learn F#
What I would like to do is download a webpage, split it into a sequence then find the index of an item and take the next 3 items after it.
Heres the code -- can someone show me what Im doing wrong please?
let find = "<head>"
let page = downloadUrl("http://www.stackoverflow.com")
let lines = seq ( page.Replace("\r", System.String.Empty).Split([|"\n"|], StringSplitOptions.RemoveEmptyEntries) )
let pos = lines |> Seq.findIndex(fun a -> a == find) // getting a Exception of type 'System.Collections.Generic.KeyNotFoundException' was thrown.
let result = // now to get the next 3 items
printfn "%A" (Seq.toList result);;
回答1:
So you are doing some F# text processing. Here are some possible problems:
After you downloaded the HTML page, you didn't do any preprocessing, say remove all HTML tags.
page.Replace("\r", System.String.Empty).Split([|"\n"|]
is problematic because I guess you want to split the items/words out. This line only splits lines out.let pos = lines |> Seq.findIndex(fun a -> a == find)
change==
to=
. In F#,=
is the boolean operator for comparison.let result = lines |> Seq.take pos
only takes the firstpos
items. You should skip these items and then takepos
items as in:
.
lines
|> Seq.skip (pos+1)
|> Seq.take 3
回答2:
let result = lines |> Seq.take pos
This line skips everything before the found item, not takes the 3 items after it.
EDIT: Seq.findIndex
fails if the item searched for doesn't exist. You want Seq.tryFindIndex
:
match lines |> Seq.tryFindIndex(fun a -> a == find) with
| Some pos -> let result = // now to get the next 3 items
printfn "%A" (Seq.toList result)
| None -> ()
来源:https://stackoverflow.com/questions/5786454/f-take-items-from-a-sequence