Typorithm · medium

Lazy vs Actions

lazy-actionssparklazyactionsdag
Saved on this device
Full lesson · 8 learning steps Open any exercise for focused practice
1

Step 1 of 8 · intro card

Learn Lazy vs Actions

0%

Transformations & Actions

Transformations (select, filter, map, groupBy) are lazy — they build a plan but compute nothing. Actions (collect, count, show, write) trigger execution of that plan.

Transformations such as filter, select, map, and groupBy are lazy: each one only appends a step to a logical plan, a DAG of operations, without touching any data.

Nothing actually runs until an action is called. Actions — collect(), count(), show(), write — force Spark to schedule and execute the accumulated plan.

This lazy-evaluation model lets Spark optimize the whole DAG (pruning columns, pushing down filters) before a single byte is read, which is why filtered.count() does the real work, not the filter chain itself.

01

Transformations are lazy and only build a plan.

02

Actions like count() trigger execution.

03

Spark optimizes the DAG before running.

const filtered = df.filter("revenue > 0").select("country", "revenue");
filtered.count();

Read the implementation

Connect each part of the code to the concept before you type.

1. Lazy transformations

filter and select are transformations — they add to the plan but run no computation yet.

df.filter("revenue > 0").select("country", "revenue")

2. Builds a plan

Assigning the chain stores a logical plan (a DAG of steps), not materialized data.

const filtered =

3. An action triggers execution

count() is an action: it forces Spark to execute the whole plan and return a result.

filtered.count();

A complete learning loop

01

Understand

Read the concept map and follow the code breakdown.

02

Reconstruct

Type the implementation, repair mistakes and work through every recall exercise.

03

Verify

Use the final check without hints; revisit weak concepts in short practice sessions.

Lazy vs Actions: Lesson & Practice · Typorithm | ApplyDjinn