This project is read-only.
Using .reduce

Category:
Functions - Arrays, Lists, Seqs
Description: .reduce applies a given function to the collection, threading an accumulator argument through and returning a single value.
Code:
let AggregateExample3() =
let aoList2 = [ for i in 1..5 -> i*i ]
let add acc item = acc + item // accumulator function
let sum = List.reduce add aoList2

let names = [| "A"; "man"; "landed"; "on"; "the"; "moon" |]
let insertComma (acc: string) item = acc + " " + item
let sentence = Array.reduce insertComma names

printfn "sum = %d" sum
printfn "\nsentence = %s" sentence

Execution Result:
sum = 55

sentence = A man landed on the moon

Last edited Sep 14, 2011 at 3:53 AM by ttliu2000, version 1

Comments

No comments yet.