Lua with Neovim: Closures — Upvalues, Counter Factory & Functions That Remember | Episode 16
4views
009:01
T
Taught by Celeste AI - AI Coding Coach
View on YouTubeDescription
What if a function could remember a value between calls — without using globals? That is a closure. In this episode we explain
upvalues, build a counter factory with make_counter(), create a multiplier factory, and see how each closure maintains its own
independent state.
Every keystroke is shown on screen with 3-second pauses so you can follow along at your own pace.
Source code:
https://github.com/GoCelesteAI/learn-lua-in-neovim
What You'll Learn:
- Closure — a function that captures variables from its enclosing scope
- Upvalue — an outer variable captured and remembered by a function
- make_counter() — factory that returns a new counting function
- Each closure has its own independent copy of upvalues
- make_multiplier(factor) — parameterized closure factory
- Why closures replace globals for private, persistent state
Timestamps:
0:00 Intro
0:12 Preview
0:44 Counter Factory — function make_counter()
1:52 Two Independent Counters — counter_a, counter_b
3:57 Run: A counts 1-3, B starts at 1, A continues at 4
4:24 Multiplier Factory — make_multiplier(factor)
6:38 Run: double(5)=10, triple(5)=15, double(10)=20
7:00 Code review
7:08 Recap
Key Takeaways:
1. Closures capture outer variables — called upvalues
2. Each call to a factory returns an independent closure
3. Independent closures share no state with each other
4. Closures replace globals for private, scoped state
This is Episode 16 of the Learn Lua in Neovim series. New episodes cover tables, metatables, OOP, and the Neovim Lua API — all
coded inside Neovim.
Taught by CelesteAI. Like & subscribe for more tutorials!
#lua #neovim #programming #tutorial #beginners #learnlua #luaprogramming #neovimtutorial #luaclosures #upvalues #coding
Tags
lua, lua tutorial, learn lua, lua closures, lua upvalues, lua closure, lua counter factory, lua make_counter, lua multiplier,
lua factory function, lua functions that remember, lua closure tutorial, lua state, lua for beginners, neovim lua, neovim
tutorial, learn lua in neovim, lua episode 16, programming tutorial, coding tutorial, beginner programming, lua 2026, neovim
2026, screenkey, code along, lua basics, lua advanced functions