다음을 통해 공유


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

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

Parameters

  • projection
    Type: 'T -> ^U

    A function applied to transform each element of the sequence.

  • source
    Type: seq<'T>

    The input sequence.

Exceptions

Exception

Condition

ArgumentException

Thrown when the input sequence has zero elements.

ArgumentNullException

Thrown when the input sequence is null.

Return Value

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

Remarks

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.

Example

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

Output

Average: 5.500000
Average: 5.500000

Platforms

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

Reference

Collections.Seq Module (F#)

Microsoft.FSharp.Collections Namespace (F#)