Last active
November 4, 2016 11:08
-
-
Save nenono/3dc373ec59eea4ca3282f1b459be8936 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
type subject = {point: int} | |
type student = {subjects: subject list} | |
type classroom = {students: student list} | |
let data = {students = [ | |
{subjects = [{point = 10}; {point = 20}]} | |
{subjects = [{point = 20}; {point = 30}]} | |
{subjects = [{point = 30}; {point = 40}]} | |
{subjects = [{point = 40}; {point = 50}]} | |
]} | |
// クラス合計 | |
let classSum = | |
data.students | |
|> List.collect (fun x -> x.subjects) | |
|> List.sumBy (fun x -> x.point) | |
// 生徒ごとの合計 | |
let studentsSum = | |
data.students | |
|> List.map (fun x -> | |
x.subjects | |
|> List.sumBy (fun x -> x.point)) | |
// おまけでseq式 | |
let classSum2 = | |
seq { | |
for x in data.students do | |
for x in x.subjects do | |
yield x.point | |
} | |
|> Seq.sum | |
let studentsSum2 = | |
seq { | |
for x in data.students do | |
yield seq { | |
for x in x.subjects do | |
yield x.point | |
} |> Seq.sum | |
} |> Seq.toList |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment