Counting with float arguments

The count() function permits non-integer values. We can use something such as the count(0.5, 0.1) method to provide floating-point values. This will accumulate an error if the increment value doesn't have an exact representation. It's generally better to use integer count() arguments such as (0.5+x*.1 for x in count()) to assure that representation errors don't accumulate.

Here's a way to examine the accumulating error. This exploration of the float approximation shows some interesting functional programming techniques.

We'll define a function that will evaluate items from an iterator until some condition is met. Here's how we can define the until() function:

from typing import Callable, Iterator TypeVarT_ = ...

Get Functional Python Programming - Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.