F# take items from a sequence

≯℡__Kan透↙ 提交于 2019-12-11 13:00:42

问题


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:

  1. After you downloaded the HTML page, you didn't do any preprocessing, say remove all HTML tags.

  2. 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.

  3. let pos = lines |> Seq.findIndex(fun a -> a == find) change == to =. In F#, = is the boolean operator for comparison.

  4. let result = lines |> Seq.take pos only takes the first pos items. You should skip these items and then take pos 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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!