Skip to content

Instantly share code, notes, and snippets.

@nenono
Last active November 4, 2016 11:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nenono/3dc373ec59eea4ca3282f1b459be8936 to your computer and use it in GitHub Desktop.
Save nenono/3dc373ec59eea4ca3282f1b459be8936 to your computer and use it in GitHub Desktop.
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