For me one of the coolest ways of using closures was to implement undo system. It's not very efficient, but easy to write and compose.<p>Basically every editor operation can return a function that does the opposite. For example (some lua-like language)<p>function DeleteLine(lines, line_number)<p><pre><code> local deletedLine = lines[line_number]
-- do the actual deletion - move elements up
return function() InsertLine(lines, line_number, deletedLine) end
end
function InsertLine(lines, line_number, deletedLine)
-- do the actual insertion
return function() DeleteLine(lines, line_number) end
end
</code></pre>
Again, not very efficient - but easy to grasp. Without closures would be hard to compose - you would have to take care of a lot of extra parameters where they need to be stored, etc.