Using .iter

Category:
Functions - Arrays, Lists, Seqs
Description: .iter applies a given function to each element in the collection, returning a value of unit ().
Code:
let AggregateExample1() =
let aoList = [ for i in 1..5 -> (i, i*i) ]
let aoArray = [| for i in 1..5 -> (i, i*i) |]
let aoSeq = seq { for i in 1..5 -> (i, i*i) }

printf "aoList: "
List.iter (fun (a,b) -> printf "(%d, %d) " a b) aoList
printf "\naoArray: "
Array.iter (fun (a,b) -> printf "(%d, %d) " a b) aoArray
printf "\naoSeq: "
Seq.iter (fun (a,b) -> printf "(%d, %d) " a b) aoSeq

Execution Result:
aoList: (1, 1) (2, 4) (3, 9) (4, 16) (5, 25) 
aoArray: (1, 1) (2, 4) (3, 9) (4, 16) (5, 25)
aoSeq: (1, 1) (2, 4) (3, 9) (4, 16) (5, 25)

Using .map

Category:
Functions - Arrays, Lists, Seqs
Description: .map applies a given function to each element in the collection, returning a new collection.
Code:
let AggregateExample2() =
// can be applied to arrays and sequences as well
let aoList2 = List.map (fun (a,b,c) ->
let date = new System.DateTime(c, a, b)
date.ToString("F")) [(1,1,2001); (2,2,2004); (6,17,2009)]

for i in aoList2 do printfn "%A" i

Execution Result:
"Monday, January 01, 2001 12:00:00 AM"
"Monday, February 02, 2004 12:00:00 AM"
"Wednesday, June 17, 2009 12:00:00 AM"

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

Using .filter

Category:
Functions - Arrays, Lists, Seqs
Description: .filter applies a given function to the collection, returning only those elements for which the given predicate returns true.
Code:
let AggregateExample4() =
let names = [|"Bob"; "Jim"; "Stephen"; "James"; "Fred"; "Will"; "Brian"; "David"; "Mark"; "Jonathan"|]
let longNames = Array.filter (fun (x: string) -> x.Length > 4) names

printfn "names = %A\n" names
printfn "longNames = %A" longNames

Execution Result:
names = [|"Bob"; "Jim"; "Stephen"; "James"; "Fred"; "Will"; "Brian"; "David"; "Mark";
"Jonathan"|]

longNames = [|"Stephen"; "James"; "Brian"; "David"; "Jonathan"|]

Using .choose

Category:
Functions - Arrays, Lists, Seqs
Description: .choose applies a given function to the collection, returning an array of results x where the function evaluates to Some(x).
Code:
let AggregateExample5() =
let numbers = seq {1..20}
let evens = Seq.choose(fun x ->
match x with
| x when x%2=0 -> Some(x)
| _ -> None ) numbers
printfn "numbers = %A\n" numbers
printfn "evens = %A" evens

//--------------------------------------------------------

Execution Result:
numbers = seq [1; 2; 3; 4; ...]

evens = seq [2; 4; 6; 8; ...]

Last edited Sep 12, 2011 at 11:34 PM by ttliu2000, version 1

Comments

No comments yet.