27

What is the easiest way to count the number of elements in a sequence in F#?

Keith Pinson
  • 7,835
  • 7
  • 61
  • 104
Mark Pearl
  • 7,573
  • 10
  • 47
  • 57

4 Answers4

54

Use Seq.length

Returns the length of the sequence

aloisdg
  • 22,270
  • 6
  • 85
  • 105
Yin Zhu
  • 16,980
  • 13
  • 75
  • 117
8
open System.Linq

mySeq.Count()
Mark H
  • 13,797
  • 4
  • 31
  • 45
  • One advantage of this solution is that you can filter inside a Count(). For example, `x |> Seq.filter (System.Char.IsLetter) |> Seq.length` could be write `(x : string).Count (fun x -> Char.IsLetter(x))` – aloisdg Jan 27 '17 at 11:50
  • Then you could also use Seq.countBy, or not? – Anytoe Mar 26 '17 at 12:19
6

FYI, if you search the library docs for Seq for "-> int" you'll find this rather quickly.

Brian
  • 117,631
  • 17
  • 236
  • 300
5

you can use Seq.length