Settings

Theme

Using traversals to batch database queries

chrispenner.ca

4 points by cosmic_quanta 4 months ago · 1 comment

Reader

qrobit 4 months ago

For me lenses are like water. I start each haskell project with `optics` dependency, my favorite trick is deriving Generic and using labels to access the fields[1]. Field accessors never get created in the first place, so you have no namespace pollution and all your data definitions can have fields called `id`, `date`, whatever and never collide with each other or imports from other modules. The only problem is unwieldy error messages in case something goes wrong. Another tool is Magnify and Zoom[2] classes, which are useful for limiting context available to child actions in a monad stack. You can have one "global" state, but give only a slice of that global state to the child action with `magnify #subState1 (myAction1 arg1 arg2)` (or zoom respectively for MonadState actions). This approach is extensively used within Brick[3] TUI library, where you have big "global" app state, and a collection of widgets that have their separate states which you slice from the big state.

[1]: <https://www.youtube.com/live/9hrDm7xDpig?t=2200>

[2]: <https://hackage.haskell.org/package/optics-extra-0.4.2.1/doc...>

[3]: <https://hackage.haskell.org/package/brick>

Keyboard Shortcuts

j
Next item
k
Previous item
o / Enter
Open selected item
?
Show this help
Esc
Close modal / clear selection