Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #r "/home/thefacetakt/Downloads/fsdata/fsharp-FSharp.Data-46c2766/bin/FSharp.Data.dll"
- open FSharp.Data
- let implode x = List.fold (fun str x -> str + x.ToString()) "" x
- let explode (s:string) =
- [for c in s -> c]
- let wikipedia = "https://en.wikipedia.org"
- let languagesUls = HtmlDocument.Load("https://en.wikipedia.org/wiki/List_of_programming_languages").Descendants ["ul"] |> Seq.skip 4 |> Seq.take 26
- let languages = (languagesUls
- |> Seq.map (fun x -> x.Descendants ["a"])
- |> Seq.map ( Seq.choose ( fun x -> x.TryGetAttribute("href") |> Option.map (fun a -> x.InnerText(), a.Value())))) |> Seq.concat
- let parseYear s =
- let rec parseYear' = function
- |[] -> []
- |a::b::c::d::xs when (List.filter System.Char.IsDigit [a; b; c; d])
- |> List.length
- |> (=) 4 -> [a; b; c; d]
- |x::xs -> parseYear' xs
- s |> explode |> parseYear' |> implode |> int
- let firstAppeared = "First" + (char 160).ToString() + "appeared"
- let takeFirst = function
- |x when (Seq.length x > 0) -> Some(Seq.nth 0 x)
- |_ -> None
- let processLanguage (name, link) = async {
- let! page = HtmlDocument.AsyncLoad(wikipedia + link)
- return (name, page.Descendants["table"]
- |> Seq.choose (fun x -> x.TryGetAttribute("class") |> Option.map (fun y -> if y.Value() = "infobox vevent" then Some(x) else None))
- |> Seq.map (Option.map (fun table ->
- table.Descendants ["tr"]
- |> Seq.filter (fun x -> x.Descendants ["th"]
- |> Seq.exists (fun y -> y.InnerText() = firstAppeared))))
- |> Seq.choose (fun x -> x)
- |> Seq.concat
- |> Seq.map (fun x -> x.Descendants ["td"]
- |> takeFirst
- |> Option.map (fun x-> parseYear (x.InnerText())))
- |> Seq.choose (fun x -> x)
- |> takeFirst)
- }
- let res = languages
- |> Seq.map processLanguage
- |> Async.Parallel
- |> Async.RunSynchronously
- |> Array.toList
- |> List.choose (fun x ->
- match x with
- | (a, Some y) -> Some(y, a)
- | _ -> None)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement