Advertisement
TheFaceTakt

lab3

May 15th, 2016
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
F# 2.38 KB | None | 0 0
  1. #r "/home/thefacetakt/Downloads/fsdata/fsharp-FSharp.Data-46c2766/bin/FSharp.Data.dll"
  2. open FSharp.Data
  3.  
  4. let implode x = List.fold (fun str x -> str + x.ToString()) "" x
  5.  
  6. let explode (s:string) =
  7.     [for c in s -> c]
  8.  
  9. let wikipedia = "https://en.wikipedia.org"
  10.  
  11.  
  12. let languagesUls = HtmlDocument.Load("https://en.wikipedia.org/wiki/List_of_programming_languages").Descendants ["ul"] |> Seq.skip 4 |> Seq.take 26
  13.  
  14. let languages = (languagesUls
  15.     |> Seq.map (fun x -> x.Descendants ["a"])
  16.     |> Seq.map ( Seq.choose ( fun x -> x.TryGetAttribute("href")  |> Option.map (fun a -> x.InnerText(), a.Value())))) |> Seq.concat
  17.    
  18. let parseYear s =
  19.     let rec parseYear' = function
  20.        |[] -> []
  21.        |a::b::c::d::xs when (List.filter System.Char.IsDigit [a; b; c; d])
  22.                         |> List.length
  23.                         |> (=) 4 -> [a; b; c; d]
  24.        |x::xs -> parseYear' xs
  25.     s |> explode |> parseYear' |> implode |> int
  26.  
  27.  
  28. let firstAppeared = "First" + (char 160).ToString() + "appeared"
  29.  
  30. let takeFirst = function
  31.    |x when (Seq.length x > 0) -> Some(Seq.nth 0 x)
  32.    |_ -> None
  33.  
  34. let processLanguage (name, link) = async {
  35.    let! page = HtmlDocument.AsyncLoad(wikipedia + link)
  36.  
  37.    return (name, page.Descendants["table"]
  38.                 |> Seq.choose (fun x -> x.TryGetAttribute("class") |> Option.map (fun y -> if y.Value() = "infobox vevent" then Some(x) else None))
  39.                 |> Seq.map (Option.map (fun table ->
  40.                     table.Descendants ["tr"]
  41.                     |> Seq.filter (fun x -> x.Descendants ["th"]
  42.                                             |> Seq.exists (fun y -> y.InnerText() = firstAppeared))))
  43.                 |> Seq.choose (fun x -> x)
  44.                 |> Seq.concat
  45.                 |> Seq.map (fun x -> x.Descendants ["td"]
  46.                                      |> takeFirst
  47.                                      |> Option.map (fun x-> parseYear (x.InnerText())))
  48.                 |> Seq.choose (fun x -> x)
  49.                 |> takeFirst)
  50. }
  51.  
  52. let res = languages
  53.            |> Seq.map processLanguage
  54.            |> Async.Parallel
  55.            |> Async.RunSynchronously
  56.            |> Array.toList
  57.            |> List.choose (fun x ->
  58.                                match x with
  59.                                | (a, Some y) -> Some(y, a)
  60.                                | _ -> None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement