Back to Over

Iterators · Over · Practice Question

Backward-Fill Nulls with the Next Non-Null Value

Sign in to track your progress

Question

Given:

l:10 8 0n 1 0n 0n 12 13 0n 7

Replace all nulls with the next non-null value.

Expected result:

10 8 1 1 12 12 12 13 7 7f

Solution

{(next x)^x}/[l]

Alternative Solution

reverse fills reverse l

Explanation

(next x)^x fills nulls in x with the value immediately after them. Since l has consecutive nulls, one pass isn't enough on its own — wrapping the function in Converge over (/, no iteration count given) reapplies it until the result stops changing, then returns just that final, fully-filled list.

This is the same function used in the companion Scan question, "Step Through a Backward-Fill Using Scan" — the only difference is / (over) instead of \ (scan), which is exactly the over-vs-scan distinction: over returns only the end state, scan returns every pass along the way.

The alternative uses the built-in fills, which only fills forward (with the previous value) — reversing the list first, filling, then reversing back turns that into a backward fill.