Skip to content

📸 Query Snapshots🔗

Queries are lazy and can only be used once (they are exhausted after being iterated). While this provides a significant performance boost, it can be inconvenient in some cases.

This is where snapshots come in.

Snapshots provide a way to create a "checkpoint" for a Query. The snapshot is a Query object, which can be used multiple times, without being exhausted.

Snapshots are Query objects, so they support the same behavior as regular queries. The only difference is that they can be re-used (at the point of the snapping).

Snapshots are created using the snap method.

snap()🔗

Yields the same elements, and creates a snapshot for the query. This snapshot allows for multiple iterations over the same "snapped" iterable. If multiple snapshots are created in a query lifetime, the last one is considered.

Assumes a finite iterable.

Examples:

>>> from fliq import q
>>> evens = q(range(10)).where(lambda x: x % 2 == 0).snap()
>>> evens.where(lambda x: x > 3).count()
3
>>> evens.first()
0
>>> evens.select(lambda x: x ** 2).to_list()
[0, 4, 16, 36, 64]