-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmakeTable.fsx
More file actions
33 lines (30 loc) · 1.24 KB
/
makeTable.fsx
File metadata and controls
33 lines (30 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
(*
【お題】
[1, 2, 3, 4, 5, 6, 7]のようなリストを
次のような表に整形して出力せよ
<table>
<tr><td>1</td><td>2</td><td>3</td></tr>
<tr><td>4</td><td>5</td><td>6</td></tr>
<tr><td>7</td><td> </td><td> </td></tr>
</table>
*)
open System.Text
let makeTable columnCount xs =
let addHeader (sb : StringBuilder) = sb.AppendLine "<table>"
let addFooter (sb : StringBuilder) = sb.AppendLine "</table>"
let addRow sb xs =
let addRowStart (sb : StringBuilder) = sb.Append "<tr>"
let addRowEnd (sb : StringBuilder) = sb.AppendLine "</tr>"
let xs = xs |> Seq.map (function Some x -> x |> box |> string | None -> " ")
(sb |> addRowStart, xs) ||> Seq.fold (fun sb column -> sb.Append("<td>").Append(column).Append("</td>"))
|> addRowEnd
let split count xs =
let rec split xs = seq {
if xs |> Seq.head |> Option.isSome then
yield xs |> Seq.take count
yield! split (Seq.skip count xs) }
xs |> Seq.map Some |> Seq.cache |> Seq.append <| Seq.initInfinite (fun _ -> None)
|> split
let rows = xs |> split columnCount
let sb = (StringBuilder() |> addHeader, rows) ||> Seq.fold addRow |> addFooter
sb.ToString()