LengthWhile

LengthWhile[list,crit]

gives the number of contiguous elements ei starting at the beginning of list for which crit[ei] is True.

Details

  • LengthWhile can be used on an object with any head, not necessarily List.

Examples

open allclose all

Basic Examples  (1)

Find the length of the run of elements below 10:

Scope  (1)

Elements of the list can be any expression:

Generalizations & Extensions  (1)

Use on an expression with head f:

The result is the same as for a list of the values:

Applications  (1)

Find the length of the run of digits in before a 7 occurs:

Properties & Relations  (3)

LengthWhile gives the length of the result from TakeWhile:

LengthWhile is equal to Length if crit[ei] is True for all elements:

Otherwise it is 1 less than the first position for which crit[ei] is not True:

Possible Issues  (1)

The count terminates for any value of crit[ei] that is not True:

Introduced in 2007
 (6.0)
 |
Updated in 2008
 (7.0)