module Doukaku170 =
struct
open System
open System.IO
type sex = F | M | U
type meal = { Morning : string;
Lunch : string;
Dinner : string }
type data = { LastName : string;
FirstName : string;
Sex : sex;
Age : int;
Year : int;
Month : int;
MealDays : (int * meal) list }
let get_datas file =
let (|Sex|) s = if s = "F" then F else if s = "M" then M else U in
let step_read n (r:#TextReader) =
let buffer = Array.zero_create n in
ignore (r.Read(buffer, 0, n));
String.trim [' '] (new string(buffer))
in
{ use r = new StreamReader(File.OpenRead(file)) in
while not r.EndOfStream do
yield { LastName = step_read 12 r;
FirstName = step_read 12 r;
Sex = (match step_read 1 r with Sex x -> x);
Age = Int32.Parse(step_read 3 r);
Year = Int32.Parse(step_read 4 r);
Month = Int32.Parse(step_read 2 r);
MealDays = [for _ in 1..31
-> (Int32.Parse(step_read 2 r),
{ Morning = step_read 500 r;
Lunch = step_read 500 r;
Dinner = step_read 500 r })] }
done }
end;;
let _ =
let datas = @"C:\test.txt" |> Doukaku170.get_datas |> Seq.to_list in
printf "%A\n" (List.hd datas);;
いげ太
#6097()
[
Other
]
Rating0/0=0.00
うーん。もうちょっとキレイに書けるかと思ったけど挫折した。。
あ、このお題、実用的でいいお題だなと思いました!
module Doukaku170 = struct open System open System.IO type sex = F | M | U type meal = { Morning : string; Lunch : string; Dinner : string } type data = { LastName : string; FirstName : string; Sex : sex; Age : int; Year : int; Month : int; MealDays : (int * meal) list } let get_datas file = let (|Sex|) s = if s = "F" then F else if s = "M" then M else U in let step_read n (r:#TextReader) = let buffer = Array.zero_create n in ignore (r.Read(buffer, 0, n)); String.trim [' '] (new string(buffer)) in { use r = new StreamReader(File.OpenRead(file)) in while not r.EndOfStream do yield { LastName = step_read 12 r; FirstName = step_read 12 r; Sex = (match step_read 1 r with Sex x -> x); Age = Int32.Parse(step_read 3 r); Year = Int32.Parse(step_read 4 r); Month = Int32.Parse(step_read 2 r); MealDays = [for _ in 1..31 -> (Int32.Parse(step_read 2 r), { Morning = step_read 500 r; Lunch = step_read 500 r; Dinner = step_read 500 r })] } done } end;; let _ = let datas = @"C:\test.txt" |> Doukaku170.get_datas |> Seq.to_list in printf "%A\n" (List.hd datas);;Rating0/0=0.00-0+
[ reply ]