Table of contents
Collapse the table of content
Expand the table of content

Seq.averageBy<'T,^U> Function (F#)

Den Delimarsky|Last Updated: 8/18/2017
1 Contributor

Returns the average of the results generated by applying the function to each element of the sequence.

Namespace/Module Path: Microsoft.FSharp.Collections.Seq

Assembly: FSharp.Core (in FSharp.Core.dll)


// Signature:
Seq.averageBy : ('T -> ^U) -> seq<'T> -> ^U (requires ^U with static member (+) and ^U with static member DivideByInt and ^U with static member Zero)

// Usage:
Seq.averageBy projection source


projection Type: 'T -> ^U

A function applied to transform each element of the sequence.

source Type: seq<'T>

The input sequence.


ArgumentExceptionThrown when the input sequence has zero elements.
ArgumentNullExceptionThrown when the input sequence is null

Return Value

The average of the results of applying the projection function to sequence elements.


The elements are averaged using the + operator, DivideByInt method and Zero property associated with the generated type.

This function is named AverageBy in compiled assemblies. If you are accessing the function from a language other than F#, or through reflection, use this name.


The following code example shows how to use Seq.averageBy,also compares Seq.average and Seq.averageBy.

// You can use Seq.average to average elements of a list, array, or sequence.
let average1 = Seq.average [ 1.0 .. 10.0 ]
printfn "Average: %f" average1
// To average a sequence of integers, use Seq.averageBy to convert to float.
let average2 = Seq.averageBy (fun elem -> float elem) (seq { 1 .. 10 })
printfn "Average: %f" average2


Average: 5.500000
Average: 5.500000


Windows 8, Windows 7, Windows Server 2012, Windows Server 2008 R2

Version Information

F# Core Library Versions

Supported in: 2.0, 4.0, Portable

See Also

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)

© 2020 Microsoft